|
| 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']; |
0 commit comments