Skip to content

Commit

Permalink
Fix NextRequest constructor parameters (#52001)
Browse files Browse the repository at this point in the history
FIx the type change introduced in #51727
  • Loading branch information
huozhi committed Jun 29, 2023
1 parent 2f42cf5 commit dfe08ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/src/server/web/spec-extension/request.ts
Expand Up @@ -16,7 +16,7 @@ export class NextRequest extends Request {
nextUrl: NextURL
}

constructor(input: URL | RequestInfo, init: RequestInit) {
constructor(input: URL | RequestInfo, init: RequestInit = {}) {
const url =
typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url)
Expand Down
23 changes: 23 additions & 0 deletions test/unit/web-runtime/next-request.test.ts
@@ -0,0 +1,23 @@
/**
* @jest-environment @edge-runtime/jest-environment
*/

import { expectTypeOf } from 'expect-type'
import { NextRequest } from 'next/src/server/web/spec-extension/request'

it('should have 1 required parameter for constructor', () => {
expect(NextRequest.length).toBe(1)
})

it('should allow the 2nd parameter to be undefined', () => {
const request = new NextRequest('https://vercel.com')
expectTypeOf(request).toMatchTypeOf<NextRequest>()

expect(
new NextRequest('https://vercel.com', { geo: { city: 'Mars' } })
).toHaveProperty('geo.city', 'Mars')
expect(new NextRequest('https://vercel.com')).toHaveProperty(
'nextUrl.pathname',
'/'
)
})

0 comments on commit dfe08ae

Please sign in to comment.