Skip to content

Commit 1b61597

Browse files
committed
add flags to NextApiHandler and WrappedNextApiHandler types
1 parent 00c287f commit 1b61597

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

packages/nextjs/src/utils/withSentry.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import * as domain from 'domain';
1313
import { NextApiRequest, NextApiResponse } from 'next';
1414

15-
// These are the same as the official `NextApiHandler` type, except
15+
// The `NextApiHandler` and `WrappedNextApiHandler` types are the same as the official `NextApiHandler` type, except:
1616
//
1717
// a) The wrapped version returns only promises, because wrapped handlers are always async.
1818
//
@@ -26,13 +26,23 @@ import { NextApiRequest, NextApiResponse } from 'next';
2626
// type here would break the test app's build, because it would set up a situation in which the linked SDK's
2727
// `withSentry` would refer to one version of the type (from the local repo's `node_modules`) while any typed handler in
2828
// the test app would refer to the other version of the type (from the test app's `node_modules`). By using a custom
29-
// version of the type compatible with both the old and new official versions, we can use any Next version we want in
30-
// a test app without worrying about type errors.
31-
export type NextApiHandler = (
32-
req: NextApiRequest,
33-
res: NextApiResponse,
34-
) => void | Promise<void> | unknown | Promise<unknown>;
35-
export type WrappedNextApiHandler = (req: NextApiRequest, res: NextApiResponse) => Promise<void> | Promise<unknown>;
29+
// version of the type compatible with both the old and new official versions, we can use any Next version we want in a
30+
// test app without worrying about type errors.
31+
//
32+
// c) These have internal SDK flags which the official Next types obviously don't have, one to allow our auto-wrapping
33+
// function, `withSentryAPI`, to pass the parameterized route into `withSentry`, and the other to prevent a manually
34+
// wrapped route from being wrapped again by the auto-wrapper.
35+
36+
export type NextApiHandler = {
37+
__sentry_route__?: string;
38+
(req: NextApiRequest, res: NextApiResponse): void | Promise<void> | unknown | Promise<unknown>;
39+
};
40+
41+
export type WrappedNextApiHandler = {
42+
__sentry_route__?: string;
43+
__sentry_wrapped__?: boolean;
44+
(req: NextApiRequest, res: NextApiResponse): Promise<void> | Promise<unknown>;
45+
};
3646

3747
export type AugmentedNextApiResponse = NextApiResponse & {
3848
__sentryTransaction?: Transaction;

0 commit comments

Comments
 (0)