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
25 changes: 25 additions & 0 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ describe("Codex", () => {
});
});

describe("fetch override", () => {
it("should use the fetch override", async () => {
const fetchMock = jest.fn((input, init) => {
console.log("fetchMock", input, init);
return Promise.resolve(
new Response(
JSON.stringify({
data: { getNetworks: [] },
}),
{
status: 200,
headers: new Headers({
"Content-Type": "application/json",
}),
},
),
);
});

const sdkWithFetch = new Codex("test-key", { fetch: fetchMock });
await sdkWithFetch.query(getNetworksDocument, {});
expect(fetchMock).toHaveBeenCalled();
});
});

describe("dispose", () => {
it("should dispose websocket client when it exists", async () => {
const mockDispose = jest.fn();
Expand Down
4 changes: 4 additions & 0 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type ApiConfig = {

// Use this to apply dynamic headers to the requests
applyHeaders?: () => Promise<Record<string, string>>;

// Use this to override the default fetch implementation.
fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
};

const defaultConfig: ApiConfig = {
Expand Down Expand Up @@ -83,6 +86,7 @@ export class Codex {
"X-Apollo-Operation-Name": "query",
...config.headers,
}),
fetch: config.fetch,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume if config.fetch is undefined then it'll revert to it's original behaviour properly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct - the underlying graphql-request library provides a default implementation if the configured value is undefined.

});
}

Expand Down