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 { ...@@ -31,15 +31,10 @@ export class NextResponse extends Response {
return this[INTERNALS].cookies return this[INTERNALS].cookies
} }
static json(body: any, init?: ResponseInit) { static json(body: any, init?: ResponseInit): NextResponse {
const { headers, ...responseInit } = init || {} // @ts-expect-error This is not in lib/dom right now, and we can't augment it.
return new NextResponse(JSON.stringify(body), { const response: Response = Response.json(body, init)
...responseInit, return new NextResponse(response.body, response)
headers: {
...headers,
'content-type': 'application/json',
},
})
} }
static redirect(url: string | NextURL | URL, status = 307) { static redirect(url: string | NextURL | URL, status = 307) {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import { NextResponse } from 'next/server/web/spec-extension/response' import { NextResponse } from 'next/server/web/spec-extension/response'
const toJSON = async (response) => ({ const toJSON = async (response: Response) => ({
body: await response.json(), body: await response.json(),
contentType: response.headers.get('content-type'), contentType: response.headers.get('content-type'),
status: response.status, status: response.status,
...@@ -53,3 +53,8 @@ it('can be cloned', async () => { ...@@ -53,3 +53,8 @@ it('can be cloned', async () => {
server: 'Vercel', 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