Skip to content

Commit bd8d57d

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

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

codegen/templates/javascript/summary.ts.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type SvixOptions = {
2727
serverUrl?: string;
2828
/** Time in milliseconds to wait for requests to get a response. */
2929
requestTimeout?: number;
30+
fetch?: typeof fetch;
3031
} & XOR<
3132
{
3233
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -63,6 +64,7 @@ export class Svix {
6364
token,
6465
timeout: options.requestTimeout,
6566
retryScheduleInMs: options.retryScheduleInMs,
67+
fetch: options.fetch,
6668
};
6769
return;
6870
}
@@ -72,13 +74,15 @@ export class Svix {
7274
token,
7375
timeout: options.requestTimeout,
7476
numRetries: options.numRetries,
77+
fetch: options.fetch,
7578
};
7679
return;
7780
}
7881
this.requestCtx = {
7982
baseUrl,
8083
token,
8184
timeout: options.requestTimeout,
85+
fetch: options.fetch,
8286
};
8387
}
8488

javascript/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type SvixOptions = {
3636
serverUrl?: string;
3737
/** Time in milliseconds to wait for requests to get a response. */
3838
requestTimeout?: number;
39+
fetch?: typeof fetch;
3940
} & XOR<
4041
{
4142
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -71,6 +72,7 @@ export class Svix {
7172
token,
7273
timeout: options.requestTimeout,
7374
retryScheduleInMs: options.retryScheduleInMs,
75+
fetch: options.fetch,
7476
};
7577
return;
7678
}
@@ -80,13 +82,15 @@ export class Svix {
8082
token,
8183
timeout: options.requestTimeout,
8284
numRetries: options.numRetries,
85+
fetch: options.fetch,
8386
};
8487
return;
8588
}
8689
this.requestCtx = {
8790
baseUrl,
8891
token,
8992
timeout: options.requestTimeout,
93+
fetch: options.fetch,
9094
};
9195
}
9296

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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type SvixRequestContext = {
2424
token: string;
2525
/** Time in milliseconds to wait for requests to get a response. */
2626
timeout?: number;
27+
fetch?: typeof fetch;
2728
} & XOR<
2829
{
2930
/** List of delays (in milliseconds) to wait before each retry attempt.*/
@@ -159,7 +160,8 @@ export class SvixRequest {
159160
},
160161
ctx.retryScheduleInMs,
161162
ctx.retryScheduleInMs?.[0],
162-
ctx.retryScheduleInMs?.length || ctx.numRetries
163+
ctx.retryScheduleInMs?.length || ctx.numRetries,
164+
ctx.fetch
163165
);
164166
return filterResponseForErrors(response);
165167
}
@@ -200,13 +202,14 @@ async function sendWithRetry(
200202
retryScheduleInMs?: number[],
201203
nextInterval = 50,
202204
triesLeft = 2,
205+
fetchImpl: typeof fetch = fetch,
203206
retryCount = 1
204207
): Promise<Response> {
205208
const sleep = (interval: number) =>
206209
new Promise((resolve) => setTimeout(resolve, interval));
207210

208211
try {
209-
const response = await fetch(url, init);
212+
const response = await fetchImpl(url, init);
210213
if (triesLeft <= 0 || response.status < 500) {
211214
return response;
212215
}
@@ -225,6 +228,7 @@ async function sendWithRetry(
225228
retryScheduleInMs,
226229
nextInterval,
227230
--triesLeft,
231+
fetchImpl,
228232
++retryCount
229233
);
230234
}

0 commit comments

Comments
 (0)