Skip to content

Commit

Permalink
refactor: use object interface instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 19, 2022
1 parent 2bc39ca commit c0c2235
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
17 changes: 8 additions & 9 deletions packages/next/server/web/spec-extension/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cookie from 'next/dist/compiled/cookie'
import { CookieSerializeOptions } from '../types'

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

const normalizeCookieOptions = (options: CookieSerializeOptions) => {
options = Object.assign({}, options)
Expand Down Expand Up @@ -64,17 +64,16 @@ export class NextCookies extends Cookies {
super(response.headers.get('cookie'))
this.response = response
}
get = (...args: Parameters<Cookies['get']>): string | undefined => {
const [value] = this.getWithOptions(...args)
return value
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 [raw, undefined]
const { [args[0]]: value, ...opts } = cookie.parse(raw)
return [value, opts]
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])
Expand Down
52 changes: 32 additions & 20 deletions test/unit/web-runtime/next-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ it('reflect .set into `set-cookie`', async () => {
const response = new NextResponse()

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

response.cookies
.set('foo', 'bar', { path: '/test' })
Expand All @@ -44,14 +47,14 @@ it('reflect .set into `set-cookie`', async () => {
expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.get('fooz')).toBe('barz')

expect(response.cookies.getWithOptions('foo')).toEqual([
'bar',
{ Path: '/test' },
])
expect(response.cookies.getWithOptions('fooz')).toEqual([
'barz',
{ Path: '/test2' },
])
expect(response.cookies.getWithOptions('foo')).toEqual({
value: 'bar',
options: { Path: '/test' },
})
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: 'barz',
options: { Path: '/test2' },
})

expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/test, fooz=barz; Path=/test2'
Expand All @@ -71,18 +74,21 @@ it('reflect .delete into `set-cookie`', async () => {
)

expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.getWithOptions('foo')).toEqual(['bar', { Path: '/' }])
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('barz')
expect(response.cookies.getWithOptions('fooz')).toEqual([
'barz',
{ Path: '/' },
])
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: 'barz',
options: { Path: '/' },
})

const firstDelete = response.cookies.delete('foo')
expect(firstDelete).toBe(true)
Expand All @@ -91,7 +97,10 @@ it('reflect .delete into `set-cookie`', async () => {
)

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

const secondDelete = response.cookies.delete('fooz')
expect(secondDelete).toBe(true)
Expand All @@ -101,10 +110,10 @@ it('reflect .delete into `set-cookie`', async () => {
)

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

Expand All @@ -126,7 +135,10 @@ it('reflect .clear into `set-cookie`', async () => {
)

expect(response.cookies.get('foo')).toBe('bar')
expect(response.cookies.getWithOptions('foo')).toEqual(['bar', { Path: '/' }])
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

0 comments on commit c0c2235

Please sign in to comment.