All Projects → jkhsjdhjs → node-fetch-cookies

jkhsjdhjs / node-fetch-cookies

Licence: MIT License
node-fetch wrapper that adds support for cookie-jars

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-fetch-cookies

Kkjsbridge
一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
Stars: ✭ 462 (+2980%)
Mutual labels:  fetch, cookie
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (+220%)
Mutual labels:  fetch, node-fetch
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+47740%)
Mutual labels:  fetch, node-fetch
node-fetch-har
Generate HAR entries for requests made with node-fetch
Stars: ✭ 23 (+53.33%)
Mutual labels:  fetch, node-fetch
node-http-client
🔌 A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients
Stars: ✭ 21 (+40%)
Mutual labels:  fetch, node-fetch
ultrafetch
Node-based fetch backed with an RFC-7234 compliant filesystem cache.
Stars: ✭ 30 (+100%)
Mutual labels:  fetch, node-fetch
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (+40%)
Mutual labels:  fetch
re-frame-fetch-fx
js/fetch Effect Handler for re-frame
Stars: ✭ 24 (+60%)
Mutual labels:  fetch
next-fauna-auth
Implemented cookie-based user authentication in a Next.js, FaunaDB and GraphQL app
Stars: ✭ 29 (+93.33%)
Mutual labels:  cookie
micell
A collection of functions for front-end development
Stars: ✭ 16 (+6.67%)
Mutual labels:  cookie
meros
🪢 A fast utility that makes reading multipart responses simple
Stars: ✭ 109 (+626.67%)
Mutual labels:  fetch
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (+640%)
Mutual labels:  cookie
rsfetch
A WIP rewrite of rsfetch from scratch.
Stars: ✭ 33 (+120%)
Mutual labels:  fetch
packages
Cloud Posse DevOps distribution of linux packages for native apps, binaries, alpine packages, debian packages, and redhat packages.
Stars: ✭ 107 (+613.33%)
Mutual labels:  fetch
javascript-cookie-control
Javascript module for controling cookie consent on your website
Stars: ✭ 18 (+20%)
Mutual labels:  cookie
summary1
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 13 (-13.33%)
Mutual labels:  cookie
ItroublveTSC
Official Source of ItroublveTSC, totally open source. No virus or anything. Feel free to have a look :)
Stars: ✭ 82 (+446.67%)
Mutual labels:  cookie
wumpfetch
🚀🔗 A modern, lightweight, fast and easy to use Node.js HTTP client
Stars: ✭ 20 (+33.33%)
Mutual labels:  fetch
CockyGrabber
C# library for the collection of browser information such as cookies, logins, and more
Stars: ✭ 46 (+206.67%)
Mutual labels:  cookie
http4s-dom
http4s, in a browser near you
Stars: ✭ 13 (-13.33%)
Mutual labels:  fetch

node-fetch-cookies Build Status

A 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.

For upgrading from 1.2.x or below to 1.3.x or above, please read the breaking API changes.

Usage Examples

with file...

import {fetch, CookieJar} from "node-fetch-cookies";

(async () => {
    // creates a CookieJar instance
    const cookieJar = new CookieJar("jar.json");

    // load cookies from the cookie jar
    await cookieJar.load();

    // usual fetch usage, except with one or multiple cookie jars as first parameter
    const response = await fetch(cookieJar, "https://example.com");

    // save the received cookies to disk
    await cookieJar.save();
})();

...or without

import {fetch, CookieJar} from "node-fetch-cookies";

(async () => {
    const cookieJar = new CookieJar();

    // 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");
})();

Exports

This module exports the following classes/functions:

  • fetch (default)
  • CookieJar
  • Cookie
  • CookieParseError
  • nodeFetch
  • Headers
  • Request
  • Response
  • FetchError
  • isRedirect: A function that accepts a number, more precisely an http status code as input, and returns, whether the status code is a redirect status code as a boolean.
    It is implemented in node-fetch and used by node-fetch-cookies. It is also exported here, because node-fetch exports it.

Documentation

async fetch(cookieJars, url[, options])

Returns a Promise resolving to a Response instance on success.

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 mapping hostnames to maps, which map cookie names to the respective Cookie instance.
  • cookieIgnoreCallback The callback function passed to new CookieJar(), that is called whenever a cookie couldn't be parsed.

new CookieJar([file, flags = rw, cookies, cookieIgnoreCallback])

  • file An optional string containing a relative or absolute path to the file on the disk to use.
  • flags An optional string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to fetch. Default: rw
    • r: only read from this jar
    • w: only write to this jar
    • rw or wr: read/write from/to this jar
  • cookies An optional initializer for the cookie jar - either an array of Cookie instances or a single Cookie instance.
  • cookieIgnoreCallback(cookie, reason) An optional callback function which will be called when a cookie is ignored instead of added to the cookie jar.
    • cookie The cookie string
    • reason A string containing the reason why the cookie has been ignored

addCookie(cookie[, fromURL])

Adds a cookie to the jar.

  • cookie A 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.

Returns true if the cookie has been added successfully. Returns false otherwise.
If the parser throws a CookieParseError, it will be caught and cookieIgnoreCallback will be called with the respective cookie string and error message.

domains()

Returns an iterator over all domains currently stored cookies for.

*cookiesDomain(domain)

Returns an iterator over all cookies currently stored for domain.

*cookiesValid(withSession)

Returns an iterator over all valid (non-expired) cookies.

  • withSession: A boolean. Iterator will include session cookies if set to true.

*cookiesAll()

Returns an iterator over all cookies currently stored.

*cookiesValidForRequest(requestURL)

Returns an iterator over all cookies valid for a request to url.

deleteExpired(sessionEnded)

Removes all expired cookies from the jar.

  • sessionEnded: A boolean. Also removes session cookies if set to true.

async load([file = this.file])

Reads cookies from file on the disk and adds the contained cookies.

  • file: Path to the file where the cookies should be saved. Default: this.file, the file that has been passed to the constructor.

async save([file = this.file])

Saves the cookie jar to file on the disk. Only non-expired non-session cookies are saved.

  • file: Path to the file where the cookies should be saved. Default: this.file, the file that has been passed to the constructor.

Class: Cookie

An abstract representation of a cookie.

Properties

  • name The identifier of the cookie.
  • value The value of the cookie.
  • expiry A Date object of the cookies expiry date or null, if the cookie expires with the session.
  • 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 requests to subdomains of domain or not.

new Cookie(str, requestURL)

Creates a cookie instance from the string representation of a cookie as send by a webserver.

  • str The string representation of a cookie.
  • url The url the cookie has been received from.

Will throw a CookieParseError if str couldn't be parsed.

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(sessionEnded)

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.

isValidForRequest(requestURL)

Returns whether the cookie is valid for a request to url.

Class: CookieParseError

The Error that is thrown when the cookie parser located in the constructor of the Cookie class is unable to parse the input.

1.3.0 Breaking API Changes

  • new CookieJar(flags, file, cookies) has been changed to new CookieJar(file, flags = "rw", cookies).
    new CookieJar("rw") can now be written as new CookieJar(), new CookieJar("rw", "jar.json") can now be written as new CookieJar("jar.json").
    This change has been introduced to simplify the usage of this library, since rw is used for flags in most cases anyways.
  • CookieJar.addFromFile(file) has been renamed to the async function async CookieJar.load([file = this.file]), which uses the fsPromises API for non-blocking cookie loading.
    The default value for file is the file passed to the constructor.
  • CookieJar.save(file) was moved to async CookieJar.save([file = this.file]) now also uses the fsPromises API.
  • new CookieJar() now doesn't load cookies from the specified file anymore. To do so, call await CookieJar.load() after creating the CookieJar.
    NOTE: CookieJar.load() will throw an error if the cookie jar doesn't exist or doesn't contain valid JSON!

License

This project is licensed under the MIT license, see LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].