@adobe/fetch
Advanced tools
Comparing version 0.2.3 to 0.2.4
72
index.js
@@ -50,50 +50,65 @@ /* | ||
function addHeaders(token, options, predefinedHeaders) { | ||
let headers = {}; | ||
for (let name in predefinedHeaders) { | ||
const value = predefinedHeaders[name]; | ||
headers[name.toLowerCase()] = typeof value === 'function' ? value() : value; | ||
} | ||
if (options.headers) { | ||
if (typeof options.headers.entries === 'function') { | ||
function normalizeHeaders(headers) { | ||
let normalized = {}; | ||
if (headers) { | ||
if (typeof headers.entries === 'function') { | ||
// This is a headers object, iterate with for..of. | ||
for (let pair of options.headers.entries()) { | ||
for (let pair of headers.entries()) { | ||
const [name, value] = pair; | ||
headers[name.toLowerCase()] = value; | ||
normalized[name.toLowerCase()] = value; | ||
} | ||
} else { | ||
// This is a normal JSON. Iterate with for.. in | ||
for (let name in options.headers) { | ||
headers[name.toLowerCase()] = options.headers[name]; | ||
for (let name in headers) { | ||
normalized[name.toLowerCase()] = headers[name]; | ||
} | ||
} | ||
} | ||
return normalized; | ||
} | ||
function calculateHeaders(predefinedHeaders) { | ||
let headers = {}; | ||
for (let name in predefinedHeaders) { | ||
const value = predefinedHeaders[name]; | ||
if (typeof value === 'function') { | ||
headers[name] = value(); | ||
} else { | ||
headers[name] = value; | ||
} | ||
} | ||
return headers; | ||
} | ||
function getHeaders(token, options, predefinedHeaders) { | ||
let headers = calculateHeaders(predefinedHeaders); | ||
if (options && options.headers) { | ||
headers = Object.assign(headers, normalizeHeaders(options.headers)); | ||
} | ||
headers.authorization = `${capFirst(token.token_type)} ${token.access_token}`; | ||
options.headers = headers; | ||
return options; | ||
return headers; | ||
} | ||
async function _fetch(url, options, configOptions, tokenCache, forceNewToken) { | ||
async function _fetch(url, opts, configOptions, tokenCache, forceNewToken) { | ||
const token = await getToken(configOptions.auth, tokenCache, forceNewToken); | ||
const opts = addHeaders(token, options, configOptions.headers); | ||
const fetchOpts = Object.assign({}, opts); | ||
fetchOpts.headers = getHeaders(token, opts, configOptions.headers); | ||
debug( | ||
`${opts.method || 'GET'} ${url} - x-request-id: ${ | ||
opts.headers['x-request-id'] | ||
`${fetchOpts.method || 'GET'} ${url} - x-request-id: ${ | ||
fetchOpts.headers['x-request-id'] | ||
}` | ||
); | ||
const res = await fetch(url, opts); | ||
const res = await fetch(url, fetchOpts); | ||
if (!res.ok) { | ||
debug( | ||
`${opts.method || 'GET'} ${url} - status ${res.statusText} (${ | ||
`${fetchOpts.method || 'GET'} ${url} - status ${res.statusText} (${ | ||
res.status | ||
}). x-request-id: ${opts.headers['x-request-id']}` | ||
}). x-request-id: ${fetchOpts.headers['x-request-id']}` | ||
); | ||
if ((res.status === 401 || res.status === 403) && !forceNewToken) { | ||
debug(`${opts.method || 'GET'} ${url} - Will get new token.`); | ||
return await _fetch(url, options, configOptions, tokenCache, true); | ||
return await _fetch(url, opts, configOptions, tokenCache, true); | ||
} | ||
@@ -166,3 +181,2 @@ } | ||
const tokenCache = cache.config(configOptions.auth); | ||
configOptions.headers = configOptions.headers || {}; | ||
configOptions.headers = Object.assign( | ||
@@ -174,3 +188,3 @@ { | ||
}, | ||
configOptions.headers | ||
normalizeHeaders(configOptions.headers) | ||
); | ||
@@ -182,2 +196,6 @@ | ||
module.exports = { config: config }; | ||
module.exports = { | ||
config: config, | ||
normalizeHeaders: normalizeHeaders, | ||
generateRequestID: generateRequestID | ||
}; |
{ | ||
"name": "@adobe/fetch", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Call Adobe APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
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
79694
1038