Skip to content

Commit 28a430d

Browse files
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;

packages/nextjs/src/server/withSentryServerSideDocumentGetInitialProps.ts renamed to packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type DocumentGetInitialProps = typeof Document.getInitialProps;
1414
* @param parameterizedRoute The page's parameterized route
1515
* @returns A wrapped version of the function
1616
*/
17-
export function withSentryServerSideDocumentGetInitialProps(
17+
export function wrapDocumentGetInitialPropsWithSentry(
1818
origDocumentGetInitialProps: DocumentGetInitialProps,
1919
): DocumentGetInitialProps {
2020
return async function (
@@ -47,3 +47,8 @@ export function withSentryServerSideDocumentGetInitialProps(
4747
}
4848
};
4949
}
50+
51+
/**
52+
* @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
53+
*/
54+
export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry;

packages/nextjs/src/server/withSentryServerSideErrorGetInitialProps.ts renamed to packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
2020
* @param parameterizedRoute The page's parameterized route
2121
* @returns A wrapped version of the function
2222
*/
23-
export function withSentryServerSideErrorGetInitialProps(
23+
export function wrapErrorGetInitialPropsWithSentry(
2424
origErrorGetInitialProps: ErrorGetInitialProps,
2525
): ErrorGetInitialProps {
2626
return async function (this: unknown, ...args: Parameters<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
@@ -63,3 +63,8 @@ export function withSentryServerSideErrorGetInitialProps(
6363
}
6464
};
6565
}
66+
67+
/**
68+
* @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
69+
*/
70+
export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry;

packages/nextjs/src/server/withSentryServerSideGetInitialProps.ts renamed to packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type GetInitialProps = Required<NextPage>['getInitialProps'];
1818
* @param parameterizedRoute The page's parameterized route
1919
* @returns A wrapped version of the function
2020
*/
21-
export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInitialProps): GetInitialProps {
21+
export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps {
2222
return async function (this: unknown, ...args: Parameters<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
2323
if (isBuild()) {
2424
return origGetInitialProps.apply(this, args);
@@ -59,3 +59,8 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit
5959
}
6060
};
6161
}
62+
63+
/**
64+
* @deprecated Use `wrapGetInitialPropsWithSentry` instead.
65+
*/
66+
export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry;

packages/nextjs/src/server/withSentryGetServerSideProps.ts renamed to packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
* @param parameterizedRoute The page's parameterized route
1717
* @returns A wrapped version of the function
1818
*/
19-
export function withSentryGetServerSideProps(
19+
export function wrapGetServerSidePropsWithSentry(
2020
origGetServerSideProps: GetServerSideProps,
2121
parameterizedRoute: string,
2222
): GetServerSideProps {
@@ -57,3 +57,8 @@ export function withSentryGetServerSideProps(
5757
}
5858
};
5959
}
60+
61+
/**
62+
* @deprecated Use `withSentryGetServerSideProps` instead.
63+
*/
64+
export const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry;

packages/nextjs/src/server/withSentryGetStaticProps.ts renamed to packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Props = { [key: string]: unknown };
1212
* @param parameterizedRoute The page's parameterized route
1313
* @returns A wrapped version of the function
1414
*/
15-
export function withSentryGetStaticProps(
15+
export function wrapGetStaticPropsWithSentry(
1616
origGetStaticProps: GetStaticProps<Props>,
1717
parameterizedRoute: string,
1818
): GetStaticProps<Props> {
@@ -31,3 +31,8 @@ export function withSentryGetStaticProps(
3131
});
3232
};
3333
}
34+
35+
/**
36+
* @deprecated Use `wrapGetStaticPropsWithSentry` instead.
37+
*/
38+
export const withSentryGetStaticProps = wrapGetStaticPropsWithSentry;

packages/nextjs/test/config/withSentry.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('withSentry', () => {
4545
res.send('Good dog, Maisey!');
4646
};
4747

48+
// eslint-disable-next-line deprecation/deprecation
4849
const wrappedHandlerNoError = withSentry(origHandlerNoError);
4950

5051
beforeEach(() => {

packages/nextjs/test/config/wrappers.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as SentryCore from '@sentry/core';
22
import * as SentryTracing from '@sentry/tracing';
33
import type { IncomingMessage, ServerResponse } from 'http';
44

5-
import { withSentryGetServerSideProps, withSentryServerSideGetInitialProps } from '../../src/server';
5+
import { wrapGetInitialPropsWithSentry, wrapGetServerSidePropsWithSentry } from '../../src/server';
66

77
const startTransactionSpy = jest.spyOn(SentryCore, 'startTransaction');
88

@@ -23,10 +23,10 @@ describe('data-fetching function wrappers', () => {
2323
jest.clearAllMocks();
2424
});
2525

26-
test('withSentryGetServerSideProps', async () => {
26+
test('wrapGetServerSidePropsWithSentry', async () => {
2727
const origFunction = jest.fn(async () => ({ props: {} }));
2828

29-
const wrappedOriginal = withSentryGetServerSideProps(origFunction, route);
29+
const wrappedOriginal = wrapGetServerSidePropsWithSentry(origFunction, route);
3030
await wrappedOriginal({ req, res } as any);
3131

3232
expect(startTransactionSpy).toHaveBeenCalledWith(
@@ -43,10 +43,10 @@ describe('data-fetching function wrappers', () => {
4343
);
4444
});
4545

46-
test('withSentryServerSideGetInitialProps', async () => {
46+
test('wrapGetInitialPropsWithSentry', async () => {
4747
const origFunction = jest.fn(async () => ({}));
4848

49-
const wrappedOriginal = withSentryServerSideGetInitialProps(origFunction);
49+
const wrappedOriginal = wrapGetInitialPropsWithSentry(origFunction);
5050
await wrappedOriginal({ req, res, pathname: route } as any);
5151

5252
expect(startTransactionSpy).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)