Skip to content

Commit

Permalink
Merge pull request #53 from krushiraj/master
Browse files Browse the repository at this point in the history
feat: allow errors from setCookie to be ignored
  • Loading branch information
valeriangalliat authored Jun 16, 2020
2 parents 81beff8 + 55167f1 commit aad4924
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
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

0 comments on commit aad4924

Please sign in to comment.