forked from valeriangalliat/fetch-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-fetch.js
25 lines (24 loc) · 1.29 KB
/
node-fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = function nodeFetchCookieDecorator(nodeFetch, jar) {
var fetchCookie = require('./index.js')(nodeFetch, jar);
return function nodeFetchCookie(url, opts) {
// forward identical options to wrapped node-fetch but tell to not handle redirection
return fetchCookie(url, Object.assign({}, opts, { redirect: 'manual' }))
.then(function (res) {
var isRedirect = (res.status === 303
|| ((res.status === 301 || res.status === 302) && opts.method === 'POST'));
// interpret the proprietary "redirect" option in the same way that node-fetch does
if (isRedirect && opts.redirect !== 'manual' && opts.follow !== 0) {
var optsForGet = Object.assign({}, {
method: 'GET',
body: null,
// since the "follow" flag is not relevant for node-fetch in this case,
// we'll hijack it for our internal bookkeeping
follow: opts.follow !== undefined ? opts.follow - 1 : undefined
});
return nodeFetchCookie(res.headers._headers.location[0], optsForGet);
} else {
return res;
}
});
}
}