MCPcopy
hub / github.com/tinyhttp/tinyhttp / setCookie

Function setCookie

packages/res/src/cookie.ts:8–41  ·  view source on GitHub ↗
(
  req: Request & {
    secret?: string | string[]
  },
  res: Response
)

Source from the content-addressed store, hash-verified

6type Res = Pick<S, 'setHeader' | 'getHeader'>
7
8export const setCookie = <Request extends any = any, Response extends Res = Res>(
9 req: Request & {
10 secret?: string | string[]
11 },
12 res: Response
13) => (
14 name: string,
15 value: string | Record<string, unknown>,
16 options: cookie.SerializeOptions &
17 Partial<{
18 signed: boolean
19 }> = {}
20): Response => {
21 const secret = req.secret as string
22
23 const signed = options.signed || false
24
25 if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies')
26
27 let val = typeof value === 'object' ? 'j:' + JSON.stringify(value) : String(value)
28
29 if (signed) val = 's:' + sign(val, secret)
30
31 if (options.maxAge) {
32 options.expires = new Date(Date.now() + options.maxAge)
33 options.maxAge /= 1000
34 }
35
36 if (options.path == null) options.path = '/'
37
38 append(res)('Set-Cookie', `${cookie.serialize(name, String(val), options)}`)
39
40 return res
41}
42
43export const clearCookie = <Request extends any = any, Response extends Res = Res>(req: Request, res: Response) => (
44 name: string,

Callers 3

extendMiddlewareFunction · 0.90
res.test.tsFile · 0.90
clearCookieFunction · 0.70

Calls 3

signFunction · 0.90
appendFunction · 0.90
serializeMethod · 0.80

Tested by

no test coverage detected