@adobe/fetch
Advanced tools
Comparing version 0.1.1 to 0.1.2
43
index.js
@@ -19,9 +19,5 @@ /* | ||
async function getToken(options, tokenCache, forceNewToken) { | ||
if (!options.auth || !options.auth.clientId || !options.auth.metaScopes) { | ||
throw NO_CONFIG; | ||
} | ||
async function getToken(authOptions, tokenCache, forceNewToken) { | ||
const key = authOptions.clientId + '|' + authOptions.metaScopes.join(','); | ||
const key = options.auth.clientId + '|' + options.auth.metaScopes.join(','); | ||
let token = await tokenCache.get(key); | ||
@@ -33,7 +29,7 @@ | ||
try { | ||
token = await auth(options.auth); | ||
token = await auth(authOptions); | ||
if (token) { | ||
return tokenCache.set(key, token); | ||
} else { | ||
throw 'Access token came empty.'; | ||
throw 'Access token empty'; | ||
} | ||
@@ -47,8 +43,8 @@ } catch (err) { | ||
function addAuthHeaders(token, options) { | ||
function addAuthHeaders(token, options, authOptions) { | ||
return merge(options, { | ||
headers: { | ||
authorization: `${token.token_type} ${token.access_token}`, | ||
'x-api-key': options.auth.clientId, | ||
'x-gw-ims-org-id': options.auth.orgId | ||
'x-api-key': authOptions.clientId, | ||
'x-gw-ims-org-id': authOptions.orgId | ||
} | ||
@@ -58,9 +54,9 @@ }); | ||
async function _fetch(url, options, tokenCache, forceNewToken) { | ||
const token = await getToken(options, tokenCache, forceNewToken); | ||
const opts = addAuthHeaders(token, options); | ||
async function _fetch(url, options, configOptions, tokenCache, forceNewToken) { | ||
const token = await getToken(configOptions.auth, tokenCache, forceNewToken); | ||
const opts = addAuthHeaders(token, options, configOptions.auth); | ||
const res = await fetch(url, opts); | ||
if ((res.status === 401 || res.status === 403) && !forceNewToken) { | ||
return await _fetch(url, options, tokenCache, true); | ||
return await _fetch(url, options, configOptions, tokenCache, true); | ||
} else { | ||
@@ -78,4 +74,4 @@ return res; | ||
*/ | ||
function adobefetch(url, options = {}, tokenCache) { | ||
return _fetch(url, options, tokenCache, false); | ||
function adobefetch(url, options = {}, configOptions, tokenCache) { | ||
return _fetch(url, options, configOptions, tokenCache, false); | ||
} | ||
@@ -100,5 +96,16 @@ | ||
!metaScopes || metaScopes.length === 0 ? errors.push('metaScopes') : ''; | ||
if (errors.length > 0) { | ||
throw `Required parameter(s) ${errors.join(', ')} are missing`; | ||
} | ||
if ( | ||
!( | ||
typeof privateKey === 'string' || | ||
privateKey instanceof Buffer || | ||
ArrayBuffer.isView(privateKey) | ||
) | ||
) { | ||
throw 'Required parameter privateKey is invalid'; | ||
} | ||
} | ||
@@ -116,5 +123,5 @@ | ||
return (url, options = {}) => | ||
adobefetch(url, merge(configOptions, options), tokenCache); | ||
adobefetch(url, options, configOptions, tokenCache); | ||
} | ||
module.exports = { config: config }; |
{ | ||
"name": "@adobe/fetch", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Call Adobe APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -91,2 +91,19 @@ /* | ||
}); | ||
test('privateKey is of wrong type', () => { | ||
expect.assertions(1); | ||
const privateKey = new Object(); | ||
return expect(() => | ||
adobefetch.config({ | ||
auth: { | ||
clientId, | ||
clientSecret, | ||
technicalAccountId, | ||
orgId, | ||
metaScopes, | ||
privateKey | ||
} | ||
}) | ||
).toThrow('Required parameter privateKey is invalid'); | ||
}); | ||
}); |
@@ -150,2 +150,31 @@ /* | ||
test('token not stored if storage disabled', async () => { | ||
let setItemCalled = false; | ||
let getItemCalled = false; | ||
storage.getItem = jest.fn(() => { | ||
getItemCalled = true; | ||
return Promise.resolve({}); | ||
}); | ||
storage.setItem = jest.fn(() => { | ||
setItemCalled = true; | ||
return Promise.resolve(); | ||
}); | ||
testFetch = adobefetch.config({ | ||
auth: Object.assign({ disableStorage: true }, mockData.config) | ||
}); | ||
await testFetch(mockData.url); | ||
expect(setItemCalled).toBe(false); | ||
expect(getItemCalled).toBe(false); | ||
}); | ||
test('throws error if token is empty', () => { | ||
expect.assertions(1); | ||
const ERROR = 'Access token empty'; | ||
auth.mockImplementation(async () => undefined); | ||
fetch.mockImplementation(() => { | ||
return Promise.resolve({ status: 200 }); | ||
}); | ||
return expect(testFetch(mockData.url)).rejects.toEqual(ERROR); | ||
}); | ||
test('rethrows JWT errors', () => { | ||
@@ -152,0 +181,0 @@ expect.assertions(1); |
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
83853
655
9