Skip to content

Commit

Permalink
Add optional generic parameter to NextResponse (#47526)
Browse files Browse the repository at this point in the history
Fixes #45943

### What?

Allow for type checking the return type of a Route Handler in the `app/`
directory:

[TypeScript
Playground](https://www.typescriptlang.org/play?#code/MYewdgzgLgBAkgOQCoFEBKCCCAZAyjAXhlwE8BbAIxABsAKAcgEswoBTAJzAENqZ3WIAB3ARW9AJQAoSawAew9rGDUuECDARyoaAcMisAPACEQAExKEYAVzABrMCADuYAHwwtrMKfU6hI1jAA3pIwMADaiKgYOLgAugBcQSGhMCbmAPyJaSQANMkAvtKhoJBQ7FbAUCDstFQZWWYkcGCMsAA+MGBW1NQ5MMytib56os2tloH54kkpMBBWghy1jX0DUFLJoVAAFowQEcjoWHixE4WhhcnQXFCMwDAAVhDgBgBSz2DZLsvmie-g2VWLSgmRgw38Y3WiU0sm0un8bw+XxmKQA9KiYAABKAQAC0ckWlXx7HY1RgSF26j2nRAsGYMGojAoqNMIDIfEYAHNtrAHI4+lwvDBHAFgIL6LAuFZOWRPHSoAA6TYwErQPjw-RDDWiSzg-QKp7gH65frAqSzfhQKycTqsRwaLR60S0fh+fV1E2ukasc0wQqXAnVWAAMxslUY4BgAHEUEhaOJoY7tYZAlwKMBEgBGABMAGZ8m5AjB0dHPBw7jAuOxOVZZSwYIJVKJTMlGMGYLQALI3bYK9iC1lkeMwNwABgVAFZpkXlZbrWAHbCnawDR9aKn01m81NixjAKDkNPcJOqiQ9Kuq-EqBWSc5tMLhbtEq6NgVMrGDiXosm-9B3JYPHCkuwp6NKaoAkqwV4BvIQYwKGYDhpGMZINm8aJkuyZBLuGggDAnJluwFZVjWdawFYzatu2XY9n2A5ssOY6TtOMCzqwVp3kmj4roaYDrmmGYwDm+bTP+h6ASelZgBYZ5cMAwCsIIbAthcN5sfOi4Pt6z68a+76ft+si-iJ+5icewGSdJoGyfJimsC2hRAA)

![Screenshot 2023-03-25 at 16 59
51](https://user-images.githubusercontent.com/1935696/227728328-43350f50-5067-4d27-8701-000569e757cc.png)

### Why?

There is currently no nice way to enforce the return type of a Route
Handler in the `app/` directory.

This is possible with `NextApiResponse` in the `pages/` directory,
because of this PR:

- #7841

### How?

This implements a generic parameter for the `NextResponse` class, which
can be used to pass a type argument when setting the return type for a
Route Handler.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------

Co-authored-by: Shu Ding <g@shud.in>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed May 19, 2023
1 parent 7450ab4 commit 347da42
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/next/src/server/web/spec-extension/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ function handleMiddlewareField(
}
}

export class NextResponse extends Response {
export class NextResponse<Body = unknown> extends Response {
[INTERNALS]: {
cookies: ResponseCookies
url?: NextURL
body?: Body
}

constructor(body?: BodyInit | null, init: ResponseInit = {}) {
Expand Down Expand Up @@ -66,7 +67,10 @@ export class NextResponse extends Response {
return this[INTERNALS].cookies
}

static json(body: any, init?: ResponseInit): NextResponse {
static json<JsonBody>(
body: JsonBody,
init?: ResponseInit
): NextResponse<JsonBody> {
// @ts-expect-error This is not in lib/dom right now, and we can't augment it.
const response: Response = Response.json(body, init)
return new NextResponse(response.body, response)
Expand Down

0 comments on commit 347da42

Please sign in to comment.