1 | # Cookie Handling
|
---|
2 |
|
---|
3 | ## `Cookie` interface
|
---|
4 |
|
---|
5 | * **name** `string`
|
---|
6 | * **value** `string`
|
---|
7 | * **expires** `Date|number` (optional)
|
---|
8 | * **maxAge** `number` (optional)
|
---|
9 | * **domain** `string` (optional)
|
---|
10 | * **path** `string` (optional)
|
---|
11 | * **secure** `boolean` (optional)
|
---|
12 | * **httpOnly** `boolean` (optional)
|
---|
13 | * **sameSite** `'String'|'Lax'|'None'` (optional)
|
---|
14 | * **unparsed** `string[]` (optional) Left over attributes that weren't parsed.
|
---|
15 |
|
---|
16 | ## `deleteCookie(headers, name[, attributes])`
|
---|
17 |
|
---|
18 | Sets the expiry time of the cookie to the unix epoch, causing browsers to delete it when received.
|
---|
19 |
|
---|
20 | ```js
|
---|
21 | import { deleteCookie, Headers } from 'undici'
|
---|
22 |
|
---|
23 | const headers = new Headers()
|
---|
24 | deleteCookie(headers, 'name')
|
---|
25 |
|
---|
26 | console.log(headers.get('set-cookie')) // name=; Expires=Thu, 01 Jan 1970 00:00:00 GMT
|
---|
27 | ```
|
---|
28 |
|
---|
29 | Arguments:
|
---|
30 |
|
---|
31 | * **headers** `Headers`
|
---|
32 | * **name** `string`
|
---|
33 | * **attributes** `{ path?: string, domain?: string }` (optional)
|
---|
34 |
|
---|
35 | Returns: `void`
|
---|
36 |
|
---|
37 | ## `getCookies(headers)`
|
---|
38 |
|
---|
39 | Parses the `Cookie` header and returns a list of attributes and values.
|
---|
40 |
|
---|
41 | ```js
|
---|
42 | import { getCookies, Headers } from 'undici'
|
---|
43 |
|
---|
44 | const headers = new Headers({
|
---|
45 | cookie: 'get=cookies; and=attributes'
|
---|
46 | })
|
---|
47 |
|
---|
48 | console.log(getCookies(headers)) // { get: 'cookies', and: 'attributes' }
|
---|
49 | ```
|
---|
50 |
|
---|
51 | Arguments:
|
---|
52 |
|
---|
53 | * **headers** `Headers`
|
---|
54 |
|
---|
55 | Returns: `Record<string, string>`
|
---|
56 |
|
---|
57 | ## `getSetCookies(headers)`
|
---|
58 |
|
---|
59 | Parses all `Set-Cookie` headers.
|
---|
60 |
|
---|
61 | ```js
|
---|
62 | import { getSetCookies, Headers } from 'undici'
|
---|
63 |
|
---|
64 | const headers = new Headers({ 'set-cookie': 'undici=getSetCookies; Secure' })
|
---|
65 |
|
---|
66 | console.log(getSetCookies(headers))
|
---|
67 | // [
|
---|
68 | // {
|
---|
69 | // name: 'undici',
|
---|
70 | // value: 'getSetCookies',
|
---|
71 | // secure: true
|
---|
72 | // }
|
---|
73 | // ]
|
---|
74 |
|
---|
75 | ```
|
---|
76 |
|
---|
77 | Arguments:
|
---|
78 |
|
---|
79 | * **headers** `Headers`
|
---|
80 |
|
---|
81 | Returns: `Cookie[]`
|
---|
82 |
|
---|
83 | ## `setCookie(headers, cookie)`
|
---|
84 |
|
---|
85 | Appends a cookie to the `Set-Cookie` header.
|
---|
86 |
|
---|
87 | ```js
|
---|
88 | import { setCookie, Headers } from 'undici'
|
---|
89 |
|
---|
90 | const headers = new Headers()
|
---|
91 | setCookie(headers, { name: 'undici', value: 'setCookie' })
|
---|
92 |
|
---|
93 | console.log(headers.get('Set-Cookie')) // undici=setCookie
|
---|
94 | ```
|
---|
95 |
|
---|
96 | Arguments:
|
---|
97 |
|
---|
98 | * **headers** `Headers`
|
---|
99 | * **cookie** `Cookie`
|
---|
100 |
|
---|
101 | Returns: `void`
|
---|