Skip to content

Commit e8f8a3d

Browse files
committed
combine withSentry.ts with withSentryAPI.ts
1 parent e8f1ad2 commit e8f8a3d

File tree

5 files changed

+272
-252
lines changed

5 files changed

+272
-252
lines changed

packages/nextjs/src/config/templates/apiProxyLoaderTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as Sentry from '@sentry/nextjs';
1313
import type { PageConfig } from 'next';
1414

1515
// We import this from `wrappers` rather than directly from `next` because our version can work simultaneously with
16-
// multiple versions of next. See note in `wrappers/withSentry` for more.
16+
// multiple versions of next. See note in `wrappers/types` for more.
1717
import type { NextApiHandler } from '../wrappers';
1818

1919
type NextApiModule = {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
export type { AugmentedNextApiResponse, NextApiHandler, WrappedNextApiHandler } from './withSentry';
1+
export type { AugmentedNextApiResponse, NextApiHandler, WrappedNextApiHandler } from './types';
22

33
export { withSentryGetStaticProps } from './withSentryGetStaticProps';
44
export { withSentryServerSideGetInitialProps } from './withSentryServerSideGetInitialProps';
55
export { withSentryServerSideAppGetInitialProps } from './withSentryServerSideAppGetInitialProps';
66
export { withSentryServerSideDocumentGetInitialProps } from './withSentryServerSideDocumentGetInitialProps';
77
export { withSentryServerSideErrorGetInitialProps } from './withSentryServerSideErrorGetInitialProps';
88
export { withSentryGetServerSideProps } from './withSentryGetServerSideProps';
9-
export { withSentryAPI } from './withSentryAPI';
10-
export { withSentry } from './withSentry';
9+
export { withSentry, withSentryAPI } from './withSentryAPI';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Transaction } from '@sentry/types';
2+
import type { NextApiRequest, NextApiResponse } from 'next';
3+
4+
// The `NextApiHandler` and `WrappedNextApiHandler` types are the same as the official `NextApiHandler` type, except:
5+
//
6+
// a) The wrapped version returns only promises, because wrapped handlers are always async.
7+
//
8+
// b) Instead of having a return types based on `void` (Next < 12.1.6) or `unknown` (Next 12.1.6+), both the wrapped and
9+
// unwrapped versions of the type have both. This doesn't matter to users, because they exist solely on one side of that
10+
// version divide or the other. For us, though, it's entirely possible to have one version of Next installed in our
11+
// local repo (as a dev dependency) and have another Next version installed in a test app which also has the local SDK
12+
// linked in.
13+
//
14+
// In that case, if those two versions are on either side of the 12.1.6 divide, importing the official `NextApiHandler`
15+
// type here would break the test app's build, because it would set up a situation in which the linked SDK's
16+
// `withSentry` would refer to one version of the type (from the local repo's `node_modules`) while any typed handler in
17+
// the test app would refer to the other version of the type (from the test app's `node_modules`). By using a custom
18+
// version of the type compatible with both the old and new official versions, we can use any Next version we want in a
19+
// test app without worrying about type errors.
20+
//
21+
// c) These have internal SDK flags which the official Next types obviously don't have, one to allow our auto-wrapping
22+
// function, `withSentryAPI`, to pass the parameterized route into `withSentry`, and the other to prevent a manually
23+
// wrapped route from being wrapped again by the auto-wrapper.
24+
25+
export type NextApiHandler = {
26+
__sentry_route__?: string;
27+
(req: NextApiRequest, res: NextApiResponse): void | Promise<void> | unknown | Promise<unknown>;
28+
};
29+
30+
export type WrappedNextApiHandler = {
31+
__sentry_route__?: string;
32+
__sentry_wrapped__?: boolean;
33+
(req: NextApiRequest, res: NextApiResponse): Promise<void> | Promise<unknown>;
34+
};
35+
36+
export type AugmentedNextApiRequest = NextApiRequest & {
37+
__withSentry_applied__?: boolean;
38+
};
39+
40+
export type AugmentedNextApiResponse = NextApiResponse & {
41+
__sentryTransaction?: Transaction;
42+
};
43+
44+
export type ResponseEndMethod = AugmentedNextApiResponse['end'];
45+
export type WrappedResponseEndMethod = AugmentedNextApiResponse['end'];

packages/nextjs/src/config/wrappers/withSentry.ts

Lines changed: 0 additions & 244 deletions
This file was deleted.

0 commit comments

Comments
 (0)