Skip to content

Commit 28a430d

Browse files
author
Luca Forstner
authored
ref(nextjs): Rename wrapper functions (#6788)
1 parent aed2ce6 commit 28a430d

28 files changed

+169
-48
lines changed

packages/nextjs/src/config/templates/apiWrapperTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const config = {
5454
},
5555
};
5656

57-
export default userProvidedHandler ? Sentry.withSentryAPI(userProvidedHandler, '__ROUTE__') : undefined;
57+
export default userProvidedHandler ? Sentry.wrapApiHandlerWithSentry(userProvidedHandler, '__ROUTE__') : undefined;
5858

5959
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
6060
// not include anything whose name matchs something we've explicitly exported above.

packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ if ('middleware' in userApiModule && typeof userApiModule.middleware === 'functi
4040
userProvidedDefaultHandler = userApiModule;
4141
}
4242

43-
export const middleware = userProvidedNamedHandler ? Sentry.withSentryMiddleware(userProvidedNamedHandler) : undefined;
44-
export default userProvidedDefaultHandler ? Sentry.withSentryMiddleware(userProvidedDefaultHandler) : undefined;
43+
export const middleware = userProvidedNamedHandler
44+
? Sentry.wrapMiddlewareWithSentry(userProvidedNamedHandler)
45+
: undefined;
46+
export default userProvidedDefaultHandler ? Sentry.wrapMiddlewareWithSentry(userProvidedDefaultHandler) : undefined;
4547

4648
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
4749
// not include anything whose name matchs something we've explicitly exported above.

packages/nextjs/src/config/templates/pageWrapperTemplate.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ const origGetStaticProps = userPageModule.getStaticProps;
2828
const origGetServerSideProps = userPageModule.getServerSideProps;
2929

3030
const getInitialPropsWrappers: Record<string, any> = {
31-
'/_app': Sentry.withSentryServerSideAppGetInitialProps,
32-
'/_document': Sentry.withSentryServerSideDocumentGetInitialProps,
33-
'/_error': Sentry.withSentryServerSideErrorGetInitialProps,
31+
'/_app': Sentry.wrapAppGetInitialPropsWithSentry,
32+
'/_document': Sentry.wrapDocumentGetInitialPropsWithSentry,
33+
'/_error': Sentry.wrapErrorGetInitialPropsWithSentry,
3434
};
3535

36-
const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.withSentryServerSideGetInitialProps;
36+
const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.wrapGetInitialPropsWithSentry;
3737

3838
if (typeof origGetInitialProps === 'function') {
3939
pageComponent.getInitialProps = getInitialPropsWrapper(origGetInitialProps) as NextPageComponent['getInitialProps'];
4040
}
4141

4242
export const getStaticProps =
4343
typeof origGetStaticProps === 'function'
44-
? Sentry.withSentryGetStaticProps(origGetStaticProps, '__ROUTE__')
44+
? Sentry.wrapGetStaticPropsWithSentry(origGetStaticProps, '__ROUTE__')
4545
: undefined;
4646
export const getServerSideProps =
4747
typeof origGetServerSideProps === 'function'
48-
? Sentry.withSentryGetServerSideProps(origGetServerSideProps, '__ROUTE__')
48+
? Sentry.wrapGetServerSidePropsWithSentry(origGetServerSideProps, '__ROUTE__')
4949
: undefined;
5050

5151
export default pageComponent;

packages/nextjs/src/edge/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,11 @@ export function lastEventId(): string | undefined {
131131
export { flush } from './utils/flush';
132132

133133
export * from '@sentry/core';
134-
export { withSentryAPI } from './withSentryAPI';
135-
export { withSentryMiddleware } from './withSentryMiddleware';
134+
135+
export {
136+
// eslint-disable-next-line deprecation/deprecation
137+
withSentryAPI,
138+
wrapApiHandlerWithSentry,
139+
} from './wrapApiHandlerWithSentry';
140+
141+
export { wrapMiddlewareWithSentry } from './wrapMiddlewareWithSentry';

packages/nextjs/src/edge/withSentryAPI.ts renamed to packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';
66
/**
77
* Wraps a Next.js edge route handler with Sentry error and performance instrumentation.
88
*/
9-
export function withSentryAPI<H extends EdgeRouteHandler>(
9+
export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
1010
handler: H,
1111
parameterizedRoute: string,
1212
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
@@ -21,9 +21,14 @@ export function withSentryAPI<H extends EdgeRouteHandler>(
2121
? `handler (${parameterizedRoute})`
2222
: `${req.method} ${parameterizedRoute}`,
2323
spanOp: activeSpan ? 'function' : 'http.server',
24-
mechanismFunctionName: 'withSentryAPI',
24+
mechanismFunctionName: 'wrapApiHandlerWithSentry',
2525
});
2626

2727
return await wrappedHandler.apply(this, args);
2828
};
2929
}
30+
31+
/**
32+
* @deprecated Use `wrapApiHandlerWithSentry` instead.
33+
*/
34+
export const withSentryAPI = wrapApiHandlerWithSentry;

packages/nextjs/src/edge/withSentryMiddleware.ts renamed to packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';
33

44
/**
55
* Wraps Next.js middleware with Sentry error and performance instrumentation.
6+
*
7+
* @param middleware The middleware handler.
8+
* @returns a wrapped middleware handler.
69
*/
7-
export function withSentryMiddleware<H extends EdgeRouteHandler>(
10+
export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
811
middleware: H,
912
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
1013
return withEdgeWrapping(middleware, {

packages/nextjs/src/index.types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,26 @@ export declare function flush(timeout?: number | undefined): PromiseLike<boolean
3030
export declare function lastEventId(): string | undefined;
3131
export declare function getSentryRelease(fallback?: string): string | undefined;
3232

33+
/**
34+
* @deprecated Use `wrapApiHandlerWithSentry` instead
35+
*/
3336
export declare function withSentryAPI<APIHandler extends (...args: any[]) => any>(
3437
handler: APIHandler,
3538
parameterizedRoute: string,
3639
): (
3740
...args: Parameters<APIHandler>
3841
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;
42+
43+
/**
44+
* Wraps a Next.js API handler with Sentry error and performance instrumentation.
45+
*
46+
* @param handler The handler exported from the API route file.
47+
* @param parameterizedRoute The page's parameterized route.
48+
* @returns The wrapped handler.
49+
*/
50+
export declare function wrapApiHandlerWithSentry<APIHandler extends (...args: any[]) => any>(
51+
handler: APIHandler,
52+
parameterizedRoute: string,
53+
): (
54+
...args: Parameters<APIHandler>
55+
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;

packages/nextjs/src/server/index.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,44 @@ const deprecatedIsBuild = (): boolean => isBuild();
148148
// eslint-disable-next-line deprecation/deprecation
149149
export { deprecatedIsBuild as isBuild };
150150

151-
export { withSentryGetStaticProps } from './withSentryGetStaticProps';
152-
export { withSentryServerSideGetInitialProps } from './withSentryServerSideGetInitialProps';
153-
export { withSentryServerSideAppGetInitialProps } from './withSentryServerSideAppGetInitialProps';
154-
export { withSentryServerSideDocumentGetInitialProps } from './withSentryServerSideDocumentGetInitialProps';
155-
export { withSentryServerSideErrorGetInitialProps } from './withSentryServerSideErrorGetInitialProps';
156-
export { withSentryGetServerSideProps } from './withSentryGetServerSideProps';
157-
export { withSentry, withSentryAPI } from './withSentryAPI';
151+
export {
152+
// eslint-disable-next-line deprecation/deprecation
153+
withSentryGetStaticProps,
154+
wrapGetStaticPropsWithSentry,
155+
} from './wrapGetStaticPropsWithSentry';
156+
157+
export {
158+
// eslint-disable-next-line deprecation/deprecation
159+
withSentryServerSideGetInitialProps,
160+
wrapGetInitialPropsWithSentry,
161+
} from './wrapGetInitialPropsWithSentry';
162+
163+
export {
164+
// eslint-disable-next-line deprecation/deprecation
165+
withSentryServerSideAppGetInitialProps,
166+
wrapAppGetInitialPropsWithSentry,
167+
} from './wrapAppGetInitialPropsWithSentry';
168+
export {
169+
// eslint-disable-next-line deprecation/deprecation
170+
withSentryServerSideDocumentGetInitialProps,
171+
wrapDocumentGetInitialPropsWithSentry,
172+
} from './wrapDocumentGetInitialPropsWithSentry';
173+
export {
174+
// eslint-disable-next-line deprecation/deprecation
175+
withSentryServerSideErrorGetInitialProps,
176+
wrapErrorGetInitialPropsWithSentry,
177+
} from './wrapErrorGetInitialPropsWithSentry';
178+
179+
export {
180+
// eslint-disable-next-line deprecation/deprecation
181+
withSentryGetServerSideProps,
182+
wrapGetServerSidePropsWithSentry,
183+
} from './wrapGetServerSidePropsWithSentry';
184+
185+
export {
186+
// eslint-disable-next-line deprecation/deprecation
187+
withSentry,
188+
// eslint-disable-next-line deprecation/deprecation
189+
withSentryAPI,
190+
wrapApiHandlerWithSentry,
191+
} from './wrapApiHandlerWithSentry';

packages/nextjs/src/server/withSentryAPI.ts renamed to packages/nextjs/src/server/wrapApiHandlerWithSentry.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { autoEndTransactionOnResponseEnd, finishTransaction, flushQueue } from '
2525
* @param parameterizedRoute The page's route, passed in via the proxy loader
2626
* @returns The wrapped handler
2727
*/
28-
export function withSentryAPI(
28+
export function wrapApiHandlerWithSentry(
2929
maybeWrappedHandler: NextApiHandler | WrappedNextApiHandler,
3030
parameterizedRoute: string,
3131
): WrappedNextApiHandler {
@@ -47,20 +47,28 @@ export function withSentryAPI(
4747
);
4848
}
4949

50+
// eslint-disable-next-line deprecation/deprecation
5051
return withSentry(maybeWrappedHandler, parameterizedRoute);
5152
}
5253

5354
/**
54-
* Legacy function for manually wrapping API route handlers, now used as the innards of `withSentryAPI`.
55+
* @deprecated Use `wrapApiHandlerWithSentry()` instead
56+
*/
57+
export const withSentryAPI = wrapApiHandlerWithSentry;
58+
59+
/**
60+
* Legacy function for manually wrapping API route handlers, now used as the innards of `wrapApiHandlerWithSentry`.
5561
*
5662
* @param origHandler The user's original API route handler
5763
* @param parameterizedRoute The route whose handler is being wrapped. Meant for internal use only.
5864
* @returns A wrapped version of the handler
65+
*
66+
* @deprecated Use `wrapApiWithSentry()` instead
5967
*/
6068
export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: string): WrappedNextApiHandler {
6169
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
6270
return async function sentryWrappedHandler(req: AugmentedNextApiRequest, res: AugmentedNextApiResponse) {
63-
// We're now auto-wrapping API route handlers using `withSentryAPI` (which uses `withSentry` under the hood), but
71+
// We're now auto-wrapping API route handlers using `wrapApiHandlerWithSentry` (which uses `withSentry` under the hood), but
6472
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
6573
// idempotent so that those cases don't break anything.
6674
if (req.__withSentry_applied__) {

packages/nextjs/src/server/withSentryServerSideAppGetInitialProps.ts renamed to packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type AppGetInitialProps = typeof App['getInitialProps'];
1919
* @param parameterizedRoute The page's parameterized route
2020
* @returns A wrapped version of the function
2121
*/
22-
export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
22+
export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
2323
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
2424
if (isBuild()) {
2525
return origAppGetInitialProps.apply(this, args);
@@ -72,3 +72,8 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A
7272
}
7373
};
7474
}
75+
76+
/**
77+
* @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
78+
*/
79+
export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry;

0 commit comments

Comments
 (0)