Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow errors from setCookie to be ignored #53

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ esentially two different HTTP clients with different login sessions on you backe

All calls to `fetch` will store and send back cookies according to the URL.

> 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:

```ts
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;
}
```

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
jar = jar || new tough.CookieJar()

Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports = function fetchCookieDecorator (fetch, jar) {
}

// 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 })))

return res
}
Expand Down
2 changes: 1 addition & 1 deletion node-fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CookieJar } from './';

declare function c(fetch: Function, jar?: CookieJar): Function;
declare function c(fetch: Function, jar?: CookieJar, ignoreError: boolean): Function;

export = c;
9 changes: 9 additions & 0 deletions test/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ app.get('/redirect', (req, res) => {
res.redirect('http://localhost:9998/get') // FIXME: There is nothing at this port ...
})

app.get('/cookie', (req, res) => {
res.setHeader(
'set-cookie',
'my_cookie=HelloWorld; path=/; domain=www.example.com; secure; HttpOnly; SameSite=Lax'
);
res.cookie('tuna', 'can');
res.end()
});

module.exports = app
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ describe('fetch-cookie', () => {
assert.notStrictEqual(cookie1.key, cookie2.key)
})

it("should ignore error when there is error in setCookie", async () => {
const jar = new CookieJar();
const fetch = require('../index')(nodeFetch, jar);
let error = null;
try {
await fetch('http://localhost:9999/cookie');
} catch (err) {
error = err;
}
assert.isNull(error);
});

it('should throw error when there is error in setCookie', async () => {
const jar = new CookieJar();
const fetch = require('../index')(nodeFetch, jar, false);
let error = null;
try {
await fetch('http://localhost:9999/cookie');
} catch (err) {
error = err;
}
assert.instanceOf(error, Error)
});

after('stop test server', () => {
if (server) { server.close() }
})
Expand Down