Skip to content

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

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented Aug 2, 2024

While working on adding the cloudflare sdk to some open source projects, I noticed that setup for the cloudflare plugin was a bit of a hassle when you needed access to environmental variables.

This PR allows users to pass a function to sentryPagesPlugin that looks like so:

handler: (context: EventPluginContext<Env, Params, Data, PluginParams>) => CloudflareOptions

This means that users can access the cloudflare context (which only exists at the request level) to get environmental variables.

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

To make some other use cases easier, this PR also exposes the wrapRequestHandler API to users.

@AbhiPrasad AbhiPrasad requested a review from a team August 2, 2024 14:00
@AbhiPrasad AbhiPrasad self-assigned this Aug 2, 2024
@AbhiPrasad AbhiPrasad requested review from Lms24 and nicohrubec and removed request for a team August 2, 2024 14:00
@@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weakening the type, but it's on purpose - constraining the PluginParams to be both an object and function doesn't work really well 😬.

@@ -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
Copy link
Member

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?

Copy link
Member Author

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

@AbhiPrasad AbhiPrasad force-pushed the abhi-cloudflare-types-fix branch from 307d7ef to cbe6d17 Compare August 2, 2024 18:38
@AbhiPrasad AbhiPrasad added the Package: cloudflare Issues related to the Sentry Cloudflare Workers SDK label Aug 2, 2024
@AbhiPrasad AbhiPrasad enabled auto-merge (squash) August 2, 2024 19:16
@AbhiPrasad AbhiPrasad merged commit 07a30e5 into develop Aug 2, 2024
97 checks passed
@AbhiPrasad AbhiPrasad deleted the abhi-cloudflare-types-fix branch August 2, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: cloudflare Issues related to the Sentry Cloudflare Workers SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants