Skip to content

Commit

Permalink
Export the other cookie types
Browse files Browse the repository at this point in the history
  • Loading branch information
lfades committed Nov 22, 2022
1 parent 5f310ed commit 2b36be3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-ag
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'
export { ResponseCookies } from 'next/dist/server/web/spec-extension/cookies/response-cookies'
export {
RequestCookie,
ResponseCookie,
} from 'next/dist/server/web/spec-extension/cookies/types'
4 changes: 4 additions & 0 deletions packages/next/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const serverExports = {
RequestCookies:
require('next/dist/server/web/spec-extension/cookies/request-cookies')
.RequestCookies,
ResponseCookies:
require('next/dist/server/web/spec-extension/cookies/response-cookies')
.ResponseCookies,
}

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

export const config = {
runtime: 'experimental-edge',
Expand All @@ -8,7 +15,22 @@ const AsyncApiEndpoint = async (req: NextRequest) => {
// Test cookies type
const cookies: RequestCookies = req.cookies

return new Response(cookies instanceof RequestCookies ? 'ok' : 'error')
const res = new NextResponse()

const resCookies: ResponseCookies = res.cookies

const cookie: RequestCookie = { name: 'foo', value: 'bar' }

req.cookies.set(cookie)

const resCookie: ResponseCookie = { name: 'foo', value: 'bar' }

res.cookies.set(resCookie)

return NextResponse.json({
RequestCookies: cookies instanceof RequestCookies,
ResponseCookies: resCookies instanceof ResponseCookies,
})
}

export default AsyncApiEndpoint
6 changes: 4 additions & 2 deletions test/integration/typescript/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ describe('TypeScript Features', () => {
})

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

expect(data.RequestCookies).toBe(true)
expect(data.ResponseCookies).toBe(true)
})

// old behavior:
Expand Down

0 comments on commit 2b36be3

Please sign in to comment.