flumm-fetch-cookies
A flumm-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.
Usage Example
import {fetch, CookieJar} from "flumm-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();
})();
Documentation
fetch(cookieJar, url, options)
cookieJarA CookieJar instance, an array of CookieJar instances or null, if you don't want to send or store cookies.urlandoptionsas in https://github.com/bitinn/node-fetch#fetchurl-options
Class: CookieJar
A class that stores cookies.
Properties
flagsThe read/write flags as specified below.fileThe path of the cookie jar on the disk.cookiesA Map mapping cookie names to their properties.
new CookieJar(flags[, file, cookies])
flagsA string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to fetch.r: only read from this jarw: only write to this jarrworwr: read/write from/to this jar
fileAn optional string containing a relative or absolute path to the file on the disk to use.cookiesAn optional initializer for the cookie jar - either an array of Cookie instances or a single Cookie instance.
addCookie(cookie[, url])
Adds a cookie to the jar.
cookieA Cookie instance to add to the cookie jar. Alternatively this can also be a string, for example the string received from a website. In this caseurlshould be specified.urlThe url a cookie has been received from.
forEach(callback)
Just a wrapper for CookieJar.cookies.forEach(callback).
save()
Saves the cookie jar to disk. Only non-expired cookies are saved.
Class: Cookie
An abstract representation of a cookie.
Properties
nameThe identifier of the cookie.valueThe value of the cookie.expiryA Date object of the cookies expiry date.domainThe domain the cookie is valid for.pathThe path the cookie is valid for.secureA boolean value representing the cookie's secure attribute. If set the cookie will only be used forhttpsrequests.subdomainsA boolean value specifying whether the cookie should be used for subdomains of the domain or not.
new Cookie(cookie, url)
cookieThe string representation of a cookie as send by a webserver.urlThe 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.