Skip to content

Commit 67f0912

Browse files
committed
js: Add option to configure the fetch method
1 parent 0c14b4f commit 67f0912

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

codegen/templates/javascript/summary.ts.jinja

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export type SvixOptions = {
2727
serverUrl?: string;
2828
/** Time in milliseconds to wait for requests to get a response. */
2929
requestTimeout?: number;
30+
/**
31+
* Custom fetch implementation to use for HTTP requests.
32+
* Useful for testing, adding custom middleware, or running in non-standard environments.
33+
*/
34+
fetch?: typeof fetch;
3035
} & XOR<
3136
{
3237
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -63,6 +68,7 @@ export class Svix {
6368
token,
6469
timeout: options.requestTimeout,
6570
retryScheduleInMs: options.retryScheduleInMs,
71+
fetch: options.fetch,
6672
};
6773
return;
6874
}
@@ -72,13 +78,15 @@ export class Svix {
7278
token,
7379
timeout: options.requestTimeout,
7480
numRetries: options.numRetries,
81+
fetch: options.fetch,
7582
};
7683
return;
7784
}
7885
this.requestCtx = {
7986
baseUrl,
8087
token,
8188
timeout: options.requestTimeout,
89+
fetch: options.fetch,
8290
};
8391
}
8492

javascript/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export type SvixOptions = {
3636
serverUrl?: string;
3737
/** Time in milliseconds to wait for requests to get a response. */
3838
requestTimeout?: number;
39+
/**
40+
* Custom fetch implementation to use for HTTP requests.
41+
* Useful for testing, adding custom middleware, or running in non-standard environments.
42+
*/
43+
fetch?: typeof fetch;
3944
} & XOR<
4045
{
4146
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -71,6 +76,7 @@ export class Svix {
7176
token,
7277
timeout: options.requestTimeout,
7378
retryScheduleInMs: options.retryScheduleInMs,
79+
fetch: options.fetch,
7480
};
7581
return;
7682
}
@@ -80,13 +86,15 @@ export class Svix {
8086
token,
8187
timeout: options.requestTimeout,
8288
numRetries: options.numRetries,
89+
fetch: options.fetch,
8390
};
8491
return;
8592
}
8693
this.requestCtx = {
8794
baseUrl,
8895
token,
8996
timeout: options.requestTimeout,
97+
fetch: options.fetch,
9098
};
9199
}
92100

javascript/src/mockttp.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,22 @@ test("mockttp tests", async (t) => {
500500
const requests = await endpointMock.getSeenRequests();
501501
assert.equal(requests.length, 1);
502502
});
503+
504+
await t.test("should use custom fetch implementation", async () => {
505+
let customFetchCalled = false;
506+
const mockFetch: typeof fetch = async (_input, _init) => {
507+
customFetchCalled = true;
508+
return new Response(ListResponseApplicationOut, {
509+
status: 200,
510+
headers: { "Content-Type": "application/json" },
511+
});
512+
};
513+
514+
await mockServer
515+
.forGet(/\/api\/v1\/app.*/)
516+
.thenReply(200, ListResponseApplicationOut);
517+
const svx = new Svix("token", { serverUrl: mockServer.url, fetch: mockFetch });
518+
await svx.application.list({ order: Ordering.Ascending });
519+
assert(customFetchCalled);
520+
});
503521
});

javascript/src/request.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export type SvixRequestContext = {
2424
token: string;
2525
/** Time in milliseconds to wait for requests to get a response. */
2626
timeout?: number;
27+
/**
28+
* Custom fetch implementation to use for HTTP requests.
29+
* Useful for testing, adding custom middleware, or running in non-standard environments.
30+
*/
31+
fetch?: typeof fetch;
2732
} & XOR<
2833
{
2934
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -159,7 +164,8 @@ export class SvixRequest {
159164
},
160165
ctx.retryScheduleInMs,
161166
ctx.retryScheduleInMs?.[0],
162-
ctx.retryScheduleInMs?.length || ctx.numRetries
167+
ctx.retryScheduleInMs?.length || ctx.numRetries,
168+
ctx.fetch
163169
);
164170
return filterResponseForErrors(response);
165171
}
@@ -200,13 +206,14 @@ async function sendWithRetry(
200206
retryScheduleInMs?: number[],
201207
nextInterval = 50,
202208
triesLeft = 2,
209+
fetchImpl: typeof fetch = fetch,
203210
retryCount = 1
204211
): Promise<Response> {
205212
const sleep = (interval: number) =>
206213
new Promise((resolve) => setTimeout(resolve, interval));
207214

208215
try {
209-
const response = await fetch(url, init);
216+
const response = await fetchImpl(url, init);
210217
if (triesLeft <= 0 || response.status < 500) {
211218
return response;
212219
}
@@ -225,6 +232,7 @@ async function sendWithRetry(
225232
retryScheduleInMs,
226233
nextInterval,
227234
--triesLeft,
235+
fetchImpl,
228236
++retryCount
229237
);
230238
}

0 commit comments

Comments
 (0)