Socket
Socket
Sign inDemoInstall

@ambassify/ambassify-client

Package Overview
Dependencies
6
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.2 to 2.1.0

.nycrc

75

lib/index.js
'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);

23

package.json
{
"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;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc