Skip to content

ref(nextjs): Rename wrapper functions #6788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 16, 2023
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/templates/apiWrapperTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const config = {
},
};

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

// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
// not include anything whose name matchs something we've explicitly exported above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ if ('middleware' in userApiModule && typeof userApiModule.middleware === 'functi
userProvidedDefaultHandler = userApiModule;
}

export const middleware = userProvidedNamedHandler ? Sentry.withSentryMiddleware(userProvidedNamedHandler) : undefined;
export default userProvidedDefaultHandler ? Sentry.withSentryMiddleware(userProvidedDefaultHandler) : undefined;
export const middleware = userProvidedNamedHandler
? Sentry.wrapMiddlewareWithSentry(userProvidedNamedHandler)
: undefined;
export default userProvidedDefaultHandler ? Sentry.wrapMiddlewareWithSentry(userProvidedDefaultHandler) : undefined;

// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
// not include anything whose name matchs something we've explicitly exported above.
Expand Down
12 changes: 6 additions & 6 deletions packages/nextjs/src/config/templates/pageWrapperTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ const origGetStaticProps = userPageModule.getStaticProps;
const origGetServerSideProps = userPageModule.getServerSideProps;

const getInitialPropsWrappers: Record<string, any> = {
'/_app': Sentry.withSentryServerSideAppGetInitialProps,
'/_document': Sentry.withSentryServerSideDocumentGetInitialProps,
'/_error': Sentry.withSentryServerSideErrorGetInitialProps,
'/_app': Sentry.wrapAppGetInitialPropsWithSentry,
'/_document': Sentry.wrapDocumentGetInitialPropsWithSentry,
'/_error': Sentry.wrapErrorGetInitialPropsWithSentry,
};

const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.withSentryServerSideGetInitialProps;
const getInitialPropsWrapper = getInitialPropsWrappers['__ROUTE__'] || Sentry.wrapGetInitialPropsWithSentry;

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

export const getStaticProps =
typeof origGetStaticProps === 'function'
? Sentry.withSentryGetStaticProps(origGetStaticProps, '__ROUTE__')
? Sentry.wrapGetStaticPropsWithSentry(origGetStaticProps, '__ROUTE__')
: undefined;
export const getServerSideProps =
typeof origGetServerSideProps === 'function'
? Sentry.withSentryGetServerSideProps(origGetServerSideProps, '__ROUTE__')
? Sentry.wrapGetServerSidePropsWithSentry(origGetServerSideProps, '__ROUTE__')
: undefined;

export default pageComponent;
Expand Down
10 changes: 8 additions & 2 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,11 @@ export function lastEventId(): string | undefined {
export { flush } from './utils/flush';

export * from '@sentry/core';
export { withSentryAPI } from './withSentryAPI';
export { withSentryMiddleware } from './withSentryMiddleware';

export {
// eslint-disable-next-line deprecation/deprecation
withSentryAPI,
wrapApiHandlerWithSentry,
} from './wrapApiHandlerWithSentry';

export { wrapMiddlewareWithSentry } from './wrapMiddlewareWithSentry';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';
/**
* Wraps a Next.js edge route handler with Sentry error and performance instrumentation.
*/
export function withSentryAPI<H extends EdgeRouteHandler>(
export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
handler: H,
parameterizedRoute: string,
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
Expand All @@ -21,9 +21,14 @@ export function withSentryAPI<H extends EdgeRouteHandler>(
? `handler (${parameterizedRoute})`
: `${req.method} ${parameterizedRoute}`,
spanOp: activeSpan ? 'function' : 'http.server',
mechanismFunctionName: 'withSentryAPI',
mechanismFunctionName: 'wrapApiHandlerWithSentry',
});

return await wrappedHandler.apply(this, args);
};
}

/**
* @deprecated Use `wrapApiHandlerWithSentry` instead.
*/
export const withSentryAPI = wrapApiHandlerWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';

/**
* Wraps Next.js middleware with Sentry error and performance instrumentation.
*
* @param middleware The middleware handler.
* @returns a wrapped middleware handler.
*/
export function withSentryMiddleware<H extends EdgeRouteHandler>(
export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
middleware: H,
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
return withEdgeWrapping(middleware, {
Expand Down
17 changes: 17 additions & 0 deletions packages/nextjs/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,26 @@ export declare function flush(timeout?: number | undefined): PromiseLike<boolean
export declare function lastEventId(): string | undefined;
export declare function getSentryRelease(fallback?: string): string | undefined;

/**
* @deprecated Use `wrapApiHandlerWithSentry` instead
*/
export declare function withSentryAPI<APIHandler extends (...args: any[]) => any>(
handler: APIHandler,
parameterizedRoute: string,
): (
...args: Parameters<APIHandler>
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;

/**
* Wraps a Next.js API handler with Sentry error and performance instrumentation.
*
* @param handler The handler exported from the API route file.
* @param parameterizedRoute The page's parameterized route.
* @returns The wrapped handler.
*/
export declare function wrapApiHandlerWithSentry<APIHandler extends (...args: any[]) => any>(
handler: APIHandler,
parameterizedRoute: string,
): (
...args: Parameters<APIHandler>
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;
48 changes: 41 additions & 7 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,44 @@ const deprecatedIsBuild = (): boolean => isBuild();
// eslint-disable-next-line deprecation/deprecation
export { deprecatedIsBuild as isBuild };

export { withSentryGetStaticProps } from './withSentryGetStaticProps';
export { withSentryServerSideGetInitialProps } from './withSentryServerSideGetInitialProps';
export { withSentryServerSideAppGetInitialProps } from './withSentryServerSideAppGetInitialProps';
export { withSentryServerSideDocumentGetInitialProps } from './withSentryServerSideDocumentGetInitialProps';
export { withSentryServerSideErrorGetInitialProps } from './withSentryServerSideErrorGetInitialProps';
export { withSentryGetServerSideProps } from './withSentryGetServerSideProps';
export { withSentry, withSentryAPI } from './withSentryAPI';
export {
// eslint-disable-next-line deprecation/deprecation
withSentryGetStaticProps,
wrapGetStaticPropsWithSentry,
} from './wrapGetStaticPropsWithSentry';

export {
// eslint-disable-next-line deprecation/deprecation
withSentryServerSideGetInitialProps,
wrapGetInitialPropsWithSentry,
} from './wrapGetInitialPropsWithSentry';

export {
// eslint-disable-next-line deprecation/deprecation
withSentryServerSideAppGetInitialProps,
wrapAppGetInitialPropsWithSentry,
} from './wrapAppGetInitialPropsWithSentry';
export {
// eslint-disable-next-line deprecation/deprecation
withSentryServerSideDocumentGetInitialProps,
wrapDocumentGetInitialPropsWithSentry,
} from './wrapDocumentGetInitialPropsWithSentry';
export {
// eslint-disable-next-line deprecation/deprecation
withSentryServerSideErrorGetInitialProps,
wrapErrorGetInitialPropsWithSentry,
} from './wrapErrorGetInitialPropsWithSentry';

export {
// eslint-disable-next-line deprecation/deprecation
withSentryGetServerSideProps,
wrapGetServerSidePropsWithSentry,
} from './wrapGetServerSidePropsWithSentry';

export {
// eslint-disable-next-line deprecation/deprecation
withSentry,
// eslint-disable-next-line deprecation/deprecation
withSentryAPI,
wrapApiHandlerWithSentry,
} from './wrapApiHandlerWithSentry';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { autoEndTransactionOnResponseEnd, finishTransaction, flushQueue } from '
* @param parameterizedRoute The page's route, passed in via the proxy loader
* @returns The wrapped handler
*/
export function withSentryAPI(
export function wrapApiHandlerWithSentry(
maybeWrappedHandler: NextApiHandler | WrappedNextApiHandler,
parameterizedRoute: string,
): WrappedNextApiHandler {
Expand All @@ -47,20 +47,28 @@ export function withSentryAPI(
);
}

// eslint-disable-next-line deprecation/deprecation
return withSentry(maybeWrappedHandler, parameterizedRoute);
}

/**
* Legacy function for manually wrapping API route handlers, now used as the innards of `withSentryAPI`.
* @deprecated Use `wrapApiHandlerWithSentry()` instead
*/
export const withSentryAPI = wrapApiHandlerWithSentry;

/**
* Legacy function for manually wrapping API route handlers, now used as the innards of `wrapApiHandlerWithSentry`.
*
* @param origHandler The user's original API route handler
* @param parameterizedRoute The route whose handler is being wrapped. Meant for internal use only.
* @returns A wrapped version of the handler
*
* @deprecated Use `wrapApiWithSentry()` instead
*/
export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: string): WrappedNextApiHandler {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
return async function sentryWrappedHandler(req: AugmentedNextApiRequest, res: AugmentedNextApiResponse) {
// We're now auto-wrapping API route handlers using `withSentryAPI` (which uses `withSentry` under the hood), but
// We're now auto-wrapping API route handlers using `wrapApiHandlerWithSentry` (which uses `withSentry` under the hood), but
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
// idempotent so that those cases don't break anything.
if (req.__withSentry_applied__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type AppGetInitialProps = typeof App['getInitialProps'];
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
if (isBuild()) {
return origAppGetInitialProps.apply(this, args);
Expand Down Expand Up @@ -72,3 +72,8 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A
}
};
}

/**
* @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DocumentGetInitialProps = typeof Document.getInitialProps;
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryServerSideDocumentGetInitialProps(
export function wrapDocumentGetInitialPropsWithSentry(
origDocumentGetInitialProps: DocumentGetInitialProps,
): DocumentGetInitialProps {
return async function (
Expand Down Expand Up @@ -47,3 +47,8 @@ export function withSentryServerSideDocumentGetInitialProps(
}
};
}

/**
* @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryServerSideErrorGetInitialProps(
export function wrapErrorGetInitialPropsWithSentry(
origErrorGetInitialProps: ErrorGetInitialProps,
): ErrorGetInitialProps {
return async function (this: unknown, ...args: Parameters<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
Expand Down Expand Up @@ -63,3 +63,8 @@ export function withSentryServerSideErrorGetInitialProps(
}
};
}

/**
* @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GetInitialProps = Required<NextPage>['getInitialProps'];
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInitialProps): GetInitialProps {
export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps {
return async function (this: unknown, ...args: Parameters<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
if (isBuild()) {
return origGetInitialProps.apply(this, args);
Expand Down Expand Up @@ -59,3 +59,8 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit
}
};
}

/**
* @deprecated Use `wrapGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryGetServerSideProps(
export function wrapGetServerSidePropsWithSentry(
origGetServerSideProps: GetServerSideProps,
parameterizedRoute: string,
): GetServerSideProps {
Expand Down Expand Up @@ -57,3 +57,8 @@ export function withSentryGetServerSideProps(
}
};
}

/**
* @deprecated Use `withSentryGetServerSideProps` instead.
*/
export const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Props = { [key: string]: unknown };
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryGetStaticProps(
export function wrapGetStaticPropsWithSentry(
origGetStaticProps: GetStaticProps<Props>,
parameterizedRoute: string,
): GetStaticProps<Props> {
Expand All @@ -31,3 +31,8 @@ export function withSentryGetStaticProps(
});
};
}

/**
* @deprecated Use `wrapGetStaticPropsWithSentry` instead.
*/
export const withSentryGetStaticProps = wrapGetStaticPropsWithSentry;
1 change: 1 addition & 0 deletions packages/nextjs/test/config/withSentry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('withSentry', () => {
res.send('Good dog, Maisey!');
};

// eslint-disable-next-line deprecation/deprecation
const wrappedHandlerNoError = withSentry(origHandlerNoError);

beforeEach(() => {
Expand Down
10 changes: 5 additions & 5 deletions packages/nextjs/test/config/wrappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as SentryCore from '@sentry/core';
import * as SentryTracing from '@sentry/tracing';
import type { IncomingMessage, ServerResponse } from 'http';

import { withSentryGetServerSideProps, withSentryServerSideGetInitialProps } from '../../src/server';
import { wrapGetInitialPropsWithSentry, wrapGetServerSidePropsWithSentry } from '../../src/server';

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

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

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

const wrappedOriginal = withSentryGetServerSideProps(origFunction, route);
const wrappedOriginal = wrapGetServerSidePropsWithSentry(origFunction, route);
await wrappedOriginal({ req, res } as any);

expect(startTransactionSpy).toHaveBeenCalledWith(
Expand All @@ -43,10 +43,10 @@ describe('data-fetching function wrappers', () => {
);
});

test('withSentryServerSideGetInitialProps', async () => {
test('wrapGetInitialPropsWithSentry', async () => {
const origFunction = jest.fn(async () => ({}));

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

expect(startTransactionSpy).toHaveBeenCalledWith(
Expand Down
Loading