Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Allow users to pass handler to sentryPagesPlugin#13192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,23 +7,48 @@ import { wrapRequestHandler } from './request'; | ||
| * | ||
| * Initializes the SDK and wraps cloudflare pages requests with SDK instrumentation. | ||
| * | ||
| * @example | ||
| * @example Simple usage | ||
| * | ||
| * ```javascript | ||
| * // functions/_middleware.js | ||
| * import * as Sentry from '@sentry/cloudflare'; | ||
| * | ||
| * export const onRequest = Sentry.sentryPagesPlugin({ | ||
| * dsn: process.env.SENTRY_DSN, | ||
| * tracesSampleRate: 1.0, | ||
| * dsn: process.env.SENTRY_DSN, | ||
| * tracesSampleRate: 1.0, | ||
| * }); | ||
| * ``` | ||
| * | ||
| * @example Usage with handler function to access context for environmental variables | ||
| * | ||
| * ```javascript | ||
| * import * as Sentry from '@sentry/cloudflare'; | ||
| * | ||
| * const const onRequest = Sentry.sentryPagesPlugin((context) => ({ | ||
| * dsn: context.env.SENTRY_DSN, | ||
| * tracesSampleRate: 1.0, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @param handlerOrOptions Configuration options or a function that returns configuration options. | ||
| * @returns A plugin function that can be used in Cloudflare Pages. | ||
| */ | ||
| export function sentryPagesPlugin< | ||
| Env = unknown, | ||
| // 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> { | ||
| // Although it is not ideal to use `any` here, it makes usage more flexible for different setups. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| PluginParams = any, | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weakening the type, but it's on purpose - constraining the | ||
| >( | ||
| 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()); | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: If one can only access env variables in the function, should we update the JSDoc of this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes for sure, good call