(
req: Request & {
secret?: string | string[]
},
res: Response
)
| 6 | type Res = Pick<S, 'setHeader' | 'getHeader'> |
| 7 | |
| 8 | export 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 | |
| 43 | export const clearCookie = <Request extends any = any, Response extends Res = Res>(req: Request, res: Response) => ( |
| 44 | name: string, |
no test coverage detected