Unverified Commit cc8ab99a authored by Kiko Beats's avatar Kiko Beats Committed by GitHub

Edge Cookies: Add `.getWithOptions` method (#36943)

Hello,

This is an iteration after first work at https://github.com/vercel/next.js/pull/36478.

What that PR missed is a way to just get a cookie value. Well, this PR adds two new things:

`cookies.get` returns the cookie value that could be `string | undefined`:

```js
const response = new NextResponse()
response.cookies.set('foo', 'bar', { path: '/test' })

const value = response.cookies.get('foo')
console.log(value) // => 'bar'
```

Additionally, if you want to know all the cookie details, you can use `cookies.getWithOptions`:

```js
const response = new NextResponse()

response.cookies.set('foo', 'bar', { path: '/test' })
const { value, options } response.cookies.getWithOptions('foo')

console.log(value) // => 'bar'
console.log(options) // => { Path: '/test' }
```
parent 5acf9db6
import cookie from 'next/dist/compiled/cookie' import cookie from 'next/dist/compiled/cookie'
import { CookieSerializeOptions } from '../types' import { CookieSerializeOptions } from '../types'
type GetWithOptionsOutput = {
value: string | undefined
options: { [key: string]: string }
}
const normalizeCookieOptions = (options: CookieSerializeOptions) => { const normalizeCookieOptions = (options: CookieSerializeOptions) => {
options = Object.assign({}, options) options = Object.assign({}, options)
...@@ -59,10 +64,22 @@ export class NextCookies extends Cookies { ...@@ -59,10 +64,22 @@ export class NextCookies extends Cookies {
super(response.headers.get('cookie')) super(response.headers.get('cookie'))
this.response = response this.response = response
} }
get = (...args: Parameters<Cookies['get']>) => {
return this.getWithOptions(...args).value
}
getWithOptions = (
...args: Parameters<Cookies['get']>
): GetWithOptionsOutput => {
const raw = super.get(...args)
if (typeof raw !== 'string') return { value: raw, options: {} }
const { [args[0]]: value, ...options } = cookie.parse(raw)
return { value, options }
}
set = (...args: Parameters<Cookies['set']>) => { set = (...args: Parameters<Cookies['set']>) => {
const isAlreadyAdded = super.has(args[0]) const isAlreadyAdded = super.has(args[0])
const store = super.set(...args)
const currentCookie = store.get(args[0]) super.set(...args)
const currentCookie = super.get(args[0])
if (typeof currentCookie !== 'string') { if (typeof currentCookie !== 'string') {
throw new Error( throw new Error(
...@@ -89,9 +106,9 @@ export class NextCookies extends Cookies { ...@@ -89,9 +106,9 @@ export class NextCookies extends Cookies {
this.response.headers.append('set-cookie', currentCookie) this.response.headers.append('set-cookie', currentCookie)
} }
return store return this
} }
delete = (key: any, options: CookieSerializeOptions = {}) => { delete = (key: string, options: CookieSerializeOptions = {}) => {
const isDeleted = super.delete(key) const isDeleted = super.delete(key)
if (isDeleted) { if (isDeleted) {
......
...@@ -10,9 +10,6 @@ export const middleware: NextMiddleware = async function (request) { ...@@ -10,9 +10,6 @@ export const middleware: NextMiddleware = async function (request) {
const response = NextResponse.rewrite(`/rewrites/${bucket}`) const response = NextResponse.rewrite(`/rewrites/${bucket}`)
response.cookies.set('bucket', bucket, { maxAge: 10 }) response.cookies.set('bucket', bucket, { maxAge: 10 })
return response return response
} else {
// check that `bucket` is type "string", not "any"
bucket.toUpperCase()
} }
return NextResponse.rewrite(`/rewrites/${bucket}`) return NextResponse.rewrite(`/rewrites/${bucket}`)
......
...@@ -34,26 +34,30 @@ it('reflect .set into `set-cookie`', async () => { ...@@ -34,26 +34,30 @@ it('reflect .set into `set-cookie`', async () => {
const response = new NextResponse() const response = new NextResponse()
response.cookies.set('foo', 'bar') expect(response.cookies.get('foo')).toBe(undefined)
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(response.cookies.getWithOptions('foo')).toEqual({
'foo=bar; Path=/' value: undefined,
) options: {},
expect(response.cookies.get('foo')).toBe('foo=bar; Path=/') })
response.cookies.set('foo', 'barz') response.cookies
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( .set('foo', 'bar', { path: '/test' })
'foo=barz; Path=/' .set('fooz', 'barz', { path: '/test2' })
)
expect(response.cookies.get('foo')).toBe('foo=barz; Path=/') expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.get('fooz')).toBe('barz')
response.cookies.set('fooz', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(response.cookies.getWithOptions('foo')).toEqual({
'foo=barz; Path=/, fooz=barz; Path=/' value: 'bar',
) options: { Path: '/test' },
})
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: 'barz',
options: { Path: '/test2' },
})
response.cookies.set('foo', 'bar')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/, fooz=barz; Path=/' 'foo=bar; Path=/test, fooz=barz; Path=/test2'
) )
}) })
...@@ -68,13 +72,23 @@ it('reflect .delete into `set-cookie`', async () => { ...@@ -68,13 +72,23 @@ it('reflect .delete into `set-cookie`', async () => {
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/' 'foo=bar; Path=/'
) )
expect(response.cookies.get('foo')).toBe('foo=bar; Path=/')
expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.getWithOptions('foo')).toEqual({
value: 'bar',
options: { Path: '/' },
})
response.cookies.set('fooz', 'barz') response.cookies.set('fooz', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/, fooz=barz; Path=/' 'foo=bar; Path=/, fooz=barz; Path=/'
) )
expect(response.cookies.get('fooz')).toBe('fooz=barz; Path=/')
expect(response.cookies.get('fooz')).toBe('barz')
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: 'barz',
options: { Path: '/' },
})
const firstDelete = response.cookies.delete('foo') const firstDelete = response.cookies.delete('foo')
expect(firstDelete).toBe(true) expect(firstDelete).toBe(true)
...@@ -83,13 +97,23 @@ it('reflect .delete into `set-cookie`', async () => { ...@@ -83,13 +97,23 @@ it('reflect .delete into `set-cookie`', async () => {
) )
expect(response.cookies.get('foo')).toBe(undefined) expect(response.cookies.get('foo')).toBe(undefined)
expect(response.cookies.getWithOptions('foo')).toEqual({
value: undefined,
options: {},
})
const secondDelete = response.cookies.delete('fooz') const secondDelete = response.cookies.delete('fooz')
expect(secondDelete).toBe(true) expect(secondDelete).toBe(true)
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT' 'fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
) )
expect(response.cookies.get('fooz')).toBe(undefined) expect(response.cookies.get('fooz')).toBe(undefined)
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: undefined,
options: {},
})
expect(response.cookies.size).toBe(0) expect(response.cookies.size).toBe(0)
}) })
...@@ -109,7 +133,12 @@ it('reflect .clear into `set-cookie`', async () => { ...@@ -109,7 +133,12 @@ it('reflect .clear into `set-cookie`', async () => {
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/' 'foo=bar; Path=/'
) )
expect(response.cookies.get('foo')).toBe('foo=bar; Path=/')
expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.getWithOptions('foo')).toEqual({
value: 'bar',
options: { Path: '/' },
})
response.cookies.set('fooz', 'barz') response.cookies.set('fooz', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment