@@ -60,6 +60,24 @@ import { setSDKProcessingMetadata } from './set-sdk-processing-metadata';
6060const getExpressExport = ( express : ExpressModuleExport ) : ExpressExport =>
6161 hasDefaultProp ( express ) ? express . default : ( express as ExpressExport ) ;
6262
63+ function isLegacyOptions (
64+ options : ExpressModuleExport | ( ExpressIntegrationOptions & { express : ExpressModuleExport } ) ,
65+ ) : options is ExpressIntegrationOptions & { express : ExpressModuleExport } {
66+ return ! ! ( options as { express : ExpressModuleExport } ) . express ;
67+ }
68+
69+ // TODO: remove this deprecation handling in v11
70+ let didLegacyDeprecationWarning = false ;
71+ function deprecationWarning ( ) {
72+ if ( ! didLegacyDeprecationWarning ) {
73+ didLegacyDeprecationWarning = true ;
74+ DEBUG_BUILD &&
75+ debug . warn (
76+ '[Express] `patchExpressModule(options)` is deprecated. Use `patchExpressModule(moduleExports, getOptions)` instead.' ,
77+ ) ;
78+ }
79+ }
80+
6381/**
6482 * This is a portable instrumentatiton function that works in any environment
6583 * where Express can be loaded, without depending on OpenTelemetry.
@@ -69,11 +87,39 @@ const getExpressExport = (express: ExpressModuleExport): ExpressExport =>
6987 * import express from 'express';
7088 * import * as Sentry from '@sentry/deno'; // or any SDK that extends core
7189 *
72- * Sentry.patchExpressModule({ express })
90+ * Sentry.patchExpressModule(express, () => ({}));
91+ * ```
7392 */
74- export const patchExpressModule = ( options : ExpressIntegrationOptions ) => {
93+ export function patchExpressModule (
94+ moduleExports : ExpressModuleExport ,
95+ getOptions : ( ) => ExpressIntegrationOptions ,
96+ ) : ExpressModuleExport ;
97+ /**
98+ * @deprecated Pass the Express module export as the first argument and options getter as the second argument.
99+ */
100+ export function patchExpressModule (
101+ options : ExpressIntegrationOptions & { express : ExpressModuleExport } ,
102+ ) : ExpressModuleExport ;
103+ export function patchExpressModule (
104+ optionsOrExports : ExpressModuleExport | ( ExpressIntegrationOptions & { express : ExpressModuleExport } ) ,
105+ maybeGetOptions ?: ( ) => ExpressIntegrationOptions ,
106+ ) : ExpressModuleExport {
107+ let getOptions : ( ) => ExpressIntegrationOptions ;
108+ let moduleExports : ExpressModuleExport ;
109+ if ( ! maybeGetOptions && isLegacyOptions ( optionsOrExports ) ) {
110+ const { express, ...options } = optionsOrExports ;
111+ moduleExports = express ;
112+ getOptions = ( ) => options ;
113+ deprecationWarning ( ) ;
114+ } else if ( typeof maybeGetOptions !== 'function' ) {
115+ throw new TypeError ( '`patchExpressModule(moduleExports, getOptions)` requires a `getOptions` callback' ) ;
116+ } else {
117+ getOptions = maybeGetOptions ;
118+ moduleExports = optionsOrExports as ExpressModuleExport ;
119+ }
120+
75121 // pass in the require() or import() result of express
76- const express = getExpressExport ( options . express ) ;
122+ const express = getExpressExport ( moduleExports ) ;
77123 const routerProto : ExpressRouterv4 | ExpressRouterv5 | undefined = isExpressWithRouterPrototype ( express )
78124 ? express . Router . prototype // Express v5
79125 : isExpressWithoutRouterPrototype ( express )
@@ -93,7 +139,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
93139 function routeTrace ( this : ExpressRouter , ...args : Parameters < typeof originalRouteMethod > [ ] ) {
94140 const route = originalRouteMethod . apply ( this , args ) ;
95141 const layer = this . stack [ this . stack . length - 1 ] as ExpressLayer ;
96- patchLayer ( options , layer , getLayerPath ( args ) ) ;
142+ patchLayer ( getOptions , layer , getLayerPath ( args ) ) ;
97143 return route ;
98144 } ,
99145 ) ;
@@ -113,7 +159,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
113159 if ( ! layer ) {
114160 return route ;
115161 }
116- patchLayer ( options , layer , getLayerPath ( args ) ) ;
162+ patchLayer ( getOptions , layer , getLayerPath ( args ) ) ;
117163 return route ;
118164 } ,
119165 ) ;
@@ -141,7 +187,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
141187 if ( router ) {
142188 const layer = router . stack [ router . stack . length - 1 ] ;
143189 if ( layer ) {
144- patchLayer ( options , layer , getLayerPath ( args ) ) ;
190+ patchLayer ( getOptions , layer , getLayerPath ( args ) ) ;
145191 }
146192 }
147193 return route ;
@@ -152,7 +198,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
152198 }
153199
154200 return express ;
155- } ;
201+ }
156202
157203/**
158204 * An Express-compatible error handler, used by setupExpressErrorHandler
0 commit comments