You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1.2 KiB
29 lines
1.2 KiB
4 years ago
|
module.exports = function nodeFetchCookieDecorator (nodeFetch, jar) {
|
||
|
const fetchCookie = require('./')(nodeFetch, jar)
|
||
|
|
||
|
return function nodeFetchCookie (url, userOptions = {}) {
|
||
|
const opts = Object.assign({}, userOptions, { redirect: 'manual' })
|
||
|
|
||
|
// Forward identical options to wrapped node-fetch but tell to not handle redirection.
|
||
|
return fetchCookie(url, opts)
|
||
|
.then(res => {
|
||
|
const isRedirect = (res.status === 303 || ((res.status === 301 || res.status === 302)))
|
||
|
|
||
|
// 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,
|
||
|
// 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)
|
||
|
} else {
|
||
|
return res
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|