@@ -10,10 +10,12 @@ const { parseRequest } = Handlers;
10
10
// purely for clarity
11
11
type WrappedNextApiHandler = NextApiHandler ;
12
12
13
- type AugmentedResponse = NextApiResponse & { __sentryTransaction ?: Transaction } ;
13
+ export type AugmentedNextApiResponse = NextApiResponse & {
14
+ __sentryTransaction ?: Transaction ;
15
+ } ;
14
16
15
17
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
16
- export const withSentry = ( handler : NextApiHandler ) : WrappedNextApiHandler => {
18
+ export const withSentry = ( origHandler : NextApiHandler ) : WrappedNextApiHandler => {
17
19
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
18
20
return async ( req , res ) => {
19
21
// first order of business: monkeypatch `res.end()` so that it will wait for us to send events to sentry before it
@@ -69,12 +71,12 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
69
71
70
72
// save a link to the transaction on the response, so that even if there's an error (landing us outside of
71
73
// the domain), we can still finish it (albeit possibly missing some scope data)
72
- ( res as AugmentedResponse ) . __sentryTransaction = transaction ;
74
+ ( res as AugmentedNextApiResponse ) . __sentryTransaction = transaction ;
73
75
}
74
76
}
75
77
76
78
try {
77
- return await handler ( req , res ) ; // Call original handler
79
+ return await origHandler ( req , res ) ; // Call original handler
78
80
} catch ( e ) {
79
81
if ( currentScope ) {
80
82
currentScope . addEventProcessor ( event => {
@@ -94,12 +96,12 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
94
96
} ;
95
97
} ;
96
98
97
- type ResponseEndMethod = AugmentedResponse [ 'end' ] ;
98
- type WrappedResponseEndMethod = AugmentedResponse [ 'end' ] ;
99
+ type ResponseEndMethod = AugmentedNextApiResponse [ 'end' ] ;
100
+ type WrappedResponseEndMethod = AugmentedNextApiResponse [ 'end' ] ;
99
101
100
102
function wrapEndMethod ( origEnd : ResponseEndMethod ) : WrappedResponseEndMethod {
101
- return async function newEnd ( this : AugmentedResponse , ...args : unknown [ ] ) {
102
103
const transaction = this . __sentryTransaction ;
104
+ return async function newEnd ( this : AugmentedNextApiResponse , ...args : unknown [ ] ) {
103
105
104
106
if ( transaction ) {
105
107
transaction . setHttpStatus ( this . statusCode ) ;
0 commit comments