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

keep method when cloning Request, fixes #36522 #36539

Merged
merged 2 commits into from
May 22, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/server/web/spec-compliant/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class BaseRequest extends Body implements Request {
}

constructor(input: BaseRequest | string, init: RequestInit = {}) {
const method = init.method?.toUpperCase() ?? 'GET'
const method =
init.method?.toUpperCase() ??
(input instanceof BaseRequest ? input.method?.toUpperCase() : 'GET')
Comment on lines +19 to +21
Copy link
Contributor

@SukkaW SukkaW May 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const method =
init.method?.toUpperCase() ??
(input instanceof BaseRequest ? input.method?.toUpperCase() : 'GET')
const method = init.method?.toUpperCase() ??
(typeof input !== 'string' ? input.method?.toUpperCase() : 'GET')

The performant way to add type guard here.

Copy link
Member

@ijjk ijjk May 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we also need to update the checks to consider input being nullish if we check for !== 'string' here?


if (
(method === 'GET' || method === 'HEAD') &&
Expand Down
11 changes: 11 additions & 0 deletions test/unit/web-runtime/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ it('Request.referrer can be customized', async () => {
const request = new Request('https://vercel.com', { referrer: 'client' })
expect(request.referrer).toBe('client')
})

it('Request copies over method', async () => {
const request = new Request('https://vercel.com', {
method: 'POST',
body: 'hello',
})

const clonedRequest = new Request(request)

expect(clonedRequest.method).toBe(request.method)
})