Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove NextResponse.json as we already have it defined from inheriting Response #37887

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/next/server/web/spec-extension/response.ts
Expand Up @@ -31,15 +31,10 @@ export class NextResponse extends Response {
return this[INTERNALS].cookies
}

static json(body: any, init?: ResponseInit) {
const { headers, ...responseInit } = init || {}
return new NextResponse(JSON.stringify(body), {
...responseInit,
headers: {
...headers,
'content-type': 'application/json',
},
})
static json(body: any, init?: ResponseInit): NextResponse {
// @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)
}

static redirect(url: string | NextURL | URL, status = 307) {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/web-runtime/next-response.test.ts
Expand Up @@ -4,7 +4,7 @@

import { NextResponse } from 'next/server/web/spec-extension/response'

const toJSON = async (response) => ({
const toJSON = async (response: Response) => ({
body: await response.json(),
contentType: response.headers.get('content-type'),
status: response.status,
Expand Down Expand Up @@ -53,3 +53,8 @@ it('can be cloned', async () => {
server: 'Vercel',
})
})

it('can return JSON', async () => {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
const response = NextResponse.json({ hello: 'world' })
expect(await response.json()).toEqual({ hello: 'world' })
})