-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathapiWrapperTemplate.ts
70 lines (56 loc) · 2.63 KB
/
apiWrapperTemplate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* This file is a template for the code which will be substituted when our webpack loader handles API files in the
* `pages/` directory.
*
* We use `__SENTRY_WRAPPING_TARGET_FILE__` as a placeholder for the path to the file being wrapped. Because it's not a real package,
* this causes both TS and ESLint to complain, hence the pragma comments below.
*/
import * as Sentry from '@sentry/nextjs';
// @ts-expect-error See above
import * as origModule from '__SENTRY_WRAPPING_TARGET_FILE__';
import type { PageConfig } from 'next';
import type { NextApiHandler, VercelCronsConfig } from '../../common/types';
type NextApiModule = (
| {
// ESM export
default?: NextApiHandler;
}
// CJS export
| NextApiHandler
) & { config?: PageConfig };
const userApiModule = origModule as NextApiModule;
// Default to undefined. It's possible for Next.js users to not define any exports/handlers in an API route. If that is
// the case Next.js will crash during runtime but the Sentry SDK should definitely not crash so we need to handle it.
let userProvidedHandler = undefined;
if ('default' in userApiModule && typeof userApiModule.default === 'function') {
// Handle when user defines via ESM export: `export default myFunction;`
userProvidedHandler = userApiModule.default;
} else if (typeof userApiModule === 'function') {
// Handle when user defines via CJS export: "module.exports = myFunction;"
userProvidedHandler = userApiModule;
}
const origConfig = userApiModule.config || {};
// Setting `externalResolver` to `true` prevents nextjs from throwing a warning in dev about API routes resolving
// without sending a response. It's a false positive (a response is sent, but only after we flush our send queue), and
// we throw a warning of our own to tell folks that, but it's better if we just don't have to deal with it in the first
// place.
export const config = {
...origConfig,
api: {
...origConfig.api,
externalResolver: true,
},
};
declare const __VERCEL_CRONS_CONFIGURATION__: VercelCronsConfig;
let wrappedHandler = userProvidedHandler;
if (wrappedHandler && __VERCEL_CRONS_CONFIGURATION__) {
wrappedHandler = Sentry.wrapApiHandlerWithSentryVercelCrons(wrappedHandler, __VERCEL_CRONS_CONFIGURATION__);
}
if (wrappedHandler) {
wrappedHandler = Sentry.wrapApiHandlerWithSentry(wrappedHandler, '__ROUTE__');
}
export default wrappedHandler;
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
// not include anything whose name matches something we've explicitly exported above.
// @ts-expect-error See above
export * from '__SENTRY_WRAPPING_TARGET_FILE__';