@ambassify/ambassify-client
Advanced tools
Comparing version 2.0.2 to 2.1.0
'use strict'; | ||
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | ||
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
@@ -19,2 +23,6 @@ | ||
var _get3 = require('babel-runtime/helpers/get'); | ||
var _get4 = _interopRequireDefault(_get3); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
@@ -30,4 +38,12 @@ | ||
var _get = require('lodash/get'); | ||
var _set = require('lodash/set'); | ||
var jwt = require('jsonwebtoken'); | ||
var FetchApi = require('@ambassify/fetch-api'); | ||
var _require = require('./refresh-token'), | ||
isRefreshEnabled = _require.isRefreshEnabled, | ||
doRefreshToken = _require.doRefreshToken, | ||
doCachedRefreshToken = _require.doCachedRefreshToken; | ||
FetchApi.defineContentType('JSON', require('@ambassify/fetch-api/lib/content-type/json')); | ||
@@ -79,7 +95,64 @@ | ||
return (0, _possibleConstructorReturn3.default)(this, (AmbassifyApi.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi)).call(this, (0, _extends3.default)({}, options, { | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (AmbassifyApi.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi)).call(this, (0, _extends3.default)({}, options, { | ||
contentType: options.contentType || FetchApi.ContentTypes.JSON | ||
}))); | ||
_this.options = options; | ||
return _this; | ||
} | ||
(0, _createClass3.default)(AmbassifyApi, [{ | ||
key: 'refreshTokenEnabled', | ||
value: function refreshTokenEnabled() { | ||
return isRefreshEnabled(this.options); | ||
} | ||
}, { | ||
key: 'refreshToken', | ||
value: function refreshToken() { | ||
var _this2 = this; | ||
return doRefreshToken(this.fetch, this.options).then(function (res) { | ||
return _this2._updateAuthentication(res); | ||
}); | ||
} | ||
}, { | ||
key: '_updateAuthentication', | ||
value: function _updateAuthentication(options) { | ||
var token = options.token, | ||
tokenType = options.tokenType, | ||
refreshToken = options.refreshToken; | ||
if (refreshToken) this.options.refreshToken = refreshToken; | ||
this.headers['authorization'] = tokenType + ' ' + token; | ||
} | ||
}, { | ||
key: '_request', | ||
value: function _request(url, options) { | ||
var _this3 = this; | ||
if (!this.refreshTokenEnabled()) return (0, _get4.default)(AmbassifyApi.prototype.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi.prototype), '_request', this).call(this, url, options); | ||
var authorization = _get(options, 'headers.authorization', ''); | ||
var _ref = /Bearer\s+(.*)/.exec(authorization) || [], | ||
_ref2 = (0, _slicedToArray3.default)(_ref, 2), | ||
token = _ref2[1]; | ||
if (!token) return (0, _get4.default)(AmbassifyApi.prototype.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi.prototype), '_request', this).call(this, url, options); | ||
var data = jwt.decode(token); | ||
var now = Math.floor(Date.now() / 1000); | ||
if (!data || !data.exp || data.exp > now + 60) return (0, _get4.default)(AmbassifyApi.prototype.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi.prototype), '_request', this).call(this, url, options); | ||
return doCachedRefreshToken(this.fetch, (0, _extends3.default)({}, this.options, { token: token })).then(function (res) { | ||
_this3._updateAuthentication(res); | ||
_set(options, 'headers.authorization', 'Bearer ' + res.token); | ||
return (0, _get4.default)(AmbassifyApi.prototype.__proto__ || (0, _getPrototypeOf2.default)(AmbassifyApi.prototype), '_request', _this3).call(_this3, url, options); | ||
}); | ||
} | ||
}]); | ||
return AmbassifyApi; | ||
@@ -86,0 +159,0 @@ }(FetchApi); |
{ | ||
"name": "@ambassify/ambassify-client", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Base API client library for all Ambassify clients", | ||
@@ -14,4 +14,5 @@ "engines": { | ||
"build": "n-clean lib && babel -d lib src", | ||
"test": "npm run lint", | ||
"lint": "eslint src" | ||
"test": "npm -s run test:lint && npm -s run test:unit", | ||
"test:lint": "eslint src", | ||
"test:unit": "nyc mocha" | ||
}, | ||
@@ -21,3 +22,8 @@ "author": "Ambassify <dev@ambassify.com>", | ||
"dependencies": { | ||
"@ambassify/fetch-api": "^3.0.0" | ||
"@ambassify/error": "^1.0.1", | ||
"@ambassify/fetch-api": "^3.0.0", | ||
"@ambassify/json-parse-safe": "^1.1.0", | ||
"@ambassify/throttle": "^0.1.4", | ||
"jsonwebtoken": "^8.5.1", | ||
"lodash": "^4.17.15" | ||
}, | ||
@@ -31,3 +37,8 @@ "devDependencies": { | ||
"eslint-config-ambassify": "^1.2.0", | ||
"npm-build-tools": "^2.2.5" | ||
"mocha": "^6.2.0", | ||
"mocha-circleci-reporter": "0.0.3", | ||
"nock": "^11.3.3", | ||
"npm-build-tools": "^2.2.5", | ||
"nyc": "^14.1.1", | ||
"sinon": "^7.4.2" | ||
}, | ||
@@ -48,3 +59,3 @@ "peerDependencies": { | ||
"homepage": "https://github.com/ambassify/api-clients#readme", | ||
"gitHead": "ded10788e75d3a295643e09e767a8bb12d24c70a" | ||
"gitHead": "5a4f85df0c4f7ff2edba5e7d56b703d8d76dd8cc" | ||
} |
@@ -0,2 +1,6 @@ | ||
const _get = require('lodash/get'); | ||
const _set = require('lodash/set'); | ||
const jwt = require('jsonwebtoken'); | ||
const FetchApi = require('@ambassify/fetch-api'); | ||
const { isRefreshEnabled, doRefreshToken, doCachedRefreshToken } = require('./refresh-token'); | ||
@@ -47,6 +51,51 @@ FetchApi.defineContentType('JSON', require('@ambassify/fetch-api/lib/content-type/json')); | ||
}); | ||
this.options = options; | ||
} | ||
refreshTokenEnabled() { | ||
return isRefreshEnabled(this.options); | ||
} | ||
refreshToken() { | ||
return doRefreshToken(this.fetch, this.options) | ||
.then(res => this._updateAuthentication(res)); | ||
} | ||
_updateAuthentication(options) { | ||
const { token, tokenType, refreshToken } = options; | ||
if (refreshToken) | ||
this.options.refreshToken = refreshToken; | ||
this.headers['authorization'] = `${tokenType} ${token}`; | ||
} | ||
_request(url, options) { | ||
if (!this.refreshTokenEnabled()) | ||
return super._request(url, options); | ||
const authorization = _get(options, 'headers.authorization', ''); | ||
const [ , token ] = /Bearer\s+(.*)/.exec(authorization) || []; | ||
if (!token) | ||
return super._request(url, options); | ||
const data = jwt.decode(token); | ||
const now = Math.floor(Date.now() / 1000); | ||
if (!data || !data.exp || data.exp > now + 60) | ||
return super._request(url, 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); | ||
}); | ||
} | ||
} | ||
module.exports = AmbassifyApi; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
32588
12
751
7
12
8
+ Added@ambassify/error@^1.0.1
+ Added@ambassify/throttle@^0.1.4
+ Addedjsonwebtoken@^8.5.1
+ Addedlodash@^4.17.15
+ Added@ambassify/json-parse-safe@1.1.0(transitive)
+ Added@ambassify/throttle@0.1.9(transitive)
+ Addedbindings@1.5.0(transitive)
+ Addedbuffer-equal-constant-time@1.0.1(transitive)
+ Addedecdsa-sig-formatter@1.0.11(transitive)
+ Addedfile-uri-to-path@1.0.0(transitive)
+ Addedget-symbol-from-current-process-h@1.0.2(transitive)
+ Addedget-uv-event-loop-napi-h@1.0.6(transitive)
+ Addedjsonwebtoken@8.5.1(transitive)
+ Addedjwa@1.4.1(transitive)
+ Addedjws@3.2.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash.includes@4.3.0(transitive)
+ Addedlodash.isboolean@3.0.3(transitive)
+ Addedlodash.isinteger@4.0.4(transitive)
+ Addedlodash.isnumber@3.0.3(transitive)
+ Addedlodash.isplainobject@4.0.6(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.once@4.1.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-addon-api@1.7.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsetimmediate-napi@1.0.6(transitive)
+ Addedweak-napi@1.0.3(transitive)