From af019fd8b6d913c09148e616aeddd67f2a1f232b Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:13:33 +0000 Subject: [PATCH 01/15] Rename a bunch of files --- .../src/config/{withSentryConfig.ts => wrapConfigWithSentry.ts} | 0 .../nextjs/src/edge/{withSentryAPI.ts => wrapApiWithSentry.ts} | 0 .../edge/{withSentryMiddleware.ts => wrapMiddlewareWithSentry.ts} | 0 .../nextjs/src/server/{withSentryAPI.ts => wrapApiWithSentry.ts} | 0 ...eAppGetInitialProps.ts => wrapAppGetInitialPropsWithSentry.ts} | 0 ...etInitialProps.ts => wrapDocumentGetInitialPropsWithSentry.ts} | 0 ...orGetInitialProps.ts => wrapErrorGetInitialPropsWithSentry.ts} | 0 ...verSideGetInitialProps.ts => wrapGetInitialPropsWithSentry.ts} | 0 ...yGetServerSideProps.ts => wrapGetServerSidePropsWithSentry.ts} | 0 ...ithSentryGetStaticProps.ts => wrapGetStaticPropsWithSentry.ts} | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename packages/nextjs/src/config/{withSentryConfig.ts => wrapConfigWithSentry.ts} (100%) rename packages/nextjs/src/edge/{withSentryAPI.ts => wrapApiWithSentry.ts} (100%) rename packages/nextjs/src/edge/{withSentryMiddleware.ts => wrapMiddlewareWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryAPI.ts => wrapApiWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryServerSideAppGetInitialProps.ts => wrapAppGetInitialPropsWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryServerSideDocumentGetInitialProps.ts => wrapDocumentGetInitialPropsWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryServerSideErrorGetInitialProps.ts => wrapErrorGetInitialPropsWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryServerSideGetInitialProps.ts => wrapGetInitialPropsWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryGetServerSideProps.ts => wrapGetServerSidePropsWithSentry.ts} (100%) rename packages/nextjs/src/server/{withSentryGetStaticProps.ts => wrapGetStaticPropsWithSentry.ts} (100%) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/wrapConfigWithSentry.ts similarity index 100% rename from packages/nextjs/src/config/withSentryConfig.ts rename to packages/nextjs/src/config/wrapConfigWithSentry.ts diff --git a/packages/nextjs/src/edge/withSentryAPI.ts b/packages/nextjs/src/edge/wrapApiWithSentry.ts similarity index 100% rename from packages/nextjs/src/edge/withSentryAPI.ts rename to packages/nextjs/src/edge/wrapApiWithSentry.ts diff --git a/packages/nextjs/src/edge/withSentryMiddleware.ts b/packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts similarity index 100% rename from packages/nextjs/src/edge/withSentryMiddleware.ts rename to packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryAPI.ts b/packages/nextjs/src/server/wrapApiWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryAPI.ts rename to packages/nextjs/src/server/wrapApiWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryServerSideAppGetInitialProps.ts b/packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryServerSideAppGetInitialProps.ts rename to packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryServerSideDocumentGetInitialProps.ts b/packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryServerSideDocumentGetInitialProps.ts rename to packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryServerSideErrorGetInitialProps.ts b/packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryServerSideErrorGetInitialProps.ts rename to packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryServerSideGetInitialProps.ts b/packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryServerSideGetInitialProps.ts rename to packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryGetServerSideProps.ts b/packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryGetServerSideProps.ts rename to packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts diff --git a/packages/nextjs/src/server/withSentryGetStaticProps.ts b/packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/withSentryGetStaticProps.ts rename to packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts From a972cbc0307a0f7f841794478b687913a7b124e1 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:23:24 +0000 Subject: [PATCH 02/15] Rename files again --- .../edge/{wrapApiWithSentry.ts => wrapApiHandlerWithSentry.ts} | 0 .../server/{wrapApiWithSentry.ts => wrapApiHandlerWithSentry.ts} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/nextjs/src/edge/{wrapApiWithSentry.ts => wrapApiHandlerWithSentry.ts} (100%) rename packages/nextjs/src/server/{wrapApiWithSentry.ts => wrapApiHandlerWithSentry.ts} (100%) diff --git a/packages/nextjs/src/edge/wrapApiWithSentry.ts b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts similarity index 100% rename from packages/nextjs/src/edge/wrapApiWithSentry.ts rename to packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts diff --git a/packages/nextjs/src/server/wrapApiWithSentry.ts b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts similarity index 100% rename from packages/nextjs/src/server/wrapApiWithSentry.ts rename to packages/nextjs/src/server/wrapApiHandlerWithSentry.ts From e5db878622e918127fb6dee501621c74a057c7a9 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:31:51 +0000 Subject: [PATCH 03/15] Rename and deprecate api route handler wrappers --- packages/nextjs/src/edge/index.ts | 4 ++-- .../nextjs/src/edge/wrapApiHandlerWithSentry.ts | 4 +++- packages/nextjs/src/index.types.ts | 17 +++++++++++++++++ packages/nextjs/src/server/index.ts | 4 +++- .../src/server/wrapApiHandlerWithSentry.ts | 10 +++++++++- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/src/edge/index.ts b/packages/nextjs/src/edge/index.ts index b0ad8ec23901..921afa0ad5db 100644 --- a/packages/nextjs/src/edge/index.ts +++ b/packages/nextjs/src/edge/index.ts @@ -131,5 +131,5 @@ export function lastEventId(): string | undefined { export { flush } from './utils/flush'; export * from '@sentry/core'; -export { withSentryAPI } from './withSentryAPI'; -export { withSentryMiddleware } from './withSentryMiddleware'; +export { withSentryAPI, wrapApiHandlerWithSentry } from './wrapApiHandlerWithSentry'; +export { withSentryMiddleware } from './wrapMiddlewareWithSentry'; diff --git a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts index d18deac1c3c4..e89f955f5103 100644 --- a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts @@ -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( +export function wrapApiHandlerWithSentry( handler: H, parameterizedRoute: string, ): (...params: Parameters) => Promise> { @@ -27,3 +27,5 @@ export function withSentryAPI( return await wrappedHandler.apply(this, args); }; } + +export const withSentryAPI = wrapApiHandlerWithSentry; diff --git a/packages/nextjs/src/index.types.ts b/packages/nextjs/src/index.types.ts index 9f82be80a67c..fbc3bec2d71b 100644 --- a/packages/nextjs/src/index.types.ts +++ b/packages/nextjs/src/index.types.ts @@ -30,9 +30,26 @@ export declare function flush(timeout?: number | undefined): PromiseLike any>( handler: APIHandler, parameterizedRoute: string, ): ( ...args: Parameters ) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * 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 any>( + handler: APIHandler, + parameterizedRoute: string, +): ( + ...args: Parameters +) => ReturnType extends Promise ? ReturnType : Promise>; diff --git a/packages/nextjs/src/server/index.ts b/packages/nextjs/src/server/index.ts index 340f4100d611..86832a517c99 100644 --- a/packages/nextjs/src/server/index.ts +++ b/packages/nextjs/src/server/index.ts @@ -154,4 +154,6 @@ export { withSentryServerSideAppGetInitialProps } from './withSentryServerSideAp export { withSentryServerSideDocumentGetInitialProps } from './withSentryServerSideDocumentGetInitialProps'; export { withSentryServerSideErrorGetInitialProps } from './withSentryServerSideErrorGetInitialProps'; export { withSentryGetServerSideProps } from './withSentryGetServerSideProps'; -export { withSentry, withSentryAPI } from './withSentryAPI'; + +// eslint-disable-next-line deprecation/deprecation +export { withSentry, withSentryAPI, wrapApiWithSentry } from './wrapApiHandlerWithSentry'; diff --git a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts index 5d5180dd741e..9a29c6fd5c68 100644 --- a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts @@ -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 wrapApiWithSentry( maybeWrappedHandler: NextApiHandler | WrappedNextApiHandler, parameterizedRoute: string, ): WrappedNextApiHandler { @@ -47,15 +47,23 @@ export function withSentryAPI( ); } + // eslint-disable-next-line deprecation/deprecation return withSentry(maybeWrappedHandler, parameterizedRoute); } +/** + * @deprecated Use `wrapApiWithSentry()` instead + */ +export const withSentryAPI = wrapApiWithSentry; + /** * Legacy function for manually wrapping API route handlers, now used as the innards of `withSentryAPI`. * * @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 From eb87f3e86d5b1d984960c1b2353e203e44d61d2d Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:32:00 +0000 Subject: [PATCH 04/15] Rename middleware wrapper --- packages/nextjs/src/edge/index.ts | 2 +- packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/edge/index.ts b/packages/nextjs/src/edge/index.ts index 921afa0ad5db..beb35e8da53f 100644 --- a/packages/nextjs/src/edge/index.ts +++ b/packages/nextjs/src/edge/index.ts @@ -132,4 +132,4 @@ export { flush } from './utils/flush'; export * from '@sentry/core'; export { withSentryAPI, wrapApiHandlerWithSentry } from './wrapApiHandlerWithSentry'; -export { withSentryMiddleware } from './wrapMiddlewareWithSentry'; +export { wrapMiddlewareWithSentry } from './wrapMiddlewareWithSentry'; diff --git a/packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts b/packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts index 74dd6619ed62..cb535c41b28d 100644 --- a/packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts +++ b/packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts @@ -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( +export function wrapMiddlewareWithSentry( middleware: H, ): (...params: Parameters) => Promise> { return withEdgeWrapping(middleware, { From 4a8fcb184e056b9cb856ad9740cbdf07a6968839 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:45:26 +0000 Subject: [PATCH 05/15] Update data fetcher exports --- packages/nextjs/src/server/index.ts | 50 +++++++++++++++---- .../wrapAppGetInitialPropsWithSentry.ts | 7 ++- .../wrapDocumentGetInitialPropsWithSentry.ts | 7 ++- .../wrapErrorGetInitialPropsWithSentry.ts | 7 ++- .../server/wrapGetInitialPropsWithSentry.ts | 7 ++- .../wrapGetServerSidePropsWithSentry.ts | 7 ++- .../server/wrapGetStaticPropsWithSentry.ts | 7 ++- 7 files changed, 77 insertions(+), 15 deletions(-) diff --git a/packages/nextjs/src/server/index.ts b/packages/nextjs/src/server/index.ts index 86832a517c99..7e447f5a4e0c 100644 --- a/packages/nextjs/src/server/index.ts +++ b/packages/nextjs/src/server/index.ts @@ -148,12 +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'; - -// eslint-disable-next-line deprecation/deprecation -export { withSentry, withSentryAPI, wrapApiWithSentry } from './wrapApiHandlerWithSentry'; +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, + wrapApiWithSentry, +} from './wrapApiHandlerWithSentry'; diff --git a/packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts index 1508661db73e..d21ecfc0a44e 100644 --- a/packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapAppGetInitialPropsWithSentry.ts @@ -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): ReturnType { if (isBuild()) { return origAppGetInitialProps.apply(this, args); @@ -72,3 +72,8 @@ export function withSentryServerSideAppGetInitialProps(origAppGetInitialProps: A } }; } + +/** + * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts b/packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts index 95c45da095aa..7052250275d7 100644 --- a/packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapDocumentGetInitialPropsWithSentry.ts @@ -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 ( @@ -47,3 +47,8 @@ export function withSentryServerSideDocumentGetInitialProps( } }; } + +/** + * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts index 8208c46762a9..54f52ef8dfc9 100644 --- a/packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapErrorGetInitialPropsWithSentry.ts @@ -20,7 +20,7 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise; * @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): ReturnType { @@ -63,3 +63,8 @@ export function withSentryServerSideErrorGetInitialProps( } }; } + +/** + * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts index 9cf7cef3db03..aed6829a6cb4 100644 --- a/packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapGetInitialPropsWithSentry.ts @@ -18,7 +18,7 @@ type GetInitialProps = Required['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): Promise> { if (isBuild()) { return origGetInitialProps.apply(this, args); @@ -59,3 +59,8 @@ export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInit } }; } + +/** + * @deprecated Use `wrapGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts b/packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts index 1f41e91ab3e9..dc3640b6dfe9 100644 --- a/packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapGetServerSidePropsWithSentry.ts @@ -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 { @@ -57,3 +57,8 @@ export function withSentryGetServerSideProps( } }; } + +/** + * @deprecated Use `withSentryGetServerSideProps` instead. + */ +export const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry; diff --git a/packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts b/packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts index 96e6f2b81db9..727e4a69c224 100644 --- a/packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts +++ b/packages/nextjs/src/server/wrapGetStaticPropsWithSentry.ts @@ -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, parameterizedRoute: string, ): GetStaticProps { @@ -31,3 +31,8 @@ export function withSentryGetStaticProps( }); }; } + +/** + * @deprecated Use `wrapGetStaticPropsWithSentry` instead. + */ +export const withSentryGetStaticProps = wrapGetStaticPropsWithSentry; From 8a2a0b0f127060a8a2f3edfc4b43fe0f6094e34e Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:46:18 +0000 Subject: [PATCH 06/15] Add deprecation note --- packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts index e89f955f5103..97cbfd6179b9 100644 --- a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts @@ -28,4 +28,7 @@ export function wrapApiHandlerWithSentry( }; } +/** + * @deprecated Use `wrapApiHandlerWithSentry` instead. + */ export const withSentryAPI = wrapApiHandlerWithSentry; From 1ba4d4570b4c9e80037caad8cd3215a0b1cd908b Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:47:06 +0000 Subject: [PATCH 07/15] Eslint ignore --- packages/nextjs/src/edge/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/edge/index.ts b/packages/nextjs/src/edge/index.ts index beb35e8da53f..712b0f61a81f 100644 --- a/packages/nextjs/src/edge/index.ts +++ b/packages/nextjs/src/edge/index.ts @@ -131,5 +131,11 @@ export function lastEventId(): string | undefined { export { flush } from './utils/flush'; export * from '@sentry/core'; -export { withSentryAPI, wrapApiHandlerWithSentry } from './wrapApiHandlerWithSentry'; + +export { + // eslint-disable-next-line deprecation/deprecation + withSentryAPI, + wrapApiHandlerWithSentry, +} from './wrapApiHandlerWithSentry'; + export { wrapMiddlewareWithSentry } from './wrapMiddlewareWithSentry'; From f264709c726671e0403a7086fca66d9ed11c821d Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:49:18 +0000 Subject: [PATCH 08/15] Rerename withSentryConfig --- .../src/config/{wrapConfigWithSentry.ts => withSentryConfig.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/nextjs/src/config/{wrapConfigWithSentry.ts => withSentryConfig.ts} (100%) diff --git a/packages/nextjs/src/config/wrapConfigWithSentry.ts b/packages/nextjs/src/config/withSentryConfig.ts similarity index 100% rename from packages/nextjs/src/config/wrapConfigWithSentry.ts rename to packages/nextjs/src/config/withSentryConfig.ts From e4c66f4e06b0a287a45cf44cc598cc840ed80441 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:56:55 +0000 Subject: [PATCH 09/15] Update templates --- .../src/config/templates/apiWrapperTemplate.ts | 2 +- .../config/templates/middlewareWrapperTemplate.ts | 6 ++++-- .../src/config/templates/pageWrapperTemplate.ts | 12 ++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/nextjs/src/config/templates/apiWrapperTemplate.ts b/packages/nextjs/src/config/templates/apiWrapperTemplate.ts index 969d433f126f..91cf5ef1e0c6 100644 --- a/packages/nextjs/src/config/templates/apiWrapperTemplate.ts +++ b/packages/nextjs/src/config/templates/apiWrapperTemplate.ts @@ -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. diff --git a/packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts b/packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts index 373f63646933..0c833023cffe 100644 --- a/packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts +++ b/packages/nextjs/src/config/templates/middlewareWrapperTemplate.ts @@ -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. diff --git a/packages/nextjs/src/config/templates/pageWrapperTemplate.ts b/packages/nextjs/src/config/templates/pageWrapperTemplate.ts index 955e920552d1..12b8429bc7d4 100644 --- a/packages/nextjs/src/config/templates/pageWrapperTemplate.ts +++ b/packages/nextjs/src/config/templates/pageWrapperTemplate.ts @@ -28,12 +28,12 @@ const origGetStaticProps = userPageModule.getStaticProps; const origGetServerSideProps = userPageModule.getServerSideProps; const getInitialPropsWrappers: Record = { - '/_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']; @@ -41,11 +41,11 @@ if (typeof origGetInitialProps === 'function') { 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; From eac250907fc7328eb0ad3d7bc34cfb8fa76c06cb Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:57:06 +0000 Subject: [PATCH 10/15] Update occurences --- packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts | 2 +- packages/nextjs/src/server/wrapApiHandlerWithSentry.ts | 4 ++-- packages/nextjs/test/edge/withSentryAPI.test.ts | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts index 97cbfd6179b9..7e87c6a5e607 100644 --- a/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts @@ -21,7 +21,7 @@ export function wrapApiHandlerWithSentry( ? `handler (${parameterizedRoute})` : `${req.method} ${parameterizedRoute}`, spanOp: activeSpan ? 'function' : 'http.server', - mechanismFunctionName: 'withSentryAPI', + mechanismFunctionName: 'wrapApiHandlerWithSentry', }); return await wrappedHandler.apply(this, args); diff --git a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts index 9a29c6fd5c68..2c1a1567dffa 100644 --- a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts @@ -57,7 +57,7 @@ export function wrapApiWithSentry( export const withSentryAPI = wrapApiWithSentry; /** - * Legacy function for manually wrapping API route handlers, now used as the innards of `withSentryAPI`. + * 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. @@ -68,7 +68,7 @@ export const withSentryAPI = wrapApiWithSentry; 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__) { diff --git a/packages/nextjs/test/edge/withSentryAPI.test.ts b/packages/nextjs/test/edge/withSentryAPI.test.ts index aacef0fa3f79..1981220b50f0 100644 --- a/packages/nextjs/test/edge/withSentryAPI.test.ts +++ b/packages/nextjs/test/edge/withSentryAPI.test.ts @@ -1,7 +1,7 @@ import * as coreSdk from '@sentry/core'; import * as sentryTracing from '@sentry/tracing'; -import { withSentryAPI } from '../../src/edge'; +import { wrapApiHandlerWithSentry } from '../../src/edge'; // @ts-ignore Request does not exist on type Global const origRequest = global.Request; @@ -35,14 +35,14 @@ beforeEach(() => { jest.spyOn(sentryTracing, 'hasTracingEnabled').mockImplementation(() => true); }); -describe('withSentryAPI', () => { +describe('wrapApiHandlerWithSentry', () => { it('should return a function that starts a transaction with the correct name when there is no active transaction and a request is being passed', async () => { const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction'); const origFunctionReturnValue = new Response(); const origFunction = jest.fn(_req => origFunctionReturnValue); - const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); + const wrappedFunction = wrapApiHandlerWithSentry(origFunction, '/user/[userId]/post/[postId]'); const request = new Request('https://sentry.io/'); await wrappedFunction(request); @@ -63,7 +63,7 @@ describe('withSentryAPI', () => { const origFunctionReturnValue = new Response(); const origFunction = jest.fn(() => origFunctionReturnValue); - const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); + const wrappedFunction = wrapApiHandlerWithSentry(origFunction, '/user/[userId]/post/[postId]'); await wrappedFunction(); expect(startTransactionSpy).not.toHaveBeenCalled(); @@ -78,7 +78,7 @@ describe('withSentryAPI', () => { const origFunctionReturnValue = new Response(); const origFunction = jest.fn(() => origFunctionReturnValue); - const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); + const wrappedFunction = wrapApiHandlerWithSentry(origFunction, '/user/[userId]/post/[postId]'); await wrappedFunction(); expect(startChildSpy).toHaveBeenCalledTimes(1); From 24ca6d1af7eadc78c7c9014e5b13acb61c266b9c Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 12:58:41 +0000 Subject: [PATCH 11/15] Rename integration test folder --- .../unwrapped/[...pathParts].ts | 0 .../unwrapped/[animal].ts | 0 .../unwrapped/cjsExport.ts | 0 .../unwrapped/noParams.ts | 0 .../wrapped/[...pathParts].ts | 0 .../wrapped/[animal].ts | 0 .../wrapped/cjsExport.ts | 0 .../wrapped/noParams.ts | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/unwrapped/[...pathParts].ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/unwrapped/[animal].ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/unwrapped/cjsExport.ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/unwrapped/noParams.ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/wrapped/[...pathParts].ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/wrapped/[animal].ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/wrapped/cjsExport.ts (100%) rename packages/nextjs/test/integration/pages/api/{withSentryAPI => wrapApiHandlerWithSentry}/wrapped/noParams.ts (100%) diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/[...pathParts].ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/[...pathParts].ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/[...pathParts].ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/[...pathParts].ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/[animal].ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/[animal].ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/[animal].ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/[animal].ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/cjsExport.ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/cjsExport.ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/cjsExport.ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/cjsExport.ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/noParams.ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/noParams.ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/unwrapped/noParams.ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/unwrapped/noParams.ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/[...pathParts].ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/[...pathParts].ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/[...pathParts].ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/[...pathParts].ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/[animal].ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/[animal].ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/[animal].ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/[animal].ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/cjsExport.ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/cjsExport.ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/cjsExport.ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/cjsExport.ts diff --git a/packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/noParams.ts b/packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/noParams.ts similarity index 100% rename from packages/nextjs/test/integration/pages/api/withSentryAPI/wrapped/noParams.ts rename to packages/nextjs/test/integration/pages/api/wrapApiHandlerWithSentry/wrapped/noParams.ts From acfdbde710627270c3fe1fb5dc43e95acb31bccf Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 13:01:07 +0000 Subject: [PATCH 12/15] Replace occurences --- packages/nextjs/test/config/withSentry.test.ts | 1 + packages/nextjs/test/config/wrappers.test.ts | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/test/config/withSentry.test.ts b/packages/nextjs/test/config/withSentry.test.ts index 6cbf4ac02c09..75709d3f0bea 100644 --- a/packages/nextjs/test/config/withSentry.test.ts +++ b/packages/nextjs/test/config/withSentry.test.ts @@ -45,6 +45,7 @@ describe('withSentry', () => { res.send('Good dog, Maisey!'); }; + // eslint-disable-next-line deprecation/deprecation const wrappedHandlerNoError = withSentry(origHandlerNoError); beforeEach(() => { diff --git a/packages/nextjs/test/config/wrappers.test.ts b/packages/nextjs/test/config/wrappers.test.ts index 8edf2de57567..4824d6dc1573 100644 --- a/packages/nextjs/test/config/wrappers.test.ts +++ b/packages/nextjs/test/config/wrappers.test.ts @@ -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'); @@ -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( @@ -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( From 6d947ba7f407249b7095a42819d298eea96ff415 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 13:06:43 +0000 Subject: [PATCH 13/15] Renam api handler function --- packages/nextjs/src/server/index.ts | 2 +- packages/nextjs/src/server/wrapApiHandlerWithSentry.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nextjs/src/server/index.ts b/packages/nextjs/src/server/index.ts index 7e447f5a4e0c..8ab42f9aa829 100644 --- a/packages/nextjs/src/server/index.ts +++ b/packages/nextjs/src/server/index.ts @@ -187,5 +187,5 @@ export { withSentry, // eslint-disable-next-line deprecation/deprecation withSentryAPI, - wrapApiWithSentry, + wrapApiHandlerWithSentry, } from './wrapApiHandlerWithSentry'; diff --git a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts index 2c1a1567dffa..44cad73e6e81 100644 --- a/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/server/wrapApiHandlerWithSentry.ts @@ -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 wrapApiWithSentry( +export function wrapApiHandlerWithSentry( maybeWrappedHandler: NextApiHandler | WrappedNextApiHandler, parameterizedRoute: string, ): WrappedNextApiHandler { @@ -52,9 +52,9 @@ export function wrapApiWithSentry( } /** - * @deprecated Use `wrapApiWithSentry()` instead + * @deprecated Use `wrapApiHandlerWithSentry()` instead */ -export const withSentryAPI = wrapApiWithSentry; +export const withSentryAPI = wrapApiHandlerWithSentry; /** * Legacy function for manually wrapping API route handlers, now used as the innards of `wrapApiHandlerWithSentry`. From f2702af78c331312f88471c1619eda89e060c6fd Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 13:07:55 +0000 Subject: [PATCH 14/15] Fix integration tests --- .../test/server/cjsApiEndpoints.js | 4 +-- .../test/server/tracingWithSentryAPI.js | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/nextjs/test/integration/test/server/cjsApiEndpoints.js b/packages/nextjs/test/integration/test/server/cjsApiEndpoints.js index e154599fcbd7..3b25a405589a 100644 --- a/packages/nextjs/test/integration/test/server/cjsApiEndpoints.js +++ b/packages/nextjs/test/integration/test/server/cjsApiEndpoints.js @@ -4,7 +4,7 @@ const { sleep } = require('../utils/common'); const { getAsync, interceptTracingRequest } = require('../utils/server'); module.exports = async ({ url: urlBase, argv }) => { - const unwrappedRoute = '/api/withSentryAPI/unwrapped/cjsExport'; + const unwrappedRoute = '/api/wrapApiHandlerWithSentry/unwrapped/cjsExport'; const interceptedUnwrappedRequest = interceptTracingRequest( { contexts: { @@ -26,7 +26,7 @@ module.exports = async ({ url: urlBase, argv }) => { const responseUnwrapped = await getAsync(`${urlBase}${unwrappedRoute}`); assert.equal(responseUnwrapped, '{"success":true}'); - const wrappedRoute = '/api/withSentryAPI/wrapped/cjsExport'; + const wrappedRoute = '/api/wrapApiHandlerWithSentry/wrapped/cjsExport'; const interceptedWrappedRequest = interceptTracingRequest( { contexts: { diff --git a/packages/nextjs/test/integration/test/server/tracingWithSentryAPI.js b/packages/nextjs/test/integration/test/server/tracingWithSentryAPI.js index a9ceebc835c6..a630e5b97e00 100644 --- a/packages/nextjs/test/integration/test/server/tracingWithSentryAPI.js +++ b/packages/nextjs/test/integration/test/server/tracingWithSentryAPI.js @@ -6,12 +6,27 @@ const { getAsync, interceptTracingRequest } = require('../utils/server'); module.exports = async ({ url: urlBase, argv }) => { const urls = { // testName: [url, route] - unwrappedNoParamURL: [`/api/withSentryAPI/unwrapped/noParams`, '/api/withSentryAPI/unwrapped/noParams'], - unwrappedDynamicURL: [`/api/withSentryAPI/unwrapped/dog`, '/api/withSentryAPI/unwrapped/[animal]'], - unwrappedCatchAllURL: [`/api/withSentryAPI/unwrapped/dog/facts`, '/api/withSentryAPI/unwrapped/[...pathParts]'], - wrappedNoParamURL: [`/api/withSentryAPI/wrapped/noParams`, '/api/withSentryAPI/wrapped/noParams'], - wrappedDynamicURL: [`/api/withSentryAPI/wrapped/dog`, '/api/withSentryAPI/wrapped/[animal]'], - wrappedCatchAllURL: [`/api/withSentryAPI/wrapped/dog/facts`, '/api/withSentryAPI/wrapped/[...pathParts]'], + unwrappedNoParamURL: [ + `/api/wrapApiHandlerWithSentry/unwrapped/noParams`, + '/api/wrapApiHandlerWithSentry/unwrapped/noParams', + ], + unwrappedDynamicURL: [ + `/api/wrapApiHandlerWithSentry/unwrapped/dog`, + '/api/wrapApiHandlerWithSentry/unwrapped/[animal]', + ], + unwrappedCatchAllURL: [ + `/api/wrapApiHandlerWithSentry/unwrapped/dog/facts`, + '/api/wrapApiHandlerWithSentry/unwrapped/[...pathParts]', + ], + wrappedNoParamURL: [ + `/api/wrapApiHandlerWithSentry/wrapped/noParams`, + '/api/wrapApiHandlerWithSentry/wrapped/noParams', + ], + wrappedDynamicURL: [`/api/wrapApiHandlerWithSentry/wrapped/dog`, '/api/wrapApiHandlerWithSentry/wrapped/[animal]'], + wrappedCatchAllURL: [ + `/api/wrapApiHandlerWithSentry/wrapped/dog/facts`, + '/api/wrapApiHandlerWithSentry/wrapped/[...pathParts]', + ], }; const interceptedRequests = {}; From 2c59d79b7e5c6ea678e8570cae09772efc463de6 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 16 Jan 2023 13:38:23 +0000 Subject: [PATCH 15/15] fix(nextjs): Export isomorphic data fetching wrappers from client SDK --- packages/nextjs/src/client/index.ts | 22 ++++++ .../wrapAppGetInitialPropsWithSentry.ts | 18 +++++ .../wrapDocumentGetInitialPropsWithSentry.ts | 23 +++++++ .../wrapErrorGetInitialPropsWithSentry.ts | 21 ++++++ .../client/wrapGetInitialPropsWithSentry.ts | 18 +++++ packages/nextjs/src/index.types.ts | 68 +++++++++++++++++++ 6 files changed, 170 insertions(+) create mode 100644 packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts create mode 100644 packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts create mode 100644 packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts create mode 100644 packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts diff --git a/packages/nextjs/src/client/index.ts b/packages/nextjs/src/client/index.ts index 3ea78374290d..fbd9e1eaefba 100644 --- a/packages/nextjs/src/client/index.ts +++ b/packages/nextjs/src/client/index.ts @@ -92,3 +92,25 @@ function addClientIntegrations(options: BrowserOptions): void { options.integrations = integrations; } + +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'; diff --git a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..205650d41dac --- /dev/null +++ b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts @@ -0,0 +1,18 @@ +import type App from 'next/app'; + +type AppGetInitialProps = typeof App['getInitialProps']; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps { + return async function (this: unknown, ...args: Parameters): ReturnType { + return await origAppGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..c68c2a266df1 --- /dev/null +++ b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts @@ -0,0 +1,23 @@ +import type Document from 'next/document'; + +type DocumentGetInitialProps = typeof Document.getInitialProps; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapDocumentGetInitialPropsWithSentry( + origDocumentGetInitialProps: DocumentGetInitialProps, +): DocumentGetInitialProps { + return async function ( + this: unknown, + ...args: Parameters + ): ReturnType { + return await origDocumentGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..e018fb47246d --- /dev/null +++ b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts @@ -0,0 +1,21 @@ +import type { NextPageContext } from 'next'; +import type { ErrorProps } from 'next/error'; + +type ErrorGetInitialProps = (context: NextPageContext) => Promise; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapErrorGetInitialPropsWithSentry( + origErrorGetInitialProps: ErrorGetInitialProps, +): ErrorGetInitialProps { + return async function (this: unknown, ...args: Parameters): ReturnType { + return await origErrorGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts new file mode 100644 index 000000000000..f29561a4f333 --- /dev/null +++ b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts @@ -0,0 +1,18 @@ +import type { NextPage } from 'next'; + +type GetInitialProps = Required['getInitialProps']; + +/** + * A passthrough function in case this function is used on the clientside. We need to make the returned function async + * so we are consistent with the serverside implementation. + */ +export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps { + return async function (this: unknown, ...args: Parameters): Promise> { + return origGetInitialProps.apply(this, args); + }; +} + +/** + * @deprecated Use `wrapGetInitialPropsWithSentry` instead. + */ +export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry; diff --git a/packages/nextjs/src/index.types.ts b/packages/nextjs/src/index.types.ts index fbc3bec2d71b..28b6314c5225 100644 --- a/packages/nextjs/src/index.types.ts +++ b/packages/nextjs/src/index.types.ts @@ -53,3 +53,71 @@ export declare function wrapApiHandlerWithSentry ) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_app` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapAppGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideAppGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_document` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapDocumentGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideDocumentGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getInitialProps` function of a custom `_error` page with Sentry error and performance instrumentation. + * + * @param getInitialProps The `getInitialProps` function + * @returns A wrapped version of the function + */ +export declare function wrapErrorGetInitialPropsWithSentry any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead. + */ +export declare function withSentryServerSideErrorGetInitialProps any>( + getInitialProps: F, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>;