Skip to content

Commit

Permalink
Add fetcher option to transportOptions (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcepa committed Jul 26, 2023
1 parent 87e50c9 commit 3ce9e8e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/toucan-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ This SDK provides all options and methods of [Hub](https://github.com/getsentry/
| 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. |
| requestDataOptions | RequestDataOptions | Object containing allowlist for specific parts of request. Refer to sensitive data section below. |

### Constructor options overrides

#### Transport options

On top of base `transportOptions` you can pass additional configuration:

| Option | Type | Description |
| ------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| headers | Record<string, string> | Custom headers passed to fetch. |
| 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. |

### Additional methods

- `Toucan.setEnabled(enabled: boolean): void`: Can be used to disable and again enable the SDK later in your code.
Expand Down
3 changes: 2 additions & 1 deletion packages/toucan-js/src/transports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function makeFetchTransport(options: FetchTransportOptions): Transport {
body,
}: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
try {
const request = fetch(options.url, {
const fetchFn = options.fetcher ?? fetch;
const request = fetchFn(options.url, {
method: 'POST',
headers: options.headers,
body,
Expand Down
11 changes: 11 additions & 0 deletions packages/toucan-js/src/transports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import type { BaseTransportOptions } from '@sentry/types';
import type { Context } from '../types';

export type FetchTransportOptions = BaseTransportOptions & {
/**
* Custom headers passed to fetch.
*/
headers?: Record<string, string>;

/**
* Cloudflare Workers context.
*/
context?: Context;

/**
* Custom fetch function.
*/
fetcher?: typeof fetch;
};
4 changes: 4 additions & 0 deletions packages/toucan-js/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const realMathRandom = Math.random;
let mathRandomReturnValues: number[] = [];
let mathRandomReturnValuesCurrentIndex = -1;

export const mockFetch = () => {
return jest.fn(async () => new Response());
};

export const mockMathRandom = (returnValues: number[]) => {
if (returnValues.length === 0)
jest.fn(() => {
Expand Down
19 changes: 19 additions & 0 deletions packages/toucan-js/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Event, EventProcessor, Integration } from '@sentry/types';
import { Toucan } from 'toucan-js';
import {
mockConsole,
mockFetch,
mockMathRandom,
resetConsole,
resetMathRandom,
Expand Down Expand Up @@ -309,6 +310,24 @@ describe('Toucan', () => {
expect(requests[0].headers['x-custom-header']).toEqual('1');
});

test('custom fetcher', async () => {
const fetcher = mockFetch();
const toucan = new Toucan({
dsn: VALID_DSN,
transportOptions: {
fetcher,
},
context,
});
toucan.captureMessage('test');

const waitUntilResults = await getMiniflareWaitUntil(context);

expect(waitUntilResults.length).toBe(1);
expect(requests.length).toBe(0);
expect(fetcher.mock.calls.length).toBe(1);
});

test('unhandled exception in SDK options does not explode the worker', async () => {
const toucan = new Toucan({
dsn: VALID_DSN,
Expand Down

0 comments on commit 3ce9e8e

Please sign in to comment.