2019-01-14 03:46:38 +00:00
# node-fetch-cookies
2019-06-15 22:06:28 +00:00
A [node-fetch ](https://github.com/bitinn/node-fetch ) wrapper with support for cookies.
It supports reading/writing from/to a JSON cookie jar and keeps cookies in memory until you call `CookieJar.save()` to reduce disk I/O.
2019-01-14 03:46:38 +00:00
2019-06-15 22:06:28 +00:00
## Usage Example
2019-01-14 03:46:38 +00:00
```javascript
2019-06-15 22:06:28 +00:00
import {fetch, CookieJar} from "node-fetch-cookies";
(async () => {
// creates a CookieJar instance
let cookieJar = new CookieJar("rw", "jar.json");
// usual fetch usage, except with one or multiple cookie jars as first parameter
const response = await fetch(cookieJar, "https://google.de");
// save the received cookies to disk
cookieJar.save();
})();
2019-01-14 03:46:38 +00:00
```
2019-06-15 22:06:28 +00:00
## Documentation
### fetch(cookieJar, url, options)
2019-07-22 11:57:47 +00:00
- `cookieJar` A [CookieJar ](#class-cookiejar ) instance, an array of CookieJar instances or null, if you don't want to send or store cookies.
2019-06-15 22:06:28 +00:00
- `url` and `options` as in https://github.com/bitinn/node-fetch#fetchurl-options
### Class: CookieJar
A class that stores cookies.
#### Properties
- `flags` The read/write flags as specified below.
- `file` The path of the cookie jar on the disk.
- `cookies` A [Map ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map ) mapping cookie names to their properties.
2019-07-20 21:39:15 +00:00
#### new CookieJar(flags[, file, cookies])
2019-06-15 22:06:28 +00:00
- `flags` A string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to [fetch ](#fetchcookiejar-url-options ).
- `r` : only read from this jar
- `w` : only write to this jar
- `rw` or `wr` : read/write from/to this jar
2019-07-20 21:39:15 +00:00
- `file` An optional string containing a relative or absolute path to the file on the disk to use.
2019-06-15 22:06:28 +00:00
- `cookies` An optional initializer for the cookie jar - either an array of [Cookie ](#class-cookie ) instances or a single Cookie instance.
#### addCookie(cookie[, url])
Adds a cookie to the jar.
2019-08-13 20:19:25 +00:00
- `cookie` A [Cookie ](#class-cookie ) instance to add to the cookie jar. Alternatively this can also be a string, for example a serialized cookie received from a website. In this case `url` should be specified.
2019-06-15 22:06:28 +00:00
- `url` The url a cookie has been received from.
2019-08-13 20:19:25 +00:00
#### addFromFile(file)
Reads a cookie jar from the disk and adds the contained cookies.
#### domains()
2019-08-14 21:35:55 +00:00
Returns an iterator over all domains currently stored cookies for.
2019-08-13 20:19:25 +00:00
2019-08-14 21:35:55 +00:00
#### *cookiesDomain(domain)
Returns an iterator over all cookies currently stored for `domain` .
2019-08-13 20:19:25 +00:00
2019-08-14 21:35:55 +00:00
#### *cookiesValid()
2019-08-13 20:19:25 +00:00
Returns an iterator over all valid (non-expired) cookies.
2019-08-14 21:35:55 +00:00
#### *cookiesAll()
2019-08-13 20:19:25 +00:00
Returns an iterator over all cookies currently stored.
2019-08-14 21:35:55 +00:00
#### *cookiesValidForRequest(url)
Returns an iterator over all cookies valid for a request to `url` .
2019-08-13 20:19:25 +00:00
#### deleteExpired()
Removes all expired cookies from the jar.
2019-06-15 22:06:28 +00:00
#### save()
Saves the cookie jar to disk. Only non-expired cookies are saved.
### Class: Cookie
An abstract representation of a cookie.
#### Properties
- `name` The identifier of the cookie.
- `value` The value of the cookie.
- `expiry` A [Date ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date ) object of the cookies expiry date.
- `domain` The domain the cookie is valid for.
- `path` The path the cookie is valid for.
- `secure` A boolean value representing the cookie's secure attribute. If set the cookie will only be used for `https` requests.
- `subdomains` A boolean value specifying whether the cookie should be used for subdomains of the domain or not.
#### new Cookie(cookie, url)
- `cookie` The string representation of a cookie as send by a webserver.
- `url` The url the cookie has been received from.
#### static fromObject(obj)
Creates a cookie instance from an already existing object with the same properties.
#### serialize()
Serializes the cookie, transforming it to `name=value` so it can be used in requests.
#### hasExpired()
Returns whether the cookie has expired or not.
#### isValidForRequest(url)
Returns whether the cookie is valid for a request to `url` . If not, it won't be send by the fetch wrapper.
## License
This project is licensed under the MIT license, see [LICENSE ](LICENSE ).