Skip to content

Commit 7e54293

Browse files
authored
feat: allow overriding default fetch function (#29)
1 parent 72101d8 commit 7e54293

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/sdk/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,31 @@ describe("Codex", () => {
230230
});
231231
});
232232

233+
describe("fetch override", () => {
234+
it("should use the fetch override", async () => {
235+
const fetchMock = jest.fn((input, init) => {
236+
console.log("fetchMock", input, init);
237+
return Promise.resolve(
238+
new Response(
239+
JSON.stringify({
240+
data: { getNetworks: [] },
241+
}),
242+
{
243+
status: 200,
244+
headers: new Headers({
245+
"Content-Type": "application/json",
246+
}),
247+
},
248+
),
249+
);
250+
});
251+
252+
const sdkWithFetch = new Codex("test-key", { fetch: fetchMock });
253+
await sdkWithFetch.query(getNetworksDocument, {});
254+
expect(fetchMock).toHaveBeenCalled();
255+
});
256+
});
257+
233258
describe("dispose", () => {
234259
it("should dispose websocket client when it exists", async () => {
235260
const mockDispose = jest.fn();

src/sdk/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export type ApiConfig = {
3333

3434
// Use this to apply dynamic headers to the requests
3535
applyHeaders?: () => Promise<Record<string, string>>;
36+
37+
// Use this to override the default fetch implementation.
38+
fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
3639
};
3740

3841
const defaultConfig: ApiConfig = {
@@ -83,6 +86,7 @@ export class Codex {
8386
"X-Apollo-Operation-Name": "query",
8487
...config.headers,
8588
}),
89+
fetch: config.fetch,
8690
});
8791
}
8892

0 commit comments

Comments
 (0)