@adobe/fetch
Advanced tools
Comparing version 0.2.1 to 0.2.2
54
index.js
@@ -17,3 +17,2 @@ /* | ||
const auth = require('@adobe/jwt-auth'); | ||
const merge = require('deepmerge'); | ||
const debug = require('debug')('@adobe/fetch'); | ||
@@ -48,24 +47,32 @@ const NO_CONFIG = 'Auth configuration missing.'; | ||
function addAuthHeaders(token, options, authOptions) { | ||
const tokenType = capFirst(token.token_type); | ||
let apiKey = authOptions.clientId; | ||
let xrequestid = uuid().replace(/-/g, ''); | ||
function generateRequestID() { | ||
return uuid().replace(/-/g, ''); | ||
} | ||
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 (options.headers['x-api-key']) { | ||
apiKey = options.headers['x-api-key']; | ||
if (typeof options.headers.entries === 'function') { | ||
// This is a headers object, iterate with for..of. | ||
for (let pair of options.headers.entries()) { | ||
const [name, value] = pair; | ||
headers[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]; | ||
} | ||
} | ||
if (options.headers['x-request-id']) { | ||
xrequestid = options.headers['x-request-id']; | ||
} | ||
} | ||
return merge(options, { | ||
headers: { | ||
authorization: `${tokenType} ${token.access_token}`, | ||
'x-api-key': apiKey, | ||
'x-request-id': xrequestid, | ||
'x-gw-ims-org-id': authOptions.orgId | ||
} | ||
}); | ||
headers.authorization = `${capFirst(token.token_type)} ${token.access_token}`; | ||
options.headers = headers; | ||
return options; | ||
} | ||
@@ -75,3 +82,3 @@ | ||
const token = await getToken(configOptions.auth, tokenCache, forceNewToken); | ||
const opts = addAuthHeaders(token, options, configOptions.auth); | ||
const opts = addHeaders(token, options, configOptions.headers); | ||
@@ -161,2 +168,11 @@ debug( | ||
const tokenCache = cache.config(configOptions.auth); | ||
configOptions.headers = configOptions.headers || {}; | ||
configOptions.headers = Object.assign( | ||
{ | ||
'x-api-key': configOptions.auth.clientId, | ||
'x-request-id': () => generateRequestID(), | ||
'x-gw-ims-org-id': configOptions.auth.orgId | ||
}, | ||
configOptions.headers | ||
); | ||
@@ -163,0 +179,0 @@ return (url, options = {}) => |
{ | ||
"name": "@adobe/fetch", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Call Adobe APIs", | ||
@@ -36,5 +36,4 @@ "main": "index.js", | ||
"debug": "^4.1.1", | ||
"deepmerge": "^4.0.0", | ||
"dotenv": "^8.1.0", | ||
"node-fetch": "^2.3.0", | ||
"node-fetch": "^2.6.0", | ||
"node-persist": "^3.0.5", | ||
@@ -44,5 +43,5 @@ "uuid": "^3.3.3" | ||
"devDependencies": { | ||
"eslint": "^6.2.1", | ||
"eslint-config-prettier": "^6.1.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"eslint": "^6.6.0", | ||
"eslint-config-prettier": "^6.5.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"jest": "^24.9.0", | ||
@@ -49,0 +48,0 @@ "prettier": "^1.18.2" |
@@ -52,5 +52,5 @@ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) | ||
#### Config object | ||
#### Config Auth object | ||
The `config` object is where you pass in all the required and optional parameters to authenticate API calls. | ||
The `config.auth` object is where you pass in all the required and optional parameters to authenticate API calls. | ||
@@ -110,5 +110,34 @@ | parameter | integration name | required | type | default | | ||
#### Predefined Headers | ||
If you have HTTP headers that are required for each request, you can provide them in the configuration. | ||
They will be then added automatically to each request. | ||
You can provide either a value or a function. | ||
A function can be used when you need to generate a dynamic header value on each request. | ||
For example: | ||
```javascript | ||
const config = { | ||
auth: { | ||
... Auth Configuration ... | ||
}, | ||
headers: { | ||
'x-sandbox-name': 'prod', | ||
'x-request-id': () => idGenerationFunc() | ||
} | ||
}; | ||
``` | ||
The following headers are added automatically by adobe-fetch, you can override all of them besides **authorization***[]: | ||
- authorization | ||
- x-api-key | ||
- x-request-id | ||
- x-gw-ims-org-id | ||
#### Custom Storage | ||
By default, [node-persist](https://github.com/bitinn/node-persist) is used to store all the active tokens locally. | ||
By default, [node-persist](https://github.com/simonlast/node-persist) is used to store all the active tokens locally. | ||
Tokens will be stored under **/.node-perist/storage** | ||
@@ -186,3 +215,3 @@ | ||
Contributions are welcomed! Read the [Contributing Guide](.github/CONTRIBUTING.MD) for more information. | ||
Contributions are welcomed! Read the [Contributing Guide](.github/CONTRIBUTING.md) for more information. | ||
@@ -189,0 +218,0 @@ ### Licensing |
@@ -147,2 +147,12 @@ /* | ||
test('returns response when fetch returns 444', async () => { | ||
fetch.mockImplementation(() => | ||
Promise.resolve(mockData.responseUnauthorizedOther) | ||
); | ||
const res = await testFetch(mockData.url); | ||
expect(res).toBeDefined(); | ||
expect(res.status).toBe(444); | ||
expect(res.ok).toBe(false); | ||
}); | ||
test('allows x-api-key override', async () => { | ||
@@ -149,0 +159,0 @@ expect.assertions(5); |
@@ -42,2 +42,8 @@ /* | ||
}, | ||
responseUnauthorizedOther: { | ||
url: MOCK_URL, | ||
status: 444, | ||
statusText: 'Unauthorized', | ||
ok: false | ||
}, | ||
config: { | ||
@@ -44,0 +50,0 @@ clientId: CLIENT_ID, |
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
78852
6
1020
218
17
- Removeddeepmerge@^4.0.0
- Removeddeepmerge@4.3.1(transitive)
Updatednode-fetch@^2.6.0