Skip to content

Commit

Permalink
Add flag to disable API warning
Browse files Browse the repository at this point in the history
This flag is useful when you are using an external API resolver like express when using you are defining your API route, since the native functionality doesn't realize that the API actually sent a response.

A very simple use case example https://github.com/PabloSzx/next-external-api-resolver-example
  • Loading branch information
PabloSzx committed Mar 26, 2020
1 parent ee00813 commit 710aeeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/api-routes/api-middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export const config = {
}
```

`externalResolver` is a explicit flag that tells the server that this route is being handled by an external resolver like _express_ or _connect_ and it shouldn't make misleading warnings

```js
export const config = {
api: {
externalResolver: true,
},
}
```

## Connect/Express middleware support

You can also use [Connect](https://github.com/senchalabs/connect) compatible middleware.
Expand Down
8 changes: 7 additions & 1 deletion packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function apiResolver(
}
const config: PageConfig = resolverModule.config || {}
const bodyParser = config.api?.bodyParser !== false
const externalResolver = config.api?.externalResolver ?? false

apiReq.env = env ? collectEnv(req.url!, env, config.env) : {}

Expand Down Expand Up @@ -75,7 +76,12 @@ export async function apiResolver(
// Call API route method
await resolver(req, res)

if (process.env.NODE_ENV !== 'production' && !isResSent(res) && !wasPiped) {
if (
process.env.NODE_ENV !== 'production' &&
!externalResolver &&
!isResSent(res) &&
!wasPiped
) {
console.warn(
`API resolved without sending a response for ${req.url}, this may result in stalled requests.`
)
Expand Down
6 changes: 6 additions & 0 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export type PageConfig = {
* format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
*/
bodyParser?: { sizeLimit?: number | string } | false
/**
* Flag to disable warning "API page resolved
* without sending a response", due to explicitly
* using an external API resolver, like express
*/
externalResolver?: true
}
env?: Array<string>
}
Expand Down

0 comments on commit 710aeeb

Please sign in to comment.