Skip to content

Commit 7620215

Browse files
committed
add API proxy module template
1 parent 6760221 commit 7620215

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

packages/nextjs/rollup.npm.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export default [
1414
),
1515
...makeNPMConfigVariants(
1616
makeBaseNPMConfig({
17-
entrypoints: ['src/config/templates/prefixLoaderTemplate.ts', 'src/config/templates/proxyLoaderTemplate.ts'],
17+
entrypoints: [
18+
'src/config/templates/prefixLoaderTemplate.ts',
19+
'src/config/templates/proxyLoaderTemplate.ts',
20+
'src/config/templates/apiProxyLoaderTemplate.ts',
21+
],
1822

1923
packageSpecificConfig: {
2024
plugins: [plugins.makeRemoveMultiLineCommentsPlugin()],
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// Setting `externalResolver` to `true` prevents nextjs from throwing a warning in dev about API routes resolving
30+
// without sending a response. It's a false positive (a response is sent, but only after we flush our send queue), and
31+
// 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
32+
// place.
33+
export const config = {
34+
...origConfig,
35+
api: {
36+
...origConfig.api,
37+
externalResolver: true,
38+
},
39+
};
40+
41+
export default Sentry.withSentryAPI(maybeWrappedHandler, '__ROUTE__');
42+
43+
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
44+
// not include anything whose name matchs something we've explicitly exported above.
45+
// @ts-ignore See above
46+
// eslint-disable-next-line import/no-unresolved
47+
export * from '__RESOURCE_PATH__';

packages/nextjs/src/utils/withSentry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import { NextApiRequest, NextApiResponse } from 'next';
2828
// the test app would refer to the other version of the type (from the test app's `node_modules`). By using a custom
2929
// version of the type compatible with both the old and new official versions, we can use any Next version we want in
3030
// a test app without worrying about type errors.
31-
type NextApiHandler = (req: NextApiRequest, res: NextApiResponse) => void | Promise<void> | unknown | Promise<unknown>;
31+
export type NextApiHandler = (
32+
req: NextApiRequest,
33+
res: NextApiResponse,
34+
) => void | Promise<void> | unknown | Promise<unknown>;
3235
export type WrappedNextApiHandler = (req: NextApiRequest, res: NextApiResponse) => Promise<void> | Promise<unknown>;
3336

3437
export type AugmentedNextApiResponse = NextApiResponse & {

0 commit comments

Comments
 (0)