Unverified Commit 92915e47 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

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

Lately, `Response.json` was introduced as a standard static method.
That means we can remove our implementation of `NextResponse.json` piggyback on `Response.json`
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 2ff660c2
......@@ -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) {
......
......@@ -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,
......@@ -53,3 +53,8 @@ it('can be cloned', async () => {
server: 'Vercel',
})
})
it('can return JSON', async () => {
const response = NextResponse.json({ hello: 'world' })
expect(await response.json()).toEqual({ hello: 'world' })
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment