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