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 2 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
11 changes: 0 additions & 11 deletions packages/next/server/web/spec-extension/response.ts
Expand Up @@ -31,17 +31,6 @@ 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 redirect(url: string | NextURL | URL, status = 307) {
if (!REDIRECTS.has(status)) {
throw new RangeError(
Expand Down
46 changes: 5 additions & 41 deletions test/unit/web-runtime/next-response.test.ts
Expand Up @@ -4,47 +4,6 @@

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

const toJSON = async (response) => ({
body: await response.json(),
contentType: response.headers.get('content-type'),
status: response.status,
})

it('automatically parses and formats JSON', async () => {
expect(await toJSON(NextResponse.json({ message: 'hello!' }))).toMatchObject({
contentType: 'application/json',
body: { message: 'hello!' },
})

expect(
await toJSON(NextResponse.json({ status: 'success' }, { status: 201 }))
).toMatchObject({
contentType: 'application/json',
body: { status: 'success' },
status: 201,
})

expect(
await toJSON(
NextResponse.json({ error: { code: 'bad_request' } }, { status: 400 })
)
).toMatchObject({
contentType: 'application/json',
body: { error: { code: 'bad_request' } },
status: 400,
})

expect(await toJSON(NextResponse.json(null))).toMatchObject({
contentType: 'application/json',
body: null,
})

expect(await toJSON(NextResponse.json(''))).toMatchObject({
contentType: 'application/json',
body: '',
})
})

it('can be cloned', async () => {
const fetchResponse = await fetch('https://example.vercel.sh')
const newResponse = new NextResponse(fetchResponse.body, fetchResponse)
Expand All @@ -53,3 +12,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' })
})