Skip to content

Commit

Permalink
feat(cloudflare): Allow users to pass handler to sentryPagesPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Aug 2, 2024
1 parent 21b0dae commit 307d7ef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export const onRequest = [
];
```

If you need to access the `context` object (for example to grab environmental variables), you can pass a function to
`sentryPagesPlugin` that takes the `context` object as an argument and returns `init` options:

```javascript
export const onRequest = Sentry.sentryPagesPlugin(context => ({
dsn: context.env.SENTRY_DSN,
tracesSampleRate: 1.0,
}));
```

## Setup (Cloudflare Workers)

To use this SDK, wrap your handler with the `withSentry` function. This will initialize the SDK and hook into the
Expand Down
13 changes: 11 additions & 2 deletions packages/cloudflare/src/pages-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ export function sentryPagesPlugin<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Params extends string = any,
Data extends Record<string, unknown> = Record<string, unknown>,
>(options: CloudflareOptions): PagesPluginFunction<Env, Params, Data, CloudflareOptions> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
PluginParams = any,
>(
handlerOrOptions:
| CloudflareOptions
| ((context: EventPluginContext<Env, Params, Data, PluginParams>) => CloudflareOptions),
): PagesPluginFunction<Env, Params, Data, PluginParams> {
setAsyncLocalStorageAsyncContextStrategy();
return context => wrapRequestHandler({ options, request: context.request, context }, () => context.next());
return context => {
const options = typeof handlerOrOptions === 'function' ? handlerOrOptions(context) : handlerOrOptions;
return wrapRequestHandler({ options, request: context.request, context }, () => context.next());
};
}
22 changes: 22 additions & 0 deletions packages/cloudflare/test/pages-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ describe('sentryPagesPlugin', () => {
vi.clearAllMocks();
});

test('calls handler function if a function is provided', async () => {
const mockOptionsHandler = vi.fn().mockReturnValue(MOCK_OPTIONS);
const mockOnRequest = sentryPagesPlugin(mockOptionsHandler);

const MOCK_CONTEXT = {
request: new Request('https://example.com'),
functionPath: 'test',
waitUntil: vi.fn(),
passThroughOnException: vi.fn(),
next: () => Promise.resolve(new Response('test')),
env: { ASSETS: { fetch: vi.fn() } },
params: {},
data: {},
pluginArgs: MOCK_OPTIONS,
};

await mockOnRequest(MOCK_CONTEXT);

expect(mockOptionsHandler).toHaveBeenCalledTimes(1);
expect(mockOptionsHandler).toHaveBeenLastCalledWith(MOCK_CONTEXT);
});

test('passes through the response from the handler', async () => {
const response = new Response('test');
const mockOnRequest = sentryPagesPlugin(MOCK_OPTIONS);
Expand Down

0 comments on commit 307d7ef

Please sign in to comment.