Skip to content

fix(nextjs): Export isomorphic data fetching wrappers from client SDK #6790

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 16 commits into from
Jan 16, 2023
Merged
22 changes: 22 additions & 0 deletions packages/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
18 changes: 18 additions & 0 deletions packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we theoretically create spans instead of transactions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we definitely could in the future!

* so we are consistent with the serverside implementation.
*/
export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
return await origAppGetInitialProps.apply(this, args);
};
}

/**
* @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry;
Original file line number Diff line number Diff line change
@@ -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<DocumentGetInitialProps>
): ReturnType<DocumentGetInitialProps> {
return await origDocumentGetInitialProps.apply(this, args);
};
}

/**
* @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry;
21 changes: 21 additions & 0 deletions packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { NextPageContext } from 'next';
import type { ErrorProps } from 'next/error';

type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;

/**
* 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<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
return await origErrorGetInitialProps.apply(this, args);
};
}

/**
* @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry;
18 changes: 18 additions & 0 deletions packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { NextPage } from 'next';

type GetInitialProps = Required<NextPage>['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<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
return origGetInitialProps.apply(this, args);
};
}

/**
* @deprecated Use `wrapGetInitialPropsWithSentry` instead.
*/
export const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry;
68 changes: 68 additions & 0 deletions packages/nextjs/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,71 @@ export declare function wrapApiHandlerWithSentry<APIHandler extends (...args: an
): (
...args: Parameters<APIHandler>
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;

/**
* 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<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* @deprecated Use `wrapGetInitialPropsWithSentry` instead.
*/
export declare function withSentryServerSideGetInitialProps<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* 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<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
*/
export declare function withSentryServerSideAppGetInitialProps<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* 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<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
*/
export declare function withSentryServerSideDocumentGetInitialProps<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* 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<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;

/**
* @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
*/
export declare function withSentryServerSideErrorGetInitialProps<F extends (...args: any[]) => any>(
getInitialProps: F,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>>;