fetch-cookie
Advanced tools
Comparing version 0.9.1 to 0.10.0
declare namespace c { | ||
interface CookieJar { | ||
getCookieString(currentUrl: string, cb: (err: any, cookies: string) => void): void; | ||
setCookie(cookieString: string, currentUrl: string, cb: (err: any) => void): void; | ||
setCookie(cookieString: string, currentUrl: string, cb: (err: any) => void, opts: { ignoreError: boolean }): void; | ||
} | ||
} | ||
declare function c(fetch: Function, jar?: c.CookieJar): Function; | ||
declare function c(fetch: Function, jar?: c.CookieJar, ignoreError: boolean): Function; | ||
export = c; |
const { promisify } = require('util') | ||
const tough = require('tough-cookie') | ||
module.exports = function fetchCookieDecorator (fetch, jar) { | ||
module.exports = function fetchCookieDecorator (fetch, jar, ignoreError = true) { | ||
fetch = fetch || window.fetch | ||
@@ -48,3 +48,3 @@ jar = jar || new tough.CookieJar() | ||
// Store all present cookies | ||
await Promise.all(cookies.map((cookie) => setCookie(cookie, res.url))) | ||
await Promise.all(cookies.map((cookie) => setCookie(cookie, res.url, { ignoreError }))) | ||
@@ -51,0 +51,0 @@ return res |
import { CookieJar } from './'; | ||
declare function c(fetch: Function, jar?: CookieJar): Function; | ||
declare function c(fetch: Function, jar?: CookieJar, ignoreError: boolean): Function; | ||
export = c; |
@@ -10,15 +10,20 @@ module.exports = function nodeFetchCookieDecorator (nodeFetch, jar) { | ||
.then(res => { | ||
const isRedirect = (res.status === 303 || ((res.status === 301 || res.status === 302))) | ||
const isRedirect = (res.status === 303 || res.status === 301 || res.status === 302 || res.status === 307) | ||
// Interpret the proprietary "redirect" option in the same way that node-fetch does. | ||
if (isRedirect && userOptions.redirect !== 'manual' && userOptions.follow !== 0) { | ||
const optsForGet = Object.assign({}, { | ||
method: 'GET', | ||
body: null, | ||
const statusOpts = { | ||
// Since the "follow" flag is not relevant for node-fetch in this case, | ||
// we'll hijack it for our internal bookkeeping. | ||
follow: userOptions.follow !== undefined ? userOptions.follow - 1 : undefined | ||
}) | ||
} | ||
return nodeFetchCookie(res.headers.get('location'), optsForGet) | ||
if (res.status !== 307) { | ||
statusOpts.method = 'GET' | ||
statusOpts.body = null | ||
} | ||
const redirectOpts = Object.assign({}, userOptions, statusOpts) | ||
return nodeFetchCookie(res.headers.get('location'), redirectOpts) | ||
} else { | ||
@@ -25,0 +30,0 @@ return res |
{ | ||
"name": "fetch-cookie", | ||
"version": "0.9.1", | ||
"version": "0.10.0", | ||
"description": "Decorator for a `fetch` function to support automatic cookies.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/valeriangalliat/fetch-cookie", |
@@ -47,2 +47,12 @@ # fetch-cookie [![npm version](https://badge.fury.io/js/fetch-cookie.svg)](https://badge.fury.io/js/fetch-cookie) [![Build Status](https://travis-ci.org/valeriangalliat/fetch-cookie.svg?branch=master)](https://travis-ci.org/valeriangalliat/fetch-cookie) | ||
> Note: All errors when setting cookies are ignored by default. You can make it to throw errors in cookies by passing a third argument (default is true). | ||
```js | ||
const nodeFetch = require('node-fetch') | ||
const tough = require('tough-cookie') | ||
const fetch = require('fetch-cookie')(nodeFetch, new tough.CookieJar(), false) // default value is true | ||
// false - doesn't ignore errors, throws when an error occurs in setting cookies and breaks the request and execution | ||
// true - silently ignores errors and continues to make requests/redirections | ||
``` | ||
If you use a cookie jar that is not tough-cookie, keep in mind that it must implement this interface to be compatible: | ||
@@ -53,3 +63,3 @@ | ||
getCookieString(currentUrl: string, cb: (err: any, cookies: string) => void): void; | ||
setCookie(cookieString: string, currentUrl: string, cb: (err: any) => void): void; | ||
setCookie(cookieString: string, currentUrl: string, cb: (err: any) => void, opts: { ignoreError:boolean }): void; | ||
} | ||
@@ -56,0 +66,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9669
82
84