Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest-client-sdk

Package Overview
Dependencies
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-client-sdk - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0

138

dist/client/AbstractClient.js

@@ -11,36 +11,36 @@ Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _urijs=require('urijs');var _urijs2=_interopRequireDefault(_urijs);

this._tokenStorage=sdk.tokenStorage;
this.entityFactory=sdk.entityFactory;}_createClass(AbstractClient,[{key:'getDefaultParameters',value:function getDefaultParameters()
this.entityFactory=sdk.entityFactory;
}_createClass(AbstractClient,[{key:'getDefaultParameters',value:function getDefaultParameters()
{
return[];}},{key:'getPathBase',value:function getPathBase()
return[];
}},{key:'getPathBase',value:function getPathBase()
{var pathParameters=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};
throw new Error('AbstractClient::getPathBase can not be called directly.\n You must implement "getPathBase" method.');
{var pathParameters=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];
throw new Error('AbstractClient::getPathBase can not be called directly.\n You must implement "getPathBase" method.');}},{key:'getName',value:function getName()
}},{key:'getName',value:function getName()
{
throw new Error('AbstractClient::getName can not be called directly.\n You must implement "getName" method.');}},{key:'find',value:function find(
throw new Error('AbstractClient::getName can not be called directly.\n You must implement "getName" method.');
}},{key:'find',value:function find(
id){var queryParam=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];var pathParameters=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];
id){var queryParam=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var pathParameters=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};
var url=this._generateUrlFromParams(queryParam,pathParameters,id);
return this.createEntityFromJsonResponse(this.authorizedFetch(url),'item');}},{key:'findBy',value:function findBy(
return this.createEntityFromJsonResponse(this.authorizedFetch(url),'item');
}},{key:'findBy',value:function findBy(
queryParam){var pathParameters=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
queryParam){var pathParameters=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};
var url=this._generateUrlFromParams(queryParam,pathParameters);
return this.createEntityFromJsonResponse(this.authorizedFetch(url),'list');}},{key:'findAll',value:function findAll()
return this.createEntityFromJsonResponse(this.authorizedFetch(url),'list');
}},{key:'findAll',value:function findAll()
{var queryParam=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var pathParameters=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};
return this.findBy(queryParam,pathParameters);
}},{key:'create',value:function create(
{var queryParam=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];var pathParameters=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
return this.findBy(queryParam,pathParameters);}},{key:'create',value:function create(
entity){var queryParam=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];var pathParameters=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];
entity){var queryParam=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var pathParameters=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};
var url=new _urijs2.default(this.getPathBase(pathParameters));

@@ -54,7 +54,7 @@ url.addSearch(queryParam);

'item');}},{key:'update',value:function update(
'item');
}},{key:'update',value:function update(
entity){var queryParam=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
entity){var queryParam=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};
var url=new _urijs2.default(entity.get('@id'));

@@ -68,6 +68,6 @@ url.addSearch(queryParam);

'item');}},{key:'delete',value:function _delete(
'item');
}},{key:'delete',value:function _delete(
entity){

@@ -79,13 +79,13 @@ var url=entity.get('@id');

'item');}},{key:'createEntityFromJsonResponse',value:function createEntityFromJsonResponse(
'item');
}},{key:'createEntityFromJsonResponse',value:function createEntityFromJsonResponse(
requestPromise,listOrItem){var _this=this;
return requestPromise.
then(function(response){return response.json();}).
then(function(val){return _this.entityFactory(val,listOrItem,_this.getName());});}},{key:'makeUri',value:function makeUri(
then(function(val){return _this.entityFactory(val,listOrItem,_this.getName());});
}},{key:'makeUri',value:function makeUri(
input){

@@ -98,26 +98,26 @@ var url=input instanceof _urijs2.default?input:new _urijs2.default(input);

if(this.sdk.config.port){
url.port(this.sdk.config.port);}
url.port(this.sdk.config.port);
}
if(this.sdk.config.prefix){
var segments=url.segment();
segments.unshift(this.sdk.config.prefix);
url.segment(segments);}
url.segment(segments);
}
return url;
}},{key:'authorizedFetch',value:function authorizedFetch(
return url;}},{key:'authorizedFetch',value:function authorizedFetch(
input,init){
var url=this.makeUri(input);
return this._doFetch(url.toString(),init);}},{key:'_generateUrlFromParams',value:function _generateUrlFromParams(
return this._doFetch(url.toString(),init);
}},{key:'_generateUrlFromParams',value:function _generateUrlFromParams(
queryParam){var pathParameters=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];var id=arguments.length<=2||arguments[2]===undefined?null:arguments[2];
queryParam){var pathParameters=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var id=arguments.length>2&&arguments[2]!==undefined?arguments[2]:null;
var params=queryParam;
if(this.sdk.config.useDefaultParameters){
_extends(params,this.getDefaultParameters());}
_extends(params,this.getDefaultParameters());
}
var url=new _urijs2.default(!!id?

@@ -128,18 +128,18 @@ this.getPathBase(pathParameters)+'/'+id:

if(params){
url.addSearch(params);}
url.addSearch(params);
}
return url;
}},{key:'_doFetch',value:function _doFetch(
return url;}},{key:'_doFetch',value:function _doFetch(
input,init){var _this2=this;
if(!input){
throw new Error('input is empty');}
throw new Error('input is empty');
}
return this._tokenStorage.getAccessToken().
then(function(token){return _this2._fetchWithToken(token,input,init);});}},{key:'_manageAccessDenied',value:function _manageAccessDenied(
then(function(token){return _this2._fetchWithToken(token,input,init);});
}},{key:'_manageAccessDenied',value:function _manageAccessDenied(
response,input,init){var _this3=this;

@@ -155,19 +155,19 @@ return response.json().

catch(function(){
throw new _Error.AccessDeniedError('Unable to renew access_token',response);});}
throw new _Error.AccessDeniedError('Unable to renew access_token',response);
});
}
break;
default:
throw new _Error.AccessDeniedError(body.error_description,response);}}
throw new _Error.AccessDeniedError(body.error_description,response);}
}
throw new _Error.AccessDeniedError('Unable to access ressource: 401 found !',response);
});
throw new _Error.AccessDeniedError('Unable to access ressource: 401 found !',response);});}},{key:'_fetchWithToken',value:function _fetchWithToken(
}},{key:'_fetchWithToken',value:function _fetchWithToken(
accessToken,input,init){var _this4=this;

@@ -177,3 +177,3 @@ var params=init;

var tokenHeaders={
Authorization:'Bearer '+accessToken};
Authorization:this.sdk.config.authorizationType+' '+accessToken};

@@ -183,24 +183,24 @@

if(!params.headers){
params.headers={};}
params.headers={};
}
params.headers=_extends(params.headers,tokenHeaders);
}else{
params={headers:tokenHeaders};
}
params.headers=_extends(params.headers,tokenHeaders);}else
{
params={headers:tokenHeaders};}
return fetch(input,params).
then(function(response){
if(response.status===401){
return _this4._manageAccessDenied(response,input,params);}else
if(response.status===403){
throw new _Error.ForbiddenError('Forbidden acces: 403 found !',response);}
return _this4._manageAccessDenied(response,input,params);
}else if(response.status===403){
throw new _Error.ForbiddenError('Forbidden acces: 403 found !',response);
}
return response;
});
return response;});}}]);return AbstractClient;}();exports.default=
}}]);return AbstractClient;}();exports.default=
AbstractClient;

@@ -1,5 +0,5 @@

Object.defineProperty(exports,"__esModule",{value:true});/**
* It's a bit tricky to extends native errors
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
*/
Object.defineProperty(exports,"__esModule",{value:true});
function AccessDeniedError(message,baseResponse){

@@ -9,4 +9,4 @@ this.name='AccessDeniedError';

this.baseResponse=baseResponse;
this.stack=new Error().stack;}
this.stack=new Error().stack;
}
AccessDeniedError.prototype=Object.create(Error.prototype);

@@ -19,4 +19,4 @@ AccessDeniedError.prototype.constructor=AccessDeniedError;

this.baseResponse=baseResponse;
this.stack=new Error().stack;}
this.stack=new Error().stack;
}
ForbiddenError.prototype=Object.create(Error.prototype);

@@ -23,0 +23,0 @@ ForbiddenError.prototype.constructor=ForbiddenError;exports.

Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _immutable=require('immutable');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}
function createEntity(val,listOrItem){var type=arguments.length<=2||arguments[2]===undefined?null:arguments[2];
return(0,_immutable.fromJS)(val);}var
function createEntity(val,listOrItem){var type=arguments.length>2&&arguments[2]!==undefined?arguments[2]:null;
return(0,_immutable.fromJS)(val);
}var
RestClientSdk=function(){
function RestClientSdk(tokenStorage,config){var _this=this;var clientList=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];var entityFactory=arguments.length<=3||arguments[3]===undefined?createEntity:arguments[3];_classCallCheck(this,RestClientSdk);
function RestClientSdk(tokenStorage,config){var _this=this;var clientList=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var entityFactory=arguments.length>3&&arguments[3]!==undefined?arguments[3]:createEntity;_classCallCheck(this,RestClientSdk);
this._checkConfigValidity(config);

@@ -16,6 +16,6 @@

Object.keys(clientList).forEach(function(key){
_this[key]=new clientList[key](_this);});}_createClass(RestClientSdk,[{key:'_mergeWithBaseConfig',value:function _mergeWithBaseConfig(
_this[key]=new clientList[key](_this);
});
}_createClass(RestClientSdk,[{key:'_mergeWithBaseConfig',value:function _mergeWithBaseConfig(
config){

@@ -27,15 +27,17 @@ var newConfig=config;

return newConfig;}},{key:'_checkConfigValidity',value:function _checkConfigValidity(
newConfig.authorizationType=config.authorizationType||'Bearer';
return newConfig;
}},{key:'_checkConfigValidity',value:function _checkConfigValidity(
config){
if(!(config&&config.path&&config.scheme)){
throw new RangeError('SDK config is not valid, it should contain a "path", a "scheme" parameter,\n and may contain a "port" and a "useDefaultParameters" parameter');}}}]);return RestClientSdk;}();exports.default=
throw new RangeError('SDK config is not valid, it should contain a "path", a "scheme" parameter,\n and may contain a "port" and a "useDefaultParameters" parameter');
}
}}]);return RestClientSdk;}();exports.default=
RestClientSdk;
Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var AbstractTokenGenerator=function(){
function AbstractTokenGenerator(){var tokenGeneratorConfig=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];_classCallCheck(this,AbstractTokenGenerator);
function AbstractTokenGenerator(){var tokenGeneratorConfig=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};_classCallCheck(this,AbstractTokenGenerator);
this.tokenGeneratorConfig=tokenGeneratorConfig;
this.canAutogenerateToken=false;
this.checkTokenGeneratorConfig(this.tokenGeneratorConfig);}_createClass(AbstractTokenGenerator,[{key:"generateToken",value:function generateToken(
this.checkTokenGeneratorConfig(this.tokenGeneratorConfig);
}_createClass(AbstractTokenGenerator,[{key:"generateToken",value:function generateToken(
parameters){
throw new Error("AbstractTokenGenerator::generateToken can not be called directly.\n You must implement \"generateToken\" method.");}},{key:"refreshToken",value:function refreshToken(
throw new Error("AbstractTokenGenerator::generateToken can not be called directly.\n You must implement \"generateToken\" method.");
}},{key:"refreshToken",value:function refreshToken(
accessToken,parameters){
throw new Error("AbstractTokenGenerator::refreshToken can not be called directly.\n You must implement \"refreshToken\" method.");}},{key:"checkTokenGeneratorConfig",value:function checkTokenGeneratorConfig(
throw new Error("AbstractTokenGenerator::refreshToken can not be called directly.\n You must implement \"refreshToken\" method.");
}},{key:"checkTokenGeneratorConfig",value:function checkTokenGeneratorConfig(
config){
return true;}},{key:"convertMapToFormData",value:function convertMapToFormData(
return true;
}},{key:"convertMapToFormData",value:function convertMapToFormData(
parameters){

@@ -28,9 +28,9 @@ var keys=Object.keys(parameters);

for(var _iterator=keys,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[typeof Symbol==="function"?Symbol.iterator:"@@iterator"]();;){var _ref;if(_isArray){if(_i>=_iterator.length)break;_ref=_iterator[_i++];}else{_i=_iterator.next();if(_i.done)break;_ref=_i.value;}var key=_ref;
formData.append(key,parameters[key]);}
formData.append(key,parameters[key]);
}
return formData;
}}]);return AbstractTokenGenerator;}();exports.default=
return formData;}}]);return AbstractTokenGenerator;}();exports.default=
AbstractTokenGenerator;

@@ -8,4 +8,4 @@ Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _urijs=require('urijs');var _urijs2=_interopRequireDefault(_urijs);

ClientCredentialsGenerator=function(_AbstractTokenGenerat){_inherits(ClientCredentialsGenerator,_AbstractTokenGenerat);function ClientCredentialsGenerator(){_classCallCheck(this,ClientCredentialsGenerator);return _possibleConstructorReturn(this,Object.getPrototypeOf(ClientCredentialsGenerator).apply(this,arguments));}_createClass(ClientCredentialsGenerator,[{key:'generateToken',value:function generateToken()
{var baseParameters=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];
ClientCredentialsGenerator=function(_AbstractTokenGenerat){_inherits(ClientCredentialsGenerator,_AbstractTokenGenerat);function ClientCredentialsGenerator(){_classCallCheck(this,ClientCredentialsGenerator);return _possibleConstructorReturn(this,(ClientCredentialsGenerator.__proto__||Object.getPrototypeOf(ClientCredentialsGenerator)).apply(this,arguments));}_createClass(ClientCredentialsGenerator,[{key:'generateToken',value:function generateToken()
{var baseParameters=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};
var parameters=baseParameters;

@@ -21,5 +21,5 @@ parameters.grant_type='client_credentials';

if(this.tokenGeneratorConfig.port){
uri.port(this.tokenGeneratorConfig.port);}
uri.port(this.tokenGeneratorConfig.port);
}
var url=uri.toString();

@@ -34,28 +34,28 @@

return response.json().
then(function(responseData){return Promise.reject(responseData);});}
then(function(responseData){return Promise.reject(responseData);});
}
return response.json();
});
}},{key:'refreshToken',value:function refreshToken(
return response.json();});}},{key:'refreshToken',value:function refreshToken(
accessToken,parameters){
return this.generateToken(parameters);}},{key:'checkTokenGeneratorConfig',value:function checkTokenGeneratorConfig(
return this.generateToken(parameters);
}},{key:'checkTokenGeneratorConfig',value:function checkTokenGeneratorConfig(
config){
if(!config||Object.keys(config).length===0){
throw new RangeError(ERROR_CONFIG_EMPTY);}
throw new RangeError(ERROR_CONFIG_EMPTY);
}
if(!(config.path&&config.scheme)){
throw new RangeError(ERROR_CONFIG_PATH_SCHEME);}
throw new RangeError(ERROR_CONFIG_PATH_SCHEME);
}
if(!(config.clientId&&config.clientSecret)){
throw new RangeError(ERROR_CONFIG_CLIENT_INFORMATIONS);}}}]);return ClientCredentialsGenerator;}(_AbstractTokenGenerator2.default);exports.default=
throw new RangeError(ERROR_CONFIG_CLIENT_INFORMATIONS);
}
}}]);return ClientCredentialsGenerator;}(_AbstractTokenGenerator2.default);exports.default=
ClientCredentialsGenerator;

@@ -12,3 +12,3 @@ Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _urijs=require('urijs');var _urijs2=_interopRequireDefault(_urijs);

PasswordGenerator=function(_AbstractTokenGenerat){_inherits(PasswordGenerator,_AbstractTokenGenerat);function PasswordGenerator(){_classCallCheck(this,PasswordGenerator);return _possibleConstructorReturn(this,Object.getPrototypeOf(PasswordGenerator).apply(this,arguments));}_createClass(PasswordGenerator,[{key:'generateToken',value:function generateToken(
PasswordGenerator=function(_AbstractTokenGenerat){_inherits(PasswordGenerator,_AbstractTokenGenerat);function PasswordGenerator(){_classCallCheck(this,PasswordGenerator);return _possibleConstructorReturn(this,(PasswordGenerator.__proto__||Object.getPrototypeOf(PasswordGenerator)).apply(this,arguments));}_createClass(PasswordGenerator,[{key:'generateToken',value:function generateToken(
baseParameters){

@@ -22,10 +22,10 @@ var parameters=baseParameters;

return this._doFetch(parameters);}},{key:'refreshToken',value:function refreshToken(
return this._doFetch(parameters);
}},{key:'refreshToken',value:function refreshToken(
accessToken){var baseParameters=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
accessToken){var baseParameters=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};
if(!(accessToken&&accessToken.refresh_token)){
throw new Error('refresh_token is not set. Did you called `generateToken` before ?');}
throw new Error('refresh_token is not set. Did you called `generateToken` before ?');
}
var parameters=baseParameters;

@@ -39,19 +39,19 @@

return this._doFetch(parameters);}},{key:'checkTokenGeneratorConfig',value:function checkTokenGeneratorConfig(
return this._doFetch(parameters);
}},{key:'checkTokenGeneratorConfig',value:function checkTokenGeneratorConfig(
config){
if(!config||Object.keys(config).length===0){
throw new RangeError(ERROR_CONFIG_EMPTY);}
throw new RangeError(ERROR_CONFIG_EMPTY);
}
if(!(config.path&&config.scheme)){
throw new RangeError(ERROR_CONFIG_PATH_SCHEME);}
throw new RangeError(ERROR_CONFIG_PATH_SCHEME);
}
if(!(config.clientId&&config.clientSecret)){
throw new RangeError(ERROR_CONFIG_CLIENT_INFORMATIONS);}}},{key:'_doFetch',value:function _doFetch(
throw new RangeError(ERROR_CONFIG_CLIENT_INFORMATIONS);
}
}},{key:'_doFetch',value:function _doFetch(
parameters){

@@ -62,5 +62,5 @@ var uri=new _urijs2.default(this.tokenGeneratorConfig.path);

if(this.tokenGeneratorConfig.port){
uri.port(this.tokenGeneratorConfig.port);}
uri.port(this.tokenGeneratorConfig.port);
}
var url=uri.toString();

@@ -75,20 +75,20 @@

return response.json().
then(function(responseData){return Promise.reject(responseData);});}
then(function(responseData){return Promise.reject(responseData);});
}
return response.json();
});
}},{key:'_checkGenerateParameters',value:function _checkGenerateParameters(
return response.json();});}},{key:'_checkGenerateParameters',value:function _checkGenerateParameters(
parameters){
if(!(parameters&&Object.keys(parameters).length>0)){
throw new RangeError(ERROR_TOKEN_EMPTY);}
throw new RangeError(ERROR_TOKEN_EMPTY);
}
if(!(parameters.username&&parameters.password)){
throw new RangeError(ERROR_TOKEN_USERNAME_PASSWORD);}}}]);return PasswordGenerator;}(_AbstractTokenGenerator2.default);exports.default=
throw new RangeError(ERROR_TOKEN_USERNAME_PASSWORD);
}
}}]);return PasswordGenerator;}(_AbstractTokenGenerator2.default);exports.default=
PasswordGenerator;

@@ -5,19 +5,19 @@ Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _AbstractTokenGenerator=require('./AbstractTokenGenerator');var _AbstractTokenGenerator2=_interopRequireDefault(_AbstractTokenGenerator);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}var

function ProvidedTokenGenerator(token){_classCallCheck(this,ProvidedTokenGenerator);var _this=_possibleConstructorReturn(this,Object.getPrototypeOf(ProvidedTokenGenerator).call(this));
function ProvidedTokenGenerator(token){_classCallCheck(this,ProvidedTokenGenerator);var _this=_possibleConstructorReturn(this,(ProvidedTokenGenerator.__proto__||Object.getPrototypeOf(ProvidedTokenGenerator)).call(this));
_this._token=token;
_this.canAutogenerateToken=true;return _this;}_createClass(ProvidedTokenGenerator,[{key:'generateToken',value:function generateToken()
_this.canAutogenerateToken=true;return _this;
}_createClass(ProvidedTokenGenerator,[{key:'generateToken',value:function generateToken()
{
return Promise.resolve({
access_token:this._token});}},{key:'refreshToken',value:function refreshToken()
access_token:this._token});
}},{key:'refreshToken',value:function refreshToken()
{
return this.generateToken();}}]);return ProvidedTokenGenerator;}(_AbstractTokenGenerator2.default);exports.default=
return this.generateToken();
}}]);return ProvidedTokenGenerator;}(_AbstractTokenGenerator2.default);exports.default=
ProvidedTokenGenerator;

@@ -7,14 +7,14 @@ Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var ACCESS_TOKEN_KEY='rest_client_sdk.api.access_token';var

this._hasATokenBeenGenerated=false;
this.setAsyncStorage(asyncStorage);}_createClass(TokenStorage,[{key:'setAsyncStorage',value:function setAsyncStorage(
this.setAsyncStorage(asyncStorage);
}_createClass(TokenStorage,[{key:'setAsyncStorage',value:function setAsyncStorage(
asyncStorage){
this._asyncStorage=asyncStorage;}},{key:'hasAccessToken',value:function hasAccessToken()
this._asyncStorage=asyncStorage;
}},{key:'hasAccessToken',value:function hasAccessToken()
{
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).
then(function(accessToken){return!!accessToken;});}},{key:'getAccessToken',value:function getAccessToken()
then(function(accessToken){return!!accessToken;});
}},{key:'getAccessToken',value:function getAccessToken()
{var _this=this;

@@ -25,29 +25,30 @@ return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).

if(!_this._hasATokenBeenGenerated&&!_this._tokenGenerator.canAutogenerateToken){
throw new Error('No token has been generated yet.');}
throw new Error('No token has been generated yet.');
}
if(!_this._hasATokenBeenGenerated&&_this._tokenGenerator.canAutogenerateToken){
return _this._tokenGenerator.generateToken().
then(function(){return _this._asyncStorage.getItem(ACCESS_TOKEN_KEY);}).
then(function(generatedToken){return generatedToken&&JSON.parse(generatedToken).access_token;});}}
return _this.generateToken().
then(function(generatedToken){return generatedToken&&generatedToken.access_token;});
}
}
return token&&JSON.parse(token).access_token;
});
}},{key:'logout',value:function logout()
return token&&JSON.parse(token).access_token;});}},{key:'logout',value:function logout()
{
return this._asyncStorage.removeItem(ACCESS_TOKEN_KEY);}},{key:'generateToken',value:function generateToken(
return this._asyncStorage.removeItem(ACCESS_TOKEN_KEY);
}},{key:'generateToken',value:function generateToken(
parameters){var _this2=this;
this._hasATokenBeenGenerated=true;
return this._tokenGenerator.generateToken(parameters).
then(function(responseData){return _this2._storeAccessToken(responseData).
then(function(){return responseData;});});}},{key:'refreshToken',value:function refreshToken(
then(function(responseData){return(
_this2._storeAccessToken(responseData).
then(function(){return responseData;}));});
}},{key:'refreshToken',value:function refreshToken(

@@ -61,15 +62,15 @@ parameters){var _this3=this;

_this3._storeAccessToken(responseData).
then(function(){return responseData;}));}));});}},{key:'_storeAccessToken',value:function _storeAccessToken(
then(function(){return responseData;}));}));});
}},{key:'_storeAccessToken',value:function _storeAccessToken(
responseData){
return this._asyncStorage.
setItem(ACCESS_TOKEN_KEY,JSON.stringify(responseData));}}]);return TokenStorage;}();exports.default=
setItem(ACCESS_TOKEN_KEY,JSON.stringify(responseData));
}}]);return TokenStorage;}();exports.default=
TokenStorage;
{
"name": "rest-client-sdk",
"version": "0.7.0",
"version": "0.8.0",
"description": "Rest Client SDK for API",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -26,2 +26,4 @@ Mapado Rest Client JS SDK [![Build Status](https://travis-ci.org/mapado/rest-client-js-sdk.svg?branch=master)](https://travis-ci.org/mapado/rest-client-js-sdk)

}
export default SomeEntityClient;
```

@@ -34,6 +36,6 @@

const tokenGeneratorConfig = { path: 'oauth.me', foo: 'bar' }
const tokenGeneratorConfig = { path: 'oauth.me', foo: 'bar' };
const tokenGenerator = new SomeTokenGenerator(tokenGeneratorConfig); // Some token generators are defined in `src/TokenGenerator/`
const storage = AsyncStorage; // create a storage instance if you are not on RN. In browser and node, localforage works fine
const tokenStorage = new TokenStorage(tokenGenerator, asyncStorage);
const tokenStorage = new TokenStorage(tokenGenerator, storage);
```

@@ -56,2 +58,3 @@ The token generator is a class implementing `generateToken` and `refreshToken`.

segment: '/my-api',
authorizationType: 'Bearer', // default to "Bearer", but can be "Basic" or anything
useDefaultParameters: true,

@@ -58,0 +61,0 @@ }; // path and scheme are mandatory

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc