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-08-15 18:48:12 +00:00
## Usage Examples
### with file...
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
2019-08-15 18:48:12 +00:00
const cookieJar = new CookieJar("rw", "jar.json");
2019-06-15 22:06:28 +00:00
// usual fetch usage, except with one or multiple cookie jars as first parameter
2019-08-15 18:48:12 +00:00
const response = await fetch(cookieJar, "https://example.com");
2019-06-15 22:06:28 +00:00
// save the received cookies to disk
cookieJar.save();
})();
2019-01-14 03:46:38 +00:00
```
2019-06-15 22:06:28 +00:00
2019-08-15 18:48:12 +00:00
### ...or without
```javascript
import {fetch, CookieJar} from "node-fetch-cookies";
(async () => {
const cookieJar = new CookieJar("rw");
// log in to some api
let response = await fetch(cookieJar, "https://example.com/api/login", {
method: "POST",
body: "credentials"
});
// do some requests you require login for
response = await fetch(cookieJar, "https://example.com/api/admin/drop-all-databases");
// and optionally log out again
response = await fetch(cookieJar, "https://example.com/api/logout");
})();
```
2019-06-15 22:06:28 +00:00
## Documentation
2019-08-15 18:48:12 +00:00
### async 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
2019-08-15 18:48:12 +00:00
Returns a Promise resolving to a [Response ](https://github.com/bitinn/node-fetch#class-response ) instance on success.
2019-06-15 22:06:28 +00:00
### 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.
2019-08-15 18:48:12 +00:00
- `cookies` A [Map ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map ) mapping hostnames to maps, which map cookie names to the respective [Cookie ](#class-cookie ) instance.
2019-06-15 22:06:28 +00:00
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.
2019-08-15 18:48:12 +00:00
#### addCookie(cookie[, fromURL])
2019-06-15 22:06:28 +00:00
Adds a cookie to the jar.
2019-08-15 18:48:12 +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 `fromURL` must be specified.
- `fromURL` The url a cookie has been received from.
2019-06-15 22:06:28 +00:00
2019-08-15 11:32:49 +00:00
Returns `true` if the cookie has been added successfully. Returns `false` otherwise.
Will log a warning to console if a cookie fails to be parsed.
2019-08-13 20:19:25 +00:00
#### addFromFile(file)
2019-08-15 18:48:12 +00:00
Reads cookies from `file` on the disk and adds the contained cookies.
2019-08-13 20:19:25 +00:00
#### 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 23:06:22 +00:00
#### *cookiesValid(withSession)
2019-08-15 18:48:12 +00:00
Returns an iterator over all valid (non-expired) cookies.
- `withSession` : A boolean. Iterator will include session cookies if set to `true` .
2019-08-13 20:19:25 +00:00
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
2019-08-15 11:32:49 +00:00
#### deleteExpired(sessionEnded)
2019-08-15 18:48:12 +00:00
Removes all expired cookies from the jar.
- `sessionEnded` : A boolean. Also removes session cookies if set to `true` .
2019-06-15 22:06:28 +00:00
#### save()
2019-08-14 23:06:22 +00:00
Saves the cookie jar to disk. Only non-expired non-session cookies are saved.
2019-06-15 22:06:28 +00:00
### Class: Cookie
An abstract representation of a cookie.
#### Properties
- `name` The identifier of the cookie.
- `value` The value of the cookie.
2019-08-14 23:06:22 +00:00
- `expiry` A [Date ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date ) object of the cookies expiry date or `null` , if the cookie expires with the session.
2019-06-15 22:06:28 +00:00
- `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.
2019-08-14 23:06:22 +00:00
- `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not.
2019-06-15 22:06:28 +00:00
2019-08-15 18:48:12 +00:00
#### new Cookie(str, url)
Creates a cookie instance from the string representation of a cookie as send by a webserver.
- `str` The string representation of a cookie.
2019-06-15 22:06:28 +00:00
- `url` The url the cookie has been received from.
2019-08-15 18:48:12 +00:00
Will throw a `CookieParseError` if `str` couldn't be parsed.
2019-08-15 11:32:49 +00:00
2019-06-15 22:06:28 +00:00
#### 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.
2019-08-14 23:06:22 +00:00
#### hasExpired(sessionEnded)
2019-08-15 18:48:12 +00:00
Returns whether the cookie has expired or not.
- `sessionEnded` : A boolean that specifies whether the current session has ended, meaning if set to `true` , the function will return `true` for session cookies.
2019-06-15 22:06:28 +00:00
#### isValidForRequest(url)
2019-08-15 18:48:12 +00:00
Returns whether the cookie is valid for a request to `url` .
2019-06-15 22:06:28 +00:00
2019-08-15 11:32:49 +00:00
2019-06-15 22:06:28 +00:00
## License
This project is licensed under the MIT license, see [LICENSE ](LICENSE ).