Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/errors/sdk-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class LilySdkError extends Error {
this.code = options.code;
this.statusCode = options.statusCode;
this.details = options.details;
this.headers = options.headers;
this.request = options.request;
this.headers = options.headers;
}
Expand Down
1 change: 1 addition & 0 deletions src/http/fetch-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function createFetchHttpClient(
}

const url = buildUrl(config.baseUrl, request.path, request.query);
await lifecycleHooks.beforeRequest?.(request);
const body = serializeBody(request.body);
const headers = buildHeaders(config, request.headers);

Expand Down
2 changes: 1 addition & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AgentClient } from './clients/agent-client';
undefinedimport { AgentClient } from './clients/agent-client';
import { IdentityClient } from './clients/identity-client';
import { PaymentClient } from './clients/payment-client';
import { SystemClient } from './clients/system-client';
Expand Down
20 changes: 20 additions & 0 deletions tests/canonical-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from 'vitest';
import { LilySdk, DEFAULT_API_URL } from '../src/sdk';

describe('DEFAULT_API_URL canonical constant (issue #408)', () => {
it('is exported and equals the canonical URL', () => {
expect(DEFAULT_API_URL).toBe('https://api.lilyprotocol.com');
});

it('LilySdk.create() without options falls back to DEFAULT_API_URL when no env var is set', () => {
const originalEnv = process.env.LILY_API_URL;
delete process.env.LILY_API_URL;
try {
const sdk = LilySdk.create();
expect(sdk.config.baseUrl.toString()).toBe('https://api.lilyprotocol.com/');
} finally {
if (originalEnv !== undefined) process.env.LILY_API_URL = originalEnv;
else delete process.env.LILY_API_URL;
}
});
});
85 changes: 85 additions & 0 deletions tests/lifecycle-hooks-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, it, expect, vi } from "vitest";
import { createFetchHttpClient } from "../src/http/fetch-http-client";
import type { ResolvedLilySdkConfig } from "../src/config/types";
import type { RequestLifecycleHooks } from "../src/http/lifecycle-hooks";

function makeConfig(overrides: Partial<ResolvedLilySdkConfig> = {}): ResolvedLilySdkConfig {
return {
baseUrl: new URL("https://api.example.com"),
apiKey: "test-key",
authToken: undefined,
userAgent: "lily-sdk/test",
defaultHeaders: {},
timeoutMs: 1000,
retry: { retries: 2, retryDelayMs: 10 },
fetch: vi.fn(),
...overrides,
} as ResolvedLilySdkConfig;
}

function jsonResponse(body: unknown, status = 200) {
return () => new Response(JSON.stringify(body), {
status,
headers: { "content-type": "application/json" },
});
}

function jsonErrorResponse(status: number, body: unknown) {
return () => new Response(JSON.stringify(body), {
status,
headers: { "content-type": "application/json" },
});
}

describe("lifecycle hooks integration (issue #409)", () => {
it("calls beforeRequest then afterResponse for a 200 response", async () => {
const calls: string[] = [];
const hooks: RequestLifecycleHooks = {
beforeRequest: () => calls.push("beforeRequest"),
afterResponse: () => calls.push("afterResponse"),
};
const config = makeConfig({ fetch: vi.fn().mockImplementation(jsonResponse({ ok: true })) });
const client = createFetchHttpClient(config, hooks);
await client.request({ method: "GET", path: "/v1/agents" });
expect(calls).toEqual(["beforeRequest", "afterResponse"]);
});

it("calls onRetry on a 429-then-success flow", async () => {
const calls: string[] = [];
const hooks: RequestLifecycleHooks = {
onRetry: () => calls.push("onRetry"),
afterResponse: () => calls.push("afterResponse"),
};
const mockFetch = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({}), { status: 429, headers: { "content-type": "application/json" } }))
.mockResolvedValueOnce(new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }));
const config = makeConfig({ fetch: mockFetch, retry: { retries: 2, retryDelayMs: 1 } });
const client = createFetchHttpClient(config, hooks);
await client.request({ method: "GET", path: "/v1/agents" });
expect(calls).toContain("onRetry");
expect(calls).toContain("afterResponse");
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it("calls onError on a terminal 4xx failure", async () => {
const calls: string[] = [];
const hooks: RequestLifecycleHooks = {
onError: () => calls.push("onError"),
};
const config = makeConfig({ fetch: vi.fn().mockImplementation(jsonErrorResponse(400, { error: "bad" })) });
const client = createFetchHttpClient(config, hooks);
await expect(client.request({ method: "GET", path: "/v1/agents" })).rejects.toThrow();
expect(calls).toContain("onError");
});

it("a throwing hook does not reject the underlying request", async () => {
const hooks: RequestLifecycleHooks = {
beforeRequest: () => { throw new Error("hook boom"); },
afterResponse: () => {},
};
const config = makeConfig({ fetch: vi.fn().mockImplementation(jsonResponse({ ok: true })) });
const client = createFetchHttpClient(config, hooks);
const result = await client.request({ method: "GET", path: "/v1/agents" });
expect(result.status).toBe(200);
});
});