@ambassify/ambassify-client
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -14,2 +14,8 @@ # Changelog | ||
## 5.0.2 | ||
#### :bug: Bug Fix | ||
- Add test support for node v18 | ||
## 5.0.1 | ||
@@ -133,1 +139,13 @@ | ||
"use strict"; | ||
const _get = require('lodash/get'); | ||
const _set = require('lodash/set'); | ||
const serializeFields = require('@ambassify/aql-serializer').fields; | ||
const FetchApi = require('@ambassify/fetch-api'); | ||
const AmbassifyError = require('@ambassify/error'); | ||
const jwt = require('./jwt'); | ||
const { | ||
@@ -20,5 +14,3 @@ isRefreshEnabled, | ||
} = require('./refresh-token'); | ||
FetchApi.defineContentType('JSON', require('@ambassify/fetch-api/lib/content-type/json')); | ||
class AmbassifyApi extends FetchApi { | ||
@@ -59,3 +51,2 @@ static impersonate() { | ||
} | ||
const { | ||
@@ -79,3 +70,2 @@ access_token, | ||
} | ||
static addAuthenticationOptions(options) { | ||
@@ -89,2 +79,3 @@ // If no token is set we don't need to update any options | ||
} = options; | ||
/** | ||
@@ -96,3 +87,2 @@ * If `tokenType` is set to `Cookie` we must set the `credentials` | ||
*/ | ||
if (tokenType.toLowerCase() == 'cookie') { | ||
@@ -105,3 +95,2 @@ tokenType = 'Basic'; | ||
} | ||
headers = { | ||
@@ -111,3 +100,4 @@ authorization: `${tokenType} ${options.token}`, | ||
}; | ||
return { ...options, | ||
return { | ||
...options, | ||
headers, | ||
@@ -117,3 +107,2 @@ fetchOptions | ||
} | ||
constructor() { | ||
@@ -126,3 +115,2 @@ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
}; | ||
if (retry !== false) { | ||
@@ -135,4 +123,4 @@ retry = { | ||
} | ||
super({ ...options, | ||
super({ | ||
...options, | ||
contentType: options.contentType || FetchApi.ContentTypes.JSON, | ||
@@ -143,25 +131,18 @@ retry | ||
} | ||
_stringifyFields(fields) { | ||
return serializeFields(fields); | ||
} | ||
_getResources(resourceName, response) { | ||
const resources = _get(response, 'body._embedded', {}); | ||
return resources[resourceName] || []; | ||
} | ||
_getNextUrl(url, response) { | ||
return _get(response, 'body._links.next.href'); | ||
} | ||
refreshTokenEnabled() { | ||
return isRefreshEnabled(this.options); | ||
} | ||
refreshToken() { | ||
return doRefreshToken(this.fetch, this.options).then(res => this._updateAuthentication(res)); | ||
} | ||
_updateAuthentication(options) { | ||
@@ -176,8 +157,5 @@ const { | ||
} | ||
_request(url, options) { | ||
if (!this.refreshTokenEnabled()) return super._request(url, options); | ||
const authorization = _get(options, 'headers.authorization', ''); | ||
const [, token] = /^Bearer\s+(.*)/i.exec(authorization) || []; | ||
@@ -188,16 +166,13 @@ if (!token) return super._request(url, options); | ||
if (!data || !data.exp || data.exp > now + 60) return super._request(url, options); | ||
return doCachedRefreshToken(this.fetch, { ...this.options, | ||
return doCachedRefreshToken(this.fetch, { | ||
...this.options, | ||
token | ||
}).then(res => { | ||
this._updateAuthentication(res); | ||
_set(options, 'headers.authorization', 'Bearer ' + res.token); | ||
return super._request(url, options); | ||
}); | ||
} | ||
request(method, url, query, body) { | ||
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
if (options.fields) { | ||
@@ -208,3 +183,4 @@ const { | ||
} = options; | ||
query = { ...query, | ||
query = { | ||
...query, | ||
fields: this._stringifyFields(fields) | ||
@@ -214,3 +190,2 @@ }; | ||
} | ||
const promise = super.request(method, url, query, body, options); | ||
@@ -224,5 +199,3 @@ promise.hal = { | ||
} | ||
} | ||
module.exports = AmbassifyApi; |
"use strict"; | ||
const decodeBase64 = require('compact-base64').decode; | ||
/** | ||
@@ -8,4 +9,2 @@ * As long as we only need to decode, we can use this small utility that does | ||
*/ | ||
module.exports = { | ||
@@ -21,3 +20,2 @@ decode(jwt) { | ||
} | ||
}; |
"use strict"; | ||
const safeJson = require('@ambassify/json-parse-safe'); | ||
const AmbassifyError = require('@ambassify/error'); | ||
const throttle = require('@ambassify/throttle'); | ||
const jwt = require('./jwt'); | ||
class TokenRefreshFailed extends AmbassifyError { | ||
@@ -15,3 +11,2 @@ static code() { | ||
} | ||
[AmbassifyError.getHttpResponse]() { | ||
@@ -25,5 +20,3 @@ return { | ||
} | ||
} | ||
function isRefreshEnabled(options) { | ||
@@ -37,7 +30,5 @@ const { | ||
} | ||
function strHash(str) { | ||
let hash = 0; | ||
if (this.length == 0) return hash; | ||
for (let i = 0; i < str.length; i++) { | ||
@@ -51,3 +42,2 @@ const c = str.charCodeAt(i); | ||
} | ||
function doExternalRefresh(fetch, options) { | ||
@@ -76,3 +66,2 @@ const { | ||
} | ||
function doInternalRefresh(fetch, options) { | ||
@@ -101,3 +90,2 @@ const { | ||
} | ||
function doRefreshToken(fetch, options) { | ||
@@ -116,3 +104,2 @@ if (!isRefreshEnabled(options)) return Promise.reject(new TokenRefreshFailed('Refresh token is not enabled')); | ||
const json = safeJson(text); | ||
if (res.ok && json) { | ||
@@ -125,3 +112,2 @@ return { | ||
} | ||
if (Math.floor(res.status / 100) == 4) { | ||
@@ -138,9 +124,6 @@ throw AmbassifyError.fromExternal({ | ||
} | ||
throw new TokenRefreshFailed(json); | ||
}); | ||
} | ||
const MAX_TTL = 59 * 60 * 1000; // 59 minutes (tokens are usually valid for an hour) | ||
const doCachedRefreshToken = throttle(doRefreshToken, MAX_TTL, { | ||
@@ -147,0 +130,0 @@ rejectFailedPromise: true, |
{ | ||
"name": "@ambassify/ambassify-client", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Base API client library for all Ambassify clients", | ||
@@ -31,2 +31,3 @@ "engines": { | ||
"@ambassify/browserslist-config": "^1.0.1", | ||
"@ambassify/nock-compat": "^1.0.5", | ||
"@babel/cli": "^7.12.16", | ||
@@ -41,3 +42,2 @@ "@babel/core": "^7.12.16", | ||
"mocha-circleci-reporter": "0.0.3", | ||
"nock": "^11.3.3", | ||
"npm-build-tools": "^2.2.5", | ||
@@ -62,3 +62,3 @@ "nyc": "^14.1.1", | ||
}, | ||
"gitHead": "d5bd73d298306e40d1e9d6a26618bb6e4adfd404" | ||
"gitHead": "039c23735ec5a767dbf05aa13abd3c6f5e3ea60f" | ||
} |
'use strict'; | ||
const nock = require('nock'); | ||
const nock = require('@ambassify/nock-compat'); | ||
@@ -4,0 +4,0 @@ describe('authentication', () => { |
'use strict'; | ||
const assert = require('assert'); | ||
const nock = require('nock'); | ||
const nock = require('@ambassify/nock-compat'); | ||
const jwt = require('jsonwebtoken'); | ||
@@ -6,0 +6,0 @@ |
'use strict'; | ||
/* globals describe, before, afterEach, it */ | ||
const nock = require('nock'); | ||
const nock = require('@ambassify/nock-compat'); | ||
const assert = require('assert'); | ||
@@ -6,0 +6,0 @@ const jwt = require('jsonwebtoken'); |
'use strict'; | ||
const nock = require('nock'); | ||
const nock = require('@ambassify/nock-compat'); | ||
const assert = require('assert'); | ||
@@ -5,0 +5,0 @@ const FetchApi = require('@ambassify/fetch-api'); |
41260
1083