From 29ddcd596294471e63f3160294f057d330df5b54 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 22 Feb 2023 16:39:25 +0000 Subject: [PATCH 1/2] fix(nextjs): Export serverside data-fetcher wrappers from client --- packages/nextjs/src/client/index.ts | 14 ++++++++++++++ .../client/wrapAppGetInitialPropsWithSentry.ts | 11 +++-------- .../wrapDocumentGetInitialPropsWithSentry.ts | 11 +++-------- .../client/wrapErrorGetInitialPropsWithSentry.ts | 13 +++---------- .../src/client/wrapGetInitialPropsWithSentry.ts | 11 +++-------- .../client/wrapGetServerSidePropsWithSentry.ts | 13 +++++++++++++ .../src/client/wrapGetStaticPropsWithSentry.ts | 15 +++++++++++++++ 7 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts create mode 100644 packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts diff --git a/packages/nextjs/src/client/index.ts b/packages/nextjs/src/client/index.ts index 27eb57858e40..354bb96b1f27 100644 --- a/packages/nextjs/src/client/index.ts +++ b/packages/nextjs/src/client/index.ts @@ -123,15 +123,29 @@ export { 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 + withSentryGetStaticProps, + wrapGetStaticPropsWithSentry, +} from './wrapGetStaticPropsWithSentry'; + export { wrapAppDirComponentWithSentry } from './wrapAppDirComponentWithSentry'; diff --git a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts index 41a24d57c5c2..629a8f70e951 100644 --- a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts @@ -3,15 +3,10 @@ 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. + * A passthrough function in case this function is used on the clientside. */ -export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps { - return new Proxy(origAppGetInitialProps, { - apply: async (wrappingTarget, thisArg, args: Parameters) => { - return await wrappingTarget.apply(thisArg, args); - }, - }); +export function wrapAppGetInitialPropsWithSentry(appGetInitialProps: AppGetInitialProps): AppGetInitialProps { + return appGetInitialProps; } /** diff --git a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts index 0af40a1f3f84..a35774bab7af 100644 --- a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts @@ -3,17 +3,12 @@ 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. + * A passthrough function in case this function is used on the clientside. */ export function wrapDocumentGetInitialPropsWithSentry( - origDocumentGetInitialProps: DocumentGetInitialProps, + documentGetInitialProps: DocumentGetInitialProps, ): DocumentGetInitialProps { - return new Proxy(origDocumentGetInitialProps, { - apply: async (wrappingTarget, thisArg, args: Parameters) => { - return await wrappingTarget.apply(thisArg, args); - }, - }); + return documentGetInitialProps; } /** diff --git a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts index 605efa58eff9..adffd66fb780 100644 --- a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts @@ -4,17 +4,10 @@ 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. + * A passthrough function in case this function is used on the clientside. */ -export function wrapErrorGetInitialPropsWithSentry( - origErrorGetInitialProps: ErrorGetInitialProps, -): ErrorGetInitialProps { - return new Proxy(origErrorGetInitialProps, { - apply: async (wrappingTarget, thisArg, args: Parameters) => { - return await wrappingTarget.apply(thisArg, args); - }, - }); +export function wrapErrorGetInitialPropsWithSentry(errorGetInitialProps: ErrorGetInitialProps): ErrorGetInitialProps { + return errorGetInitialProps; } /** diff --git a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts index 1fbbd8707063..d5fa5336acf5 100644 --- a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts @@ -3,15 +3,10 @@ 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. + * A passthrough function in case this function is used on the clientside. */ -export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps { - return new Proxy(origGetInitialProps, { - apply: async (wrappingTarget, thisArg, args: Parameters) => { - return await wrappingTarget.apply(thisArg, args); - }, - }); +export function wrapGetInitialPropsWithSentry(getInitialProps: GetInitialProps): GetInitialProps { + return getInitialProps; } /** diff --git a/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts b/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts new file mode 100644 index 000000000000..65de7ad5e46e --- /dev/null +++ b/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts @@ -0,0 +1,13 @@ +import type { GetServerSideProps } from 'next'; + +/** + * A passthrough function in case this function is used on the clientside. + */ +export function wrapGetServerSidePropsWithSentry(getServerSideProps: GetServerSideProps): GetServerSideProps { + return getServerSideProps; +} + +/** + * @deprecated Use `withSentryGetServerSideProps` instead. + */ +export const withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry; diff --git a/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts new file mode 100644 index 000000000000..f511ecf9f299 --- /dev/null +++ b/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts @@ -0,0 +1,15 @@ +import type { GetStaticProps } from 'next'; + +type Props = { [key: string]: unknown }; + +/** + * A passthrough function in case this function is used on the clientside. + */ +export function wrapGetStaticPropsWithSentry(getStaticProps: GetStaticProps): GetStaticProps { + return getStaticProps; +} + +/** + * @deprecated Use `wrapGetStaticPropsWithSentry` instead. + */ +export const withSentryGetStaticProps = wrapGetStaticPropsWithSentry; From eb481ed7cf7c14b9633ba4ba86d03286259cac99 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 22 Feb 2023 21:22:16 +0000 Subject: [PATCH 2/2] Don't mess with return types --- .../wrapAppGetInitialPropsWithSentry.ts | 11 +++-- .../wrapDocumentGetInitialPropsWithSentry.ts | 11 +++-- .../wrapErrorGetInitialPropsWithSentry.ts | 13 ++++-- .../client/wrapGetInitialPropsWithSentry.ts | 11 +++-- .../wrapGetServerSidePropsWithSentry.ts | 11 +++-- .../client/wrapGetStaticPropsWithSentry.ts | 11 +++-- packages/nextjs/src/index.types.ts | 40 +++++++++++++++++++ 7 files changed, 90 insertions(+), 18 deletions(-) diff --git a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts index 629a8f70e951..41a24d57c5c2 100644 --- a/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts @@ -3,10 +3,15 @@ import type App from 'next/app'; type AppGetInitialProps = typeof App['getInitialProps']; /** - * A passthrough function in case this function is used on the clientside. + * 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(appGetInitialProps: AppGetInitialProps): AppGetInitialProps { - return appGetInitialProps; +export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps { + return new Proxy(origAppGetInitialProps, { + apply: async (wrappingTarget, thisArg, args: Parameters) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts index a35774bab7af..0af40a1f3f84 100644 --- a/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts @@ -3,12 +3,17 @@ import type Document from 'next/document'; type DocumentGetInitialProps = typeof Document.getInitialProps; /** - * A passthrough function in case this function is used on the clientside. + * 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( - documentGetInitialProps: DocumentGetInitialProps, + origDocumentGetInitialProps: DocumentGetInitialProps, ): DocumentGetInitialProps { - return documentGetInitialProps; + return new Proxy(origDocumentGetInitialProps, { + apply: async (wrappingTarget, thisArg, args: Parameters) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts index adffd66fb780..605efa58eff9 100644 --- a/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts @@ -4,10 +4,17 @@ import type { ErrorProps } from 'next/error'; type ErrorGetInitialProps = (context: NextPageContext) => Promise; /** - * A passthrough function in case this function is used on the clientside. + * 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(errorGetInitialProps: ErrorGetInitialProps): ErrorGetInitialProps { - return errorGetInitialProps; +export function wrapErrorGetInitialPropsWithSentry( + origErrorGetInitialProps: ErrorGetInitialProps, +): ErrorGetInitialProps { + return new Proxy(origErrorGetInitialProps, { + apply: async (wrappingTarget, thisArg, args: Parameters) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts index d5fa5336acf5..1fbbd8707063 100644 --- a/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts @@ -3,10 +3,15 @@ import type { NextPage } from 'next'; type GetInitialProps = Required['getInitialProps']; /** - * A passthrough function in case this function is used on the clientside. + * 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(getInitialProps: GetInitialProps): GetInitialProps { - return getInitialProps; +export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps { + return new Proxy(origGetInitialProps, { + apply: async (wrappingTarget, thisArg, args: Parameters) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts b/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts index 65de7ad5e46e..2235016856f4 100644 --- a/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapGetServerSidePropsWithSentry.ts @@ -1,10 +1,15 @@ import type { GetServerSideProps } from 'next'; /** - * A passthrough function in case this function is used on the clientside. + * 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 wrapGetServerSidePropsWithSentry(getServerSideProps: GetServerSideProps): GetServerSideProps { - return getServerSideProps; +export function wrapGetServerSidePropsWithSentry(origGetServerSideProps: GetServerSideProps): GetServerSideProps { + return new Proxy(origGetServerSideProps, { + apply: async (wrappingTarget, thisArg, args: Parameters) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts b/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts index f511ecf9f299..735a3cd8a936 100644 --- a/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts +++ b/packages/nextjs/src/client/wrapGetStaticPropsWithSentry.ts @@ -3,10 +3,15 @@ import type { GetStaticProps } from 'next'; type Props = { [key: string]: unknown }; /** - * A passthrough function in case this function is used on the clientside. + * 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 wrapGetStaticPropsWithSentry(getStaticProps: GetStaticProps): GetStaticProps { - return getStaticProps; +export function wrapGetStaticPropsWithSentry(origGetStaticProps: GetStaticProps): GetStaticProps { + return new Proxy(origGetStaticProps, { + apply: async (wrappingTarget, thisArg, args: Parameters>) => { + return await wrappingTarget.apply(thisArg, args); + }, + }); } /** diff --git a/packages/nextjs/src/index.types.ts b/packages/nextjs/src/index.types.ts index 0b115d213d5d..eb5fb9ca258f 100644 --- a/packages/nextjs/src/index.types.ts +++ b/packages/nextjs/src/index.types.ts @@ -127,6 +127,46 @@ export declare function withSentryServerSideErrorGetInitialProps) => ReturnType extends Promise ? ReturnType : Promise>; +/** + * Wraps a `getServerSideProps` function with Sentry error and performance instrumentation. + * + * @param origGetServerSideProps The `getServerSideProps` function + * @param parameterizedRoute The page's parameterized route + * @returns A wrapped version of the function + */ +export declare function wrapGetServerSidePropsWithSentry any>( + origGetServerSideProps: F, + parameterizedRoute: string, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapGetServerSidePropsWithSentry` instead. + */ +export declare function withSentryGetServerSideProps any>( + origGetServerSideProps: F, + parameterizedRoute: string, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * Wraps a `getStaticProps` function with Sentry error and performance instrumentation. + * + * @param origGetStaticProps The `getStaticProps` function + * @param parameterizedRoute The page's parameterized route + * @returns A wrapped version of the function + */ +export declare function wrapGetStaticPropsWithSentry any>( + origGetStaticPropsa: F, + parameterizedRoute: string, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + +/** + * @deprecated Use `wrapGetStaticPropsWithSentry` instead. + */ +export declare function withSentryGetStaticProps any>( + origGetStaticPropsa: F, + parameterizedRoute: string, +): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise>; + /** * Wraps an `app` directory component with Sentry error instrumentation. (Currently only reports errors for server components) */