fetch-cookie
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -9,2 +9,10 @@ # Changelog | ||
## [2.0.1] - 2022-03-03 | ||
* Fix TypeScript types. ([#68]) | ||
* Because we can't specify different types for ESM and CJS, I chose to | ||
drop CJS support for TypeScript as I assume most TypeScript users will | ||
use this module with ESM. You can still `import fetchCookie = require('fetch-cookie')` | ||
but the type will be wrong (it's a function, not an object with a | ||
`default` property as TypeScript assumes). | ||
## [2.0.0] - 2022-02-17 | ||
@@ -17,2 +25,6 @@ * Rewrite in TypeScript. ([#43]) | ||
node-fetch implementation. ([#11], [#39], [#42], [#57]) | ||
* **Breaking:** the redirect logic is now included in the main | ||
export and the node-fetch wrapper (`require('fetch-cookie/node-fetch')`) | ||
was removed. Just `require('node-fetch')` and you're good to go with | ||
redirects! | ||
@@ -19,0 +31,0 @@ ## [1.0.1] - 2022-02-09 |
@@ -206,4 +206,4 @@ var __create = Object.create; | ||
function fetchCookie(fetch, jar, ignoreError = true) { | ||
fetch = fetch || globalThis.fetch; | ||
jar = jar || new tough.CookieJar(); | ||
const actualFetch = fetch; | ||
const actualJar = jar != null ? jar : new tough.CookieJar(); | ||
function fetchCookieWrapper(input, init) { | ||
@@ -215,8 +215,8 @@ return __async(this, null, function* () { | ||
const requestUrl = typeof input === "string" ? input : input.url; | ||
const cookie = yield jar.getCookieString(requestUrl); | ||
const cookie = yield actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = yield fetch(input, init); | ||
const response = yield actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
yield Promise.all(cookies.map((cookie2) => __async(this, null, function* () { | ||
return yield jar.setCookie(cookie2, response.url, { ignoreError }); | ||
return yield actualJar.setCookie(cookie2, response.url, { ignoreError }); | ||
}))); | ||
@@ -223,0 +223,0 @@ if (((_a = init.redirectCount) != null ? _a : 0) > 0) { |
@@ -177,4 +177,4 @@ var __defProp = Object.defineProperty; | ||
function fetchCookie(fetch, jar, ignoreError = true) { | ||
fetch = fetch || globalThis.fetch; | ||
jar = jar || new tough.CookieJar(); | ||
const actualFetch = fetch; | ||
const actualJar = jar != null ? jar : new tough.CookieJar(); | ||
function fetchCookieWrapper(input, init) { | ||
@@ -186,8 +186,8 @@ return __async(this, null, function* () { | ||
const requestUrl = typeof input === "string" ? input : input.url; | ||
const cookie = yield jar.getCookieString(requestUrl); | ||
const cookie = yield actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = yield fetch(input, init); | ||
const response = yield actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
yield Promise.all(cookies.map((cookie2) => __async(this, null, function* () { | ||
return yield jar.setCookie(cookie2, response.url, { ignoreError }); | ||
return yield actualJar.setCookie(cookie2, response.url, { ignoreError }); | ||
}))); | ||
@@ -194,0 +194,0 @@ if (((_a = init.redirectCount) != null ? _a : 0) > 0) { |
{ | ||
"name": "fetch-cookie", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Decorator for a `fetch` function to support automatic cookies.", | ||
@@ -22,3 +22,3 @@ "license": "Unlicense", | ||
"type-check": "tsc -noEmit src/*.ts", | ||
"type-declarations": "tsc --declaration --emitDeclarationOnly src/*.ts --outDir types && echo 'export = fetchCookie;' >> types/index.d.ts" | ||
"type-declarations": "tsc --declaration --emitDeclarationOnly src/*.ts --outDir types" | ||
}, | ||
@@ -25,0 +25,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# 🍪 fetch-cookie [![npm version](http://img.shields.io/npm/v/fetch-cookie.svg?style=flat-square)](https://www.npmjs.org/package/fetch-cookie) [![Build status](https://img.shields.io/github/workflow/status/valeriangalliat/fetch-cookie/Test)](https://github.com/valeriangalliat/fetch-cookie/actions/workflows/test.yml) | ||
# 🍪 fetch-cookie [![npm version](https://img.shields.io/npm/v/fetch-cookie?style=flat-square)](https://www.npmjs.org/package/fetch-cookie) [![Build status](https://img.shields.io/github/workflow/status/valeriangalliat/fetch-cookie/Test?style=flat-square)](https://github.com/valeriangalliat/fetch-cookie/actions/workflows/test.yml) | ||
@@ -6,2 +6,4 @@ > Decorator for a `fetch` function to support automatic cookie storage | ||
[Migrating from v1](#migrating-from-v1) | ||
## Description | ||
@@ -129,2 +131,24 @@ | ||
## Migrating from v1 | ||
The only breaking change with v2 is that the node-fetch wrapper (that | ||
was handling redirects only with node-fetch nonstandard APIs) was | ||
dropped and the redirects are now always handled by the main export. | ||
```js | ||
// If you were doing | ||
const fetchCookie = require('fetch-cookie/node-fetch') | ||
// Change it to | ||
const fetchCookie = require('fetch-cookie') | ||
// Or | ||
import fetchCookie from 'fetch-cookie' | ||
``` | ||
This also means that if you were *not* using the node-fetch wrapper and | ||
were happy about cookies *not* being included in redirects, cookies are | ||
now going to be stored and sent in redirects as well like they would in | ||
the browser. | ||
## Development | ||
@@ -131,0 +155,0 @@ |
import * as tough from 'tough-cookie'; | ||
declare type FetchImpl = typeof fetch; | ||
declare type FetchCookieInit = RequestInit & { | ||
interface GenericRequest { | ||
url: string; | ||
} | ||
declare type GenericRequestInfo = GenericRequest | string; | ||
interface GenericRequestInit { | ||
method?: string; | ||
redirect?: string; | ||
body?: any; | ||
referrerPolicy?: string; | ||
} | ||
interface GenericResponse { | ||
url: string; | ||
status: number; | ||
headers: { | ||
get: (name: string) => string | null; | ||
has: (name: string) => boolean; | ||
}; | ||
} | ||
declare type FetchCookieInit<T extends GenericRequestInit> = T & { | ||
maxRedirect?: number; | ||
redirectCount?: number; | ||
}; | ||
interface FetchCookieImpl { | ||
(input: RequestInfo, init?: FetchCookieInit): Promise<Response>; | ||
declare type GenericFetch<T1 extends GenericRequestInfo, T2 extends GenericRequestInit, T3> = (input: T1, init?: T2) => Promise<T3>; | ||
interface FetchCookieImpl<T1 extends GenericRequestInfo, T2 extends GenericRequestInit, T3> { | ||
(input: T1, init?: FetchCookieInit<T2>): Promise<T3>; | ||
toughCookie: typeof tough; | ||
} | ||
declare function fetchCookie(fetch: FetchImpl, jar: tough.CookieJar, ignoreError?: boolean): FetchCookieImpl; | ||
interface CookieJar { | ||
getCookieString: (currentUrl: string) => Promise<string>; | ||
setCookie: (cookieString: string, currentUrl: string, opts: { | ||
ignoreError: boolean; | ||
}) => Promise<any>; | ||
} | ||
declare function fetchCookie<T1 extends GenericRequestInfo, T2 extends GenericRequestInit, T3 extends GenericResponse>(fetch: GenericFetch<T1, T2, T3>, jar?: CookieJar, ignoreError?: boolean): FetchCookieImpl<T1, T2, T3>; | ||
declare namespace fetchCookie { | ||
@@ -16,2 +40,1 @@ var toughCookie: typeof tough; | ||
export default fetchCookie; | ||
export = fetchCookie; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33053
10
482
184
2