From ea60d87edfe06413219133166e3137cd0d8a0873 Mon Sep 17 00:00:00 2001 From: Chancellor Clark Date: Mon, 3 Feb 2025 12:10:18 -0500 Subject: [PATCH] fix(cloudflare)!: add better type handling for additional context BREAKING CHANGE: Updates the cloudflare withSentry handler generic type to allow passing in additional types used by ExportedHandler. Before: ``` Sentry.withSentry>(...) ``` After: ``` Sentry.withSentry(...) ``` --- packages/cloudflare/src/handler.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/cloudflare/src/handler.ts b/packages/cloudflare/src/handler.ts index 06cb13bb8b05..5e86b4e7122f 100644 --- a/packages/cloudflare/src/handler.ts +++ b/packages/cloudflare/src/handler.ts @@ -17,11 +17,6 @@ import { wrapRequestHandler } from './request'; import { addCloudResourceContext } from './scope-utils'; import { init } from './sdk'; -/** - * Extract environment generic from exported handler. - */ -type ExtractEnv

= P extends ExportedHandler ? Env : never; - /** * Wrapper for Cloudflare handlers. * @@ -33,16 +28,15 @@ type ExtractEnv

= P extends ExportedHandler ? Env : never; * @param handler {ExportedHandler} The handler to wrap. * @returns The wrapped handler. */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function withSentry>( - optionsCallback: (env: ExtractEnv) => CloudflareOptions, - handler: E, -): E { +export function withSentry( + optionsCallback: (env: Env) => CloudflareOptions, + handler: ExportedHandler, +): ExportedHandler { setAsyncLocalStorageAsyncContextStrategy(); if ('fetch' in handler && typeof handler.fetch === 'function' && !isInstrumented(handler.fetch)) { handler.fetch = new Proxy(handler.fetch, { - apply(target, thisArg, args: Parameters>>) { + apply(target, thisArg, args: Parameters>) { const [request, env, context] = args; const options = optionsCallback(env); return wrapRequestHandler({ options, request, context }, () => target.apply(thisArg, args)); @@ -54,7 +48,7 @@ export function withSentry>( if ('scheduled' in handler && typeof handler.scheduled === 'function' && !isInstrumented(handler.scheduled)) { handler.scheduled = new Proxy(handler.scheduled, { - apply(target, thisArg, args: Parameters>>) { + apply(target, thisArg, args: Parameters>) { const [event, env, context] = args; return withIsolationScope(isolationScope => { const options = optionsCallback(env);