Skip to content

Commit 41f8ba0

Browse files
authored
fix(cookie): add docs & expose in node v16 (nodejs#1849)
1 parent 7ee93b2 commit 41f8ba0

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

docs/api/Cookies.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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`

docsify/sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [Connector](/docs/api/Connector.md "Custom connector")
1212
* [Errors](/docs/api/Errors.md "Undici API - Errors")
1313
* [Fetch](/docs/api/Fetch.md "Undici API - Fetch")
14+
* [Cookies](/docs/api/Cookies.md "Undici API - Cookies")
1415
* [MockClient](/docs/api/MockClient.md "Undici API - MockClient")
1516
* [MockPool](/docs/api/MockPool.md "Undici API - MockPool")
1617
* [MockAgent](/docs/api/MockAgent.md "Undici API - MockAgent")

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
119119
module.exports.getGlobalOrigin = getGlobalOrigin
120120
}
121121

122-
if (nodeMajor >= 18) {
123-
const { WebSocket } = require('./lib/websocket/websocket')
124-
125-
module.exports.WebSocket = WebSocket
126-
122+
if (nodeMajor >= 16) {
127123
const { deleteCookie, getCookies, getSetCookies, setCookie } = require('./lib/cookies')
128124

129125
module.exports.deleteCookie = deleteCookie
@@ -132,6 +128,12 @@ if (nodeMajor >= 18) {
132128
module.exports.setCookie = setCookie
133129
}
134130

131+
if (nodeMajor >= 18) {
132+
const { WebSocket } = require('./lib/websocket/websocket')
133+
134+
module.exports.WebSocket = WebSocket
135+
}
136+
135137
module.exports.request = makeDispatcher(api.request)
136138
module.exports.stream = makeDispatcher(api.stream)
137139
module.exports.pipeline = makeDispatcher(api.pipeline)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"lint": "standard | snazzy",
4848
"lint:fix": "standard --fix | snazzy",
4949
"test": "npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && tsd",
50-
"test:cookies": "node scripts/verifyVersion 18 || tap test/cookie/*.js",
50+
"test:cookies": "node scripts/verifyVersion 16 || tap test/cookie/*.js",
5151
"test:node-fetch": "node scripts/verifyVersion.js 16 || mocha test/node-fetch",
5252
"test:fetch": "node scripts/verifyVersion.js 16 || (npm run build:node && tap test/fetch/*.js && tap test/webidl/*.js)",
5353
"test:jest": "node scripts/verifyVersion.js 14 || jest",

0 commit comments

Comments
 (0)