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

Edge Cookies: Add .getWithOptions method #36943

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/next/server/web/spec-extension/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import cookie from 'next/dist/compiled/cookie'
import { CookieSerializeOptions } from '../types'

type GetWithOptionsOutput = {
value: string | undefined
options: { [key: string]: string }
}

const normalizeCookieOptions = (options: CookieSerializeOptions) => {
options = Object.assign({}, options)

Expand Down Expand Up @@ -59,10 +64,22 @@ export class NextCookies extends Cookies {
super(response.headers.get('cookie'))
this.response = response
}
get = (...args: Parameters<Cookies['get']>) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we be explicit on types here?

Copy link
Member Author

Choose a reason for hiding this comment

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

with which purpose? It's actually resolving the type correctly

CleanShot 2022-05-19 at 14 35 31@2x

Kikobeats marked this conversation as resolved.
Show resolved Hide resolved
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']>) => {
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') {
throw new Error(
Expand All @@ -89,9 +106,9 @@ export class NextCookies extends Cookies {
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)

if (isDeleted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export const middleware: NextMiddleware = async function (request) {
const response = NextResponse.rewrite(`/rewrites/${bucket}`)
response.cookies.set('bucket', bucket, { maxAge: 10 })
return response
} else {
// check that `bucket` is type "string", not "any"
bucket.toUpperCase()
}

return NextResponse.rewrite(`/rewrites/${bucket}`)
Expand Down
71 changes: 50 additions & 21 deletions test/unit/web-runtime/next-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,30 @@ it('reflect .set into `set-cookie`', async () => {

const response = new NextResponse()

response.cookies.set('foo', 'bar')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/'
)
expect(response.cookies.get('foo')).toBe('foo=bar; Path=/')

response.cookies.set('foo', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=barz; Path=/'
)
expect(response.cookies.get('foo')).toBe('foo=barz; Path=/')

response.cookies.set('fooz', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=barz; Path=/, fooz=barz; Path=/'
)
expect(response.cookies.get('foo')).toBe(undefined)
expect(response.cookies.getWithOptions('foo')).toEqual({
value: undefined,
options: {},
})

response.cookies
.set('foo', 'bar', { path: '/test' })
.set('fooz', 'barz', { path: '/test2' })

expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.get('fooz')).toBe('barz')

expect(response.cookies.getWithOptions('foo')).toEqual({
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(
'foo=bar; Path=/, fooz=barz; Path=/'
'foo=bar; Path=/test, fooz=barz; Path=/test2'
)
})

Expand All @@ -68,13 +72,23 @@ it('reflect .delete into `set-cookie`', async () => {
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'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')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'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')
expect(firstDelete).toBe(true)
Expand All @@ -83,13 +97,23 @@ it('reflect .delete into `set-cookie`', async () => {
)

expect(response.cookies.get('foo')).toBe(undefined)
expect(response.cookies.getWithOptions('foo')).toEqual({
value: undefined,
options: {},
})

const secondDelete = response.cookies.delete('fooz')
expect(secondDelete).toBe(true)

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'
)

expect(response.cookies.get('fooz')).toBe(undefined)
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: undefined,
options: {},
})
expect(response.cookies.size).toBe(0)
})

Expand All @@ -109,7 +133,12 @@ it('reflect .clear into `set-cookie`', async () => {
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'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')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
Expand Down