Skip to content

Commit 3ce9e8e

Browse files
authored
Add fetcher option to transportOptions (#195)
1 parent 87e50c9 commit 3ce9e8e

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

packages/toucan-js/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ This SDK provides all options and methods of [Hub](https://github.com/getsentry/
2626
| request | Request | If set, the SDK will send information about incoming requests to Sentry. By default, only the request method and request origin + pathname are sent. If you want to include more data, you need to use `requestDataOptions` option. |
2727
| requestDataOptions | RequestDataOptions | Object containing allowlist for specific parts of request. Refer to sensitive data section below. |
2828

29+
### Constructor options overrides
30+
31+
#### Transport options
32+
33+
On top of base `transportOptions` you can pass additional configuration:
34+
35+
| Option | Type | Description |
36+
| ------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
37+
| headers | Record<string, string> | Custom headers passed to fetch. |
38+
| fetcher | typeof fetch | Custom fetch function. This can be useful for tests or when the global `fetch` used by `toucan-js` doesn't satisfy your use-cases. Note that custom fetcher must conform to `fetch` interface. |
39+
2940
### Additional methods
3041

3142
- `Toucan.setEnabled(enabled: boolean): void`: Can be used to disable and again enable the SDK later in your code.

packages/toucan-js/src/transports/fetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export function makeFetchTransport(options: FetchTransportOptions): Transport {
1515
body,
1616
}: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
1717
try {
18-
const request = fetch(options.url, {
18+
const fetchFn = options.fetcher ?? fetch;
19+
const request = fetchFn(options.url, {
1920
method: 'POST',
2021
headers: options.headers,
2122
body,

packages/toucan-js/src/transports/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ import type { BaseTransportOptions } from '@sentry/types';
22
import type { Context } from '../types';
33

44
export type FetchTransportOptions = BaseTransportOptions & {
5+
/**
6+
* Custom headers passed to fetch.
7+
*/
58
headers?: Record<string, string>;
69

10+
/**
11+
* Cloudflare Workers context.
12+
*/
713
context?: Context;
14+
15+
/**
16+
* Custom fetch function.
17+
*/
18+
fetcher?: typeof fetch;
819
};

packages/toucan-js/test/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const realMathRandom = Math.random;
44
let mathRandomReturnValues: number[] = [];
55
let mathRandomReturnValuesCurrentIndex = -1;
66

7+
export const mockFetch = () => {
8+
return jest.fn(async () => new Response());
9+
};
10+
711
export const mockMathRandom = (returnValues: number[]) => {
812
if (returnValues.length === 0)
913
jest.fn(() => {

packages/toucan-js/test/index.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Event, EventProcessor, Integration } from '@sentry/types';
22
import { Toucan } from 'toucan-js';
33
import {
44
mockConsole,
5+
mockFetch,
56
mockMathRandom,
67
resetConsole,
78
resetMathRandom,
@@ -309,6 +310,24 @@ describe('Toucan', () => {
309310
expect(requests[0].headers['x-custom-header']).toEqual('1');
310311
});
311312

313+
test('custom fetcher', async () => {
314+
const fetcher = mockFetch();
315+
const toucan = new Toucan({
316+
dsn: VALID_DSN,
317+
transportOptions: {
318+
fetcher,
319+
},
320+
context,
321+
});
322+
toucan.captureMessage('test');
323+
324+
const waitUntilResults = await getMiniflareWaitUntil(context);
325+
326+
expect(waitUntilResults.length).toBe(1);
327+
expect(requests.length).toBe(0);
328+
expect(fetcher.mock.calls.length).toBe(1);
329+
});
330+
312331
test('unhandled exception in SDK options does not explode the worker', async () => {
313332
const toucan = new Toucan({
314333
dsn: VALID_DSN,

0 commit comments

Comments
 (0)