Skip to content

Commit fbd2ff1

Browse files
authored
feat(cookie): generateCookie and generateSignedCookie helpers (#4285)
* generateCookie * generateSignedCookie * removed context arg from generators * tests * removed lines
1 parent 820d59c commit fbd2ff1

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/helper/cookie/index.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { Hono } from '../../hono'
2-
import { deleteCookie, getCookie, getSignedCookie, setCookie, setSignedCookie } from '.'
2+
import {
3+
deleteCookie,
4+
getCookie,
5+
getSignedCookie,
6+
setCookie,
7+
setSignedCookie,
8+
generateCookie,
9+
generateSignedCookie,
10+
} from '.'
311

412
describe('Cookie Middleware', () => {
513
describe('Parse cookie', () => {
@@ -445,4 +453,49 @@ describe('Cookie Middleware', () => {
445453
expect(await res.text()).toBe('choco')
446454
})
447455
})
456+
457+
describe('Generate cookie', () => {
458+
it('should generate a cookie', () => {
459+
const cookie = generateCookie('delicious_cookie', 'macha')
460+
expect(cookie).toBe('delicious_cookie=macha; Path=/')
461+
})
462+
463+
it('should generate a cookie with options', () => {
464+
const cookie = generateCookie('delicious_cookie', 'macha', {
465+
path: '/',
466+
secure: true,
467+
httpOnly: true,
468+
domain: 'example.com',
469+
})
470+
expect(cookie).toBe('delicious_cookie=macha; Domain=example.com; Path=/; HttpOnly; Secure')
471+
})
472+
473+
it('should generate a signed cookie', async () => {
474+
const cookie = await generateSignedCookie(
475+
'delicious_cookie',
476+
'macha',
477+
'secret chocolate chips'
478+
)
479+
expect(cookie).toBe(
480+
'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/'
481+
)
482+
})
483+
484+
it('should generate a signed cookie with options', async () => {
485+
const cookie = await generateSignedCookie(
486+
'delicious_cookie',
487+
'macha',
488+
'secret chocolate chips',
489+
{
490+
path: '/',
491+
secure: true,
492+
httpOnly: true,
493+
domain: 'example.com',
494+
}
495+
)
496+
expect(cookie).toBe(
497+
'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Domain=example.com; Path=/; HttpOnly; Secure'
498+
)
499+
})
500+
})
448501
})

src/helper/cookie/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const getSignedCookie: GetSignedCookie = async (
7575
return obj as any
7676
}
7777

78-
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => {
78+
export const generateCookie = (name: string, value: string, opt?: CookieOptions): string => {
7979
// Cookie names prefixed with __Secure- can be used only if they are set with the secure attribute.
8080
// Cookie names prefixed with __Host- can be used only if they are set with the secure attribute, must have a path of / (meaning any path at the host)
8181
// and must not have a Domain attribute.
@@ -93,16 +93,20 @@ export const setCookie = (c: Context, name: string, value: string, opt?: CookieO
9393
} else {
9494
cookie = serialize(name, value, { path: '/', ...opt })
9595
}
96+
return cookie
97+
}
98+
99+
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => {
100+
const cookie = generateCookie(name, value, opt)
96101
c.header('Set-Cookie', cookie, { append: true })
97102
}
98103

99-
export const setSignedCookie = async (
100-
c: Context,
104+
export const generateSignedCookie = async (
101105
name: string,
102106
value: string,
103107
secret: string | BufferSource,
104108
opt?: CookieOptions
105-
): Promise<void> => {
109+
): Promise<string> => {
106110
let cookie
107111
if (opt?.prefix === 'secure') {
108112
cookie = await serializeSigned('__Secure-' + name, value, secret, {
@@ -120,6 +124,17 @@ export const setSignedCookie = async (
120124
} else {
121125
cookie = await serializeSigned(name, value, secret, { path: '/', ...opt })
122126
}
127+
return cookie
128+
}
129+
130+
export const setSignedCookie = async (
131+
c: Context,
132+
name: string,
133+
value: string,
134+
secret: string | BufferSource,
135+
opt?: CookieOptions
136+
): Promise<void> => {
137+
const cookie = await generateSignedCookie(name, value, secret, opt)
123138
c.header('set-cookie', cookie, { append: true })
124139
}
125140

0 commit comments

Comments
 (0)