Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,38 @@ describe("Codex", () => {
createGraphQLClientSpy.mockRestore();
createWebsocketClientSpy.mockRestore();
});
it("should keep the defaults for options passed as undefined", async () => {
const urls: string[] = [];
const fetchStub = jest.fn(async (input: RequestInfo | URL) => {
urls.push(String(input));
return new Response(JSON.stringify({ data: { getNetworks: [] } }), {
headers: { "content-type": "application/json" },
});
});
const createWebsocketClientSpy = jest.spyOn(
Codex.prototype as any,
"createWebsocketClient",
);

const sdkInstance = new Codex("test-key", {
apiUrl: undefined,
apiRealtimeUrl: undefined,
ws: undefined,
fetch: fetchStub,
});
await sdkInstance.send(getNetworksString);

expect(urls).toEqual(["https://graph.codex.io/graphql"]);
expect(createWebsocketClientSpy).toHaveBeenCalledWith(
expect.objectContaining({
apiRealtimeUrl: "wss://graph.codex.io/graphql",
ws: true,
}),
);

createWebsocketClientSpy.mockRestore();
await sdkInstance.dispose();
});
});

describe("updateConfig", () => {
Expand Down
7 changes: 6 additions & 1 deletion src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ export class Codex {

private parseConfig(config: Partial<ApiConfig>) {
invariant(this.apiKey, "apiKey must be defined");
return Object.assign({}, defaultConfig, config);
// An option passed as undefined (e.g. `apiUrl: process.env.CODEX_API_URL`
// with the variable unset) keeps its default instead of replacing it.
const defined = Object.fromEntries(
Object.entries(config).filter(([, value]) => value !== undefined),
);
return Object.assign({}, defaultConfig, defined) as ApiConfig;
}

private createGraphQLClient(config: ApiConfig) {
Expand Down