Skip to content

Commit 2802a6c

Browse files
committed
add and export pass-through wrapper functions
1 parent 0ed650d commit 2802a6c

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { withSentryGSPaths } from './withSentryGSPaths';
2+
export { withSentryGSProps } from './withSentryGSProps';
3+
export { withSentryGSSP } from './withSentryGSSP';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {
2+
GetServerSideProps,
3+
GetServerSidePropsContext,
4+
GetServerSidePropsResult,
5+
GetStaticPaths,
6+
GetStaticPathsContext,
7+
GetStaticPathsResult,
8+
GetStaticProps,
9+
GetStaticPropsContext,
10+
GetStaticPropsResult,
11+
} from 'next';
12+
13+
type Paths = { [key: string]: string | string[] };
14+
type Props = { [key: string]: unknown };
15+
16+
export type GSPaths = {
17+
fn: GetStaticPaths;
18+
wrappedFn: GetStaticPaths;
19+
context: GetStaticPathsContext;
20+
result: GetStaticPathsResult<Paths>;
21+
};
22+
23+
export type GSProps = {
24+
fn: GetStaticProps;
25+
wrappedFn: GetStaticProps;
26+
context: GetStaticPropsContext;
27+
result: GetStaticPropsResult<Props>;
28+
};
29+
30+
export type GSSP = {
31+
fn: GetServerSideProps;
32+
wrappedFn: GetServerSideProps;
33+
context: GetServerSidePropsContext;
34+
result: GetServerSidePropsResult<Props>;
35+
};
36+
37+
export type DataFetchingFunction = GSPaths | GSProps | GSSP;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { GSPaths } from './types';
2+
import { callOriginal } from './wrapperUtils';
3+
4+
/**
5+
* Create a wrapped version of the user's exported `getStaticPaths` function
6+
*
7+
* @param origGSPaths: The user's `getStaticPaths` function
8+
* @returns A wrapped version of the function
9+
*/
10+
export function withSentryGSPaths(origGSPaths: GSPaths['fn']): GSPaths['wrappedFn'] {
11+
const wrappedGSPaths = async function (context: GSPaths['context']): Promise<GSPaths['result']> {
12+
return callOriginal<GSPaths>(origGSPaths, context);
13+
};
14+
15+
return wrappedGSPaths;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GSProps } from './types';
2+
import { callOriginal } from './wrapperUtils';
3+
4+
/**
5+
* Create a wrapped version of the user's exported `getStaticProps` function
6+
*
7+
* @param origGSProps: The user's `getStaticProps` function
8+
* @returns A wrapped version of the function
9+
*/
10+
export function withSentryGSProps(origGSProps: GSProps['fn']): GSProps['wrappedFn'] {
11+
const wrappedGSProps = async function (context: GSProps['context']): Promise<GSProps['result']> {
12+
return callOriginal<GSProps>(origGSProps, context);
13+
};
14+
15+
return wrappedGSProps;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GSSP } from './types';
2+
import { callOriginal } from './wrapperUtils';
3+
4+
/**
5+
* Create a wrapped version of the user's exported `getServerSideProps` function
6+
*
7+
* @param origGSSP: The user's `getServerSideProps` function
8+
* @returns A wrapped version of the function
9+
*/
10+
export function withSentryGSSP(origGSSP: GSSP['fn']): GSSP['wrappedFn'] {
11+
const wrappedGSSP = async function (context: GSSP['context']): Promise<GSSP['result']> {
12+
return callOriginal<GSSP>(origGSSP, context);
13+
};
14+
15+
return wrappedGSSP;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DataFetchingFunction } from './types';
2+
3+
/**
4+
* Pass-through wrapper for the original function, used as a first step in eventually wrapping the data-fetching
5+
* functions with code for tracing.
6+
*
7+
* @template T Types for `getStaticPaths`, `getStaticProps`, and `getServerSideProps`
8+
* @param origFunction The user's exported `getStaticPaths`, `getStaticProps`, or `getServerSideProps` function
9+
* @param context The context object passed by nextjs to the function
10+
* @returns The result of calling the user's function
11+
*/
12+
export async function callOriginal<T extends DataFetchingFunction>(
13+
origFunction: T['fn'],
14+
context: T['context'],
15+
): Promise<T['result']> {
16+
let pathsOrProps;
17+
18+
// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed
19+
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic
20+
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck.
21+
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
22+
pathsOrProps = await (origFunction as any)(context);
23+
24+
return pathsOrProps;
25+
}

packages/nextjs/src/index.server.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ function addServerIntegrations(options: NextjsOptions): void {
124124

125125
export type { SentryWebpackPluginOptions } from './config/types';
126126
export { withSentryConfig } from './config';
127-
export { withSentry } from './utils/withSentry';
128127
export { isBuild } from './utils/isBuild';
128+
export { withSentryGSProps, withSentryGSSP, withSentryGSPaths } from './config/wrappers';
129+
export { withSentry } from './utils/withSentry';
129130

130131
// Wrap various server methods to enable error monitoring and tracing. (Note: This only happens for non-Vercel
131132
// deployments, because the current method of doing the wrapping a) crashes Next 12 apps deployed to Vercel and

0 commit comments

Comments
 (0)