Skip to content

Commit 05a871a

Browse files
committed
feat(cloudflare): Allow specifying a custom fetch in Cloudflare transport options
1 parent 3b0728f commit 05a871a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/cloudflare/src/transport.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { BaseTransportOptions, Transport, TransportMakeRequestResponse, Tra
22
import { createTransport, SENTRY_BUFFER_FULL_ERROR, suppressTracing } from '@sentry/core';
33

44
export interface CloudflareTransportOptions extends BaseTransportOptions {
5+
/** Custom fetch function to use. This allows usage of things like Workers VPC */
6+
fetch?: typeof fetch;
57
/** Fetch API init parameters. */
68
fetchOptions?: RequestInit;
79
}
@@ -87,7 +89,7 @@ export function makeCloudflareTransport(options: CloudflareTransportOptions): Tr
8789
};
8890

8991
return suppressTracing(() => {
90-
return fetch(options.url, requestOptions).then(response => {
92+
return (options.fetch ?? fetch)(options.url, requestOptions).then(response => {
9193
return {
9294
statusCode: response.status,
9395
headers: {

packages/cloudflare/test/transport.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,20 @@ describe('IsolatedPromiseBuffer', () => {
161161
expect(task1).toHaveBeenCalled();
162162
expect(task2).toHaveBeenCalled();
163163
});
164+
165+
it('should allow for a custom fetch function to be passed in', async () => {
166+
const customFetch = vi.fn(async () => {
167+
return {
168+
headers: new Headers(),
169+
status: 200,
170+
text: () => Promise.resolve({}),
171+
} as unknown as Response;
172+
});
173+
174+
const transport = makeCloudflareTransport({ ...DEFAULT_EDGE_TRANSPORT_OPTIONS, fetch: customFetch });
175+
176+
await transport.send(ERROR_ENVELOPE);
177+
await transport.flush();
178+
expect(customFetch).toHaveBeenCalledTimes(1);
179+
});
164180
});

0 commit comments

Comments
 (0)