Skip to content

Commit 46638d9

Browse files
committed
add API proxy module template
1 parent a1da6ed commit 46638d9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file is a template for the code which will be substituted when our webpack loader handles API files in the
3+
* `pages/` directory.
4+
*
5+
* We use `__RESOURCE_PATH__` as a placeholder for the path to the file being wrapped. Because it's not a real package,
6+
* this causes both TS and ESLint to complain, hence the pragma comments below.
7+
*/
8+
9+
// @ts-ignore See above
10+
// eslint-disable-next-line import/no-unresolved
11+
import * as origModule from '__RESOURCE_PATH__';
12+
import * as Sentry from '@sentry/nextjs';
13+
import type { PageConfig } from 'next';
14+
15+
// We import this from `withSentry` rather than directly from `next` because our version can work simultaneously with
16+
// multiple versions of next. See note in `withSentry` for more.
17+
import type { NextApiHandler } from '../../utils/withSentry';
18+
19+
type NextApiModule = {
20+
default: NextApiHandler;
21+
config?: PageConfig;
22+
};
23+
24+
const userApiModule = origModule as NextApiModule;
25+
26+
const maybeWrappedHandler = userApiModule.default;
27+
const origConfig = userApiModule.config || {};
28+
29+
export const config = {
30+
...origConfig,
31+
api: {
32+
...origConfig.api,
33+
externalResolver: true,
34+
},
35+
};
36+
37+
export default Sentry.withSentryAPI(maybeWrappedHandler, '__ROUTE__');
38+
39+
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
40+
// not include anything whose name matchs something we've explicitly exported above.
41+
// @ts-ignore See above
42+
// eslint-disable-next-line import/no-unresolved
43+
export * from '__RESOURCE_PATH__';

0 commit comments

Comments
 (0)