From 5f310ed1d3c073f0d56ae2339f28b89e2b2c0e76 Mon Sep 17 00:00:00 2001 From: LFades Date: Wed, 2 Nov 2022 17:00:53 -0500 Subject: [PATCH 1/2] Shared cookies are the best cookies --- packages/next/server.d.ts | 1 + packages/next/server.js | 4 ++++ test/integration/typescript/pages/api/edge.ts | 14 ++++++++++++++ test/integration/typescript/test/index.test.js | 5 +++++ 4 files changed, 24 insertions(+) create mode 100644 test/integration/typescript/pages/api/edge.ts diff --git a/packages/next/server.d.ts b/packages/next/server.d.ts index 2099f54835e10..84e473b406579 100644 --- a/packages/next/server.d.ts +++ b/packages/next/server.d.ts @@ -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' diff --git a/packages/next/server.js b/packages/next/server.js index 53325c11e290c..ed936a822c207 100644 --- a/packages/next/server.js +++ b/packages/next/server.js @@ -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') { @@ -24,3 +27,4 @@ exports.NextResponse = serverExports.NextResponse exports.userAgentFromString = serverExports.userAgentFromString exports.userAgent = serverExports.userAgent exports.URLPattern = serverExports.URLPattern +exports.RequestCookies = serverExports.RequestCookies diff --git a/test/integration/typescript/pages/api/edge.ts b/test/integration/typescript/pages/api/edge.ts new file mode 100644 index 0000000000000..240dff4134da6 --- /dev/null +++ b/test/integration/typescript/pages/api/edge.ts @@ -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') +} + +export default AsyncApiEndpoint diff --git a/test/integration/typescript/test/index.test.js b/test/integration/typescript/test/index.test.js index 3268404f6c7d5..7dbc0cc3ab2e4 100644 --- a/test/integration/typescript/test/index.test.js +++ b/test/integration/typescript/test/index.test.js @@ -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...') From 2b36be3e616d0950fd60ca360e130d5d23e1e84f Mon Sep 17 00:00:00 2001 From: LFades Date: Tue, 22 Nov 2022 11:40:25 -0500 Subject: [PATCH 2/2] Export the other cookie types --- packages/next/server.d.ts | 5 ++++ packages/next/server.js | 4 +++ test/integration/typescript/pages/api/edge.ts | 26 +++++++++++++++++-- .../integration/typescript/test/index.test.js | 6 +++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/next/server.d.ts b/packages/next/server.d.ts index 84e473b406579..70b0c0cd9e294 100644 --- a/packages/next/server.d.ts +++ b/packages/next/server.d.ts @@ -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' diff --git a/packages/next/server.js b/packages/next/server.js index ed936a822c207..ada5455db6545 100644 --- a/packages/next/server.js +++ b/packages/next/server.js @@ -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') { @@ -28,3 +31,4 @@ exports.userAgentFromString = serverExports.userAgentFromString exports.userAgent = serverExports.userAgent exports.URLPattern = serverExports.URLPattern exports.RequestCookies = serverExports.RequestCookies +exports.ResponseCookies = serverExports.ResponseCookies diff --git a/test/integration/typescript/pages/api/edge.ts b/test/integration/typescript/pages/api/edge.ts index 240dff4134da6..438125d6af567 100644 --- a/test/integration/typescript/pages/api/edge.ts +++ b/test/integration/typescript/pages/api/edge.ts @@ -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', @@ -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 diff --git a/test/integration/typescript/test/index.test.js b/test/integration/typescript/test/index.test.js index 7dbc0cc3ab2e4..a22e887f9751f 100644 --- a/test/integration/typescript/test/index.test.js +++ b/test/integration/typescript/test/index.test.js @@ -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: