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

Export the RequestCookies class from next/server #42388

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { NextMiddleware } from 'next/dist/server/web/types'
export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-agent'
export { userAgent } from 'next/dist/server/web/spec-extension/user-agent'
export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url'
export { RequestCookies } from 'next/dist/server/web/spec-extension/cookies/request-cookies'
4 changes: 4 additions & 0 deletions packages/next/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const serverExports = {
.userAgentFromString,
userAgent: require('next/dist/server/web/spec-extension/user-agent')
.userAgent,
RequestCookies:
require('next/dist/server/web/spec-extension/cookies/request-cookies')
.RequestCookies,
}

if (typeof URLPattern !== 'undefined') {
Expand All @@ -24,3 +27,4 @@ exports.NextResponse = serverExports.NextResponse
exports.userAgentFromString = serverExports.userAgentFromString
exports.userAgent = serverExports.userAgent
exports.URLPattern = serverExports.URLPattern
exports.RequestCookies = serverExports.RequestCookies
14 changes: 14 additions & 0 deletions test/integration/typescript/pages/api/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NextRequest, RequestCookies } from 'next/server'

export const config = {
runtime: 'experimental-edge',
}

const AsyncApiEndpoint = async (req: NextRequest) => {
// Test cookies type
const cookies: RequestCookies = req.cookies

return new Response(cookies instanceof RequestCookies ? 'ok' : 'error')
Copy link
Member Author

Choose a reason for hiding this comment

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

This was failing for me locally, what could be the reason?

The following worked fine:

new RequestCookies(req.headers) instanceof RequestCookies // true

But when checking for req.cookies directly it does not:

req.cookies instanceof RequestCookies // false

}

export default AsyncApiEndpoint
5 changes: 5 additions & 0 deletions test/integration/typescript/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('TypeScript Features', () => {
expect($('#imported-value').text()).toBe('OK')
})

it('should handle edge api route and req cookies', async () => {
const data = await renderViaHTTP(appPort, '/api/edge')
expect(data).toEqual('ok')
})

// old behavior:
it.skip('should report type checking to stdout', async () => {
expect(output).toContain('waiting for typecheck results...')
Expand Down