@netology-group/account
Advanced tools
Comparing version 2.3.0-rc.2 to 2.3.0
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Account=t():e.Account=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(1);n.d(t,"Account",function(){return r.a});var o=n(3);n.d(t,"IdP",function(){return o.a})},function(e,t,n){"use strict";function r(e){return c(e)||i(e)||o()}function o(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function i(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function c(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){h(e,t,n[t])})}return e}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function s(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function f(e,t,n){return t&&s(e.prototype,t),n&&s(e,n),e}function h(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"a",function(){return y});var d=n(2),l=3,p=1e3,v=3e3,y=function(){function e(t,n,r){if(u(this,e),h(this,"fetchFn",void 0),h(this,"fetchOpts",void 0),h(this,"id",void 0),h(this,"label",void 0),h(this,"leeway",void 0),h(this,"requestMode",void 0),h(this,"provider",void 0),h(this,"retries",void 0),h(this,"retryDelay",void 0),h(this,"storage",void 0),!t)throw new TypeError("Missing `config`");if(!n)throw new TypeError("Provider is not defined");if(!r)throw new TypeError("Storage is not defined");this.storage=r,this.provider=n,this.fetchFn=d.a,this.fetchOpts={delay:t.retryDelay||p,retries:t.retries||l},this.leeway=t.leeway||v,this.requestMode=t.requestMode||"id";var o=this._createId(t.audience,t.label),i=o.id,c=o.label;if(this.label=c,this.id=i,!this.id)throw new TypeError("Failed to configure account. Id is not present")}return f(e,[{key:"_createId",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:".";if(!e)throw new TypeError("`audience` is absent");if(!t)throw new TypeError("`label` is absent");return{id:"".concat(t).concat(n).concat(e),label:t}}},{key:"_requestLabel",value:function(){return"label"===this.requestMode?this.label:this.id}},{key:"load",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id;return n?Promise.resolve(function(){return e.storage.getItem(n)}).then(function(e){var t=e();if(!t)throw new TypeError("Can not load data");return Object(d.c)(t)}):Promise.reject(new TypeError("`label` is absent"))}},{key:"remove",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id;return n?this.load(n).then(function(t){return e.storage.removeItem(n),t}):Promise.reject(new TypeError("`label` is absent"))}},{key:"store",value:function(e){var t=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=n||this.id;return r?Promise.resolve(e).then(function(e){var n=0;if(e.expires_in){var o=Number(e.expires_in);if(isNaN(o))throw new TypeError("Wrong `expires_in` value");n=Date.now()+1e3*(o||0)}var i=a({},e,{expires_time:n});return t.storage.setItem(r,JSON.stringify(i)),i}):Promise.reject(new TypeError("`label` is absent"))}},{key:"account",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.account;return this.tokenData(n).then(function(t){var n=t.access_token;return[e._requestLabel(),n]}).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(d.e).then(d.d)}},{key:"tokenData",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.refreshAccessToken;return this.load(n).then(function(t){if(!Object(d.b)(t,e.leeway))return t;var i=t.refresh_token;return Promise.resolve([e._requestLabel(),i]).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(d.e).then(d.d).then(function(t){return e.load(n).then(function(n){return e.store(a({},n,{access_token:t.access_token,expires_in:t.expires_in}))})})})}},{key:"revokeRefreshToken",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.revokeRefreshToken;return this.load(n).then(function(t){var n=t.refresh_token;return[e._requestLabel(),n]}).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(d.e).then(d.d).then(function(t){return e.load(n).then(function(n){return e.store(a({},n,{refresh_token:t.refresh_token}))})})}}],[{key:"fetchLabel",value:function(e,t,n){var r=e.refresh_token;if(!r)throw new TypeError("`refresh_token` is absent");if(!t)throw new TypeError("Provider is not defined");var o={delay:p,retries:l};return Object(d.a)(function(){return t.refreshAccessToken(n||"me",r)},o).then(d.e).then(d.d).then(function(e){return Object(d.a)(function(){return t.account(n||"me",e.access_token)},o).then(d.e).then(d.d)})}}]),e}()},function(e,t,n){"use strict";n.d(t,"a",function(){return r}),n.d(t,"b",function(){return o}),n.d(t,"e",function(){return i}),n.d(t,"d",function(){return c}),n.d(t,"c",function(){return a});var r=function(e,t){if(!e)throw new TypeError("Missing 'requestFn': ".concat(e));return new Promise(function(n,r){var o=[];!function i(c){c<1?r(o):fetch(e()).then(function(e){return n(e)}).catch(function(e){o.push(e),setTimeout(function(){i(c-1)},t.delay||1e3)})}(t.retries||3)})},o=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:3e3;return function(e){return!e||!e.expires_time||Date.now()>Number(e.expires_time)-t}(e)},i=function(e){if(e.status&&e.status>=200&&e.status<300)return e;throw new Error(e.statusText||"Invalid request. Status: ".concat(e.status))},c=function(e){if(!e)throw new TypeError("`response` is absent");try{return e.json()}catch(e){throw new Error("Response is not a JSON")}},a=function(e){var t="function"==typeof e?e():e;if("string"!=typeof t)throw new TypeError("Can not parse");return new Promise(function(e,n){try{e(JSON.parse(t))}catch(e){n(e)}})}},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),e}function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"a",function(){return a});var a=function(){function e(t){var n=this;if(r(this,e),c(this,"endpoint",void 0),c(this,"accountEndpoint",void 0),c(this,"authnEndpoint",void 0),!t)throw new TypeError("Missing provider configuration");this.endpoint=t.endpoint,this.accountEndpoint=function(e){if(!n.endpoint&&!e.accountEndpoint)throw new TypeError("Can not resolve account endpoint");return e.accountEndpoint&&"function"==typeof e.accountEndpoint?e.accountEndpoint():e.accountEndpoint||"".concat(n.endpoint,"/accounts")}(t),this.authnEndpoint=function(e){if(!n.endpoint&&!e.authnEndpoint)throw new TypeError("Can not resolve authentication endpoint");return e.authnEndpoint&&"function"==typeof e.authnEndpoint?e.authnEndpoint():e.authnEndpoint||"".concat(n.endpoint,"/authn")}(t)}return i(e,[{key:"refreshAccessToken",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'refreshToken': ".concat(t));var n="".concat(this.accountEndpoint,"/").concat(e,"/refresh");return new Request(n,{method:"POST",headers:{Authorization:"Bearer ".concat(t)}})}},{key:"revokeRefreshToken",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'refreshToken': ".concat(t));var n="".concat(this.accountEndpoint,"/").concat(e,"/revoke");return new Request(n,{method:"POST",headers:{Authorization:"Bearer ".concat(t)}})}},{key:"account",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'accessToken': ".concat(t));var n="".concat(this.authnEndpoint,"/").concat(e);return new Request(n,{method:"GET",headers:{Authorization:"Bearer ".concat(t)}})}}]),e}()}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Account=t():e.Account=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(1);n.d(t,"Account",function(){return r.a});var o=n(3);n.d(t,"IdP",function(){return o.a})},function(e,t,n){"use strict";function r(e){return c(e)||i(e)||o()}function o(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function i(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function c(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){h(e,t,n[t])})}return e}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function s(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function f(e,t,n){return t&&s(e.prototype,t),n&&s(e,n),e}function h(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"a",function(){return y});var l=n(2),d=3,p=1e3,v=3e3,y=function(){function e(t,n,r){if(u(this,e),h(this,"fetchFn",void 0),h(this,"fetchOpts",void 0),h(this,"id",void 0),h(this,"label",void 0),h(this,"leeway",void 0),h(this,"requestMode",void 0),h(this,"provider",void 0),h(this,"retries",void 0),h(this,"retryDelay",void 0),h(this,"storage",void 0),!t)throw new TypeError("Missing `config`");if(!n)throw new TypeError("Provider is not defined");if(!r)throw new TypeError("Storage is not defined");this.storage=r,this.provider=n,this.fetchFn=l.a,this.fetchOpts={delay:t.retryDelay||p,retries:t.retries||d},this.leeway=t.leeway||v,this.requestMode=t.requestMode||"id";var o=this._createId(t.audience,t.label),i=o.id,c=o.label;if(this.label=c,this.id=i,!this.id)throw new TypeError("Failed to configure account. Id is not present")}return f(e,[{key:"_createId",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:".";if(!e)throw new TypeError("`audience` is absent");if(!t)throw new TypeError("`label` is absent");return{id:"".concat(t).concat(n).concat(e),label:t}}},{key:"_requestLabel",value:function(){return"label"===this.requestMode?this.label:this.id}},{key:"load",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id;return n?Promise.resolve(function(){return e.storage.getItem(n)}).then(function(e){var t=e();if(!t)throw new TypeError("Could not load data");return Object(l.d)(t)}):Promise.reject(new TypeError("`label` is absent"))}},{key:"remove",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id;return n?this.load(n).then(function(t){return e.storage.removeItem(n),t}):Promise.reject(new TypeError("`label` is absent"))}},{key:"store",value:function(e){var t=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=n||this.id;if(!r)return Promise.reject(new TypeError("`label` is absent"));var o=e,i=0;i=o.expires_time?o.expires_time:Object(l.b)(o.expires_in,Date.now());var c=a({},o,{expires_time:i});return Promise.resolve(c).then(function(e){return t.storage.setItem(r,JSON.stringify(e)),e})}},{key:"account",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.account;return this.tokenData(n).then(function(t){var n=t.access_token;return[e._requestLabel(),n]}).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(l.f).then(l.e)}},{key:"tokenData",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.refreshAccessToken;return this.load(n).then(function(t){if(!Object(l.c)(t,Date.now(),e.leeway))return t;var i=t.refresh_token;return Promise.resolve([e._requestLabel(),i]).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(l.f).then(l.e).then(function(t){return e.load(n).then(function(n){return e.store(a({},n,{access_token:t.access_token,expires_in:t.expires_in,expires_time:Object(l.b)(t.expires_in,Date.now())}))})})})}},{key:"revokeRefreshToken",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=t||this.id,o=this.provider.revokeRefreshToken;return this.load(n).then(function(t){var n=t.refresh_token;return[e._requestLabel(),n]}).then(function(t){return e.fetchFn(function(){return o.call.apply(o,[e.provider].concat(r(t)))},e.fetchOpts)}).then(l.f).then(l.e).then(function(t){return e.load(n).then(function(n){return e.store(a({},n,{refresh_token:t.refresh_token}))})})}}],[{key:"fetchLabel",value:function(e,t,n){var r=e.refresh_token;if(!r)throw new TypeError("`refresh_token` is absent");if(!t)throw new TypeError("Provider is not defined");var o={delay:p,retries:d};return Object(l.a)(function(){return t.refreshAccessToken(n||"me",r)},o).then(l.f).then(l.e).then(function(e){return Object(l.a)(function(){return t.account(n||"me",e.access_token)},o).then(l.f).then(l.e)})}}]),e}()},function(e,t,n){"use strict";n.d(t,"a",function(){return r}),n.d(t,"c",function(){return o}),n.d(t,"b",function(){return i}),n.d(t,"f",function(){return c}),n.d(t,"e",function(){return a}),n.d(t,"d",function(){return u});var r=function(e,t){if(!e)throw new TypeError("Missing 'requestFn': ".concat(e));return new Promise(function(n,r){var o=[];!function i(c){c<1?r(o):fetch(e()).then(function(e){return n(e)}).catch(function(e){o.push(e),setTimeout(function(){i(c-1)},t.delay||1e3)})}(t.retries||3)})},o=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:3e3;if(!t||isNaN(t))throw new TypeError("`now` is absent");return function(e){return!e||!e.expires_time||t>Number(e.expires_time)-n}(e)},i=function(e,t){if(void 0===e)throw new TypeError("`expires_in` is absent");if(!t||isNaN(t))throw new TypeError("`now` is absent");var n=Number(e);if(isNaN(n)||n<=0)throw new TypeError("Wrong `expires_in` value");return t+1e3*(n||0)},c=function(e){if(e.status&&e.status>=200&&e.status<300)return e;throw new Error(e.statusText||"Invalid request. Status: ".concat(e.status))},a=function(e){if(!e)throw new TypeError("`response` is absent");try{return e.json()}catch(e){throw new Error("Response is not a JSON")}},u=function(e){var t="function"==typeof e?e():e;if("string"!=typeof t)throw new TypeError("Could not parse");return new Promise(function(e,n){try{e(JSON.parse(t))}catch(e){n(e)}})}},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),e}function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"a",function(){return a});var a=function(){function e(t){var n=this;if(r(this,e),c(this,"endpoint",void 0),c(this,"accountEndpoint",void 0),c(this,"authnEndpoint",void 0),!t)throw new TypeError("Missing provider configuration");this.endpoint=t.endpoint,this.accountEndpoint=function(e){if(!n.endpoint&&!e.accountEndpoint)throw new TypeError("Could not resolve account endpoint");return e.accountEndpoint&&"function"==typeof e.accountEndpoint?e.accountEndpoint():e.accountEndpoint||"".concat(n.endpoint,"/accounts")}(t),this.authnEndpoint=function(e){if(!n.endpoint&&!e.authnEndpoint)throw new TypeError("Could not resolve authentication endpoint");return e.authnEndpoint&&"function"==typeof e.authnEndpoint?e.authnEndpoint():e.authnEndpoint||"".concat(n.endpoint,"/authn")}(t)}return i(e,[{key:"refreshAccessToken",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'refreshToken': ".concat(t));var n="".concat(this.accountEndpoint,"/").concat(e,"/refresh");return new Request(n,{method:"POST",headers:{Authorization:"Bearer ".concat(t)}})}},{key:"revokeRefreshToken",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'refreshToken': ".concat(t));var n="".concat(this.accountEndpoint,"/").concat(e,"/revoke");return new Request(n,{method:"POST",headers:{Authorization:"Bearer ".concat(t)}})}},{key:"account",value:function(e,t){if(!e)throw new TypeError("Incorrect parameter `label`");if(!t)throw new TypeError("Incorrect parameter 'accessToken': ".concat(t));var n="".concat(this.authnEndpoint,"/").concat(e);return new Request(n,{method:"GET",headers:{Authorization:"Bearer ".concat(t)}})}}]),e}()}])}); |
@@ -19,3 +19,3 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
import { fetchRetry, isExpired, validResponse, parsedResponse, parse } from './utils/index'; | ||
import { fetchRetry, isExpired, getExpiresTime, validResponse, parsedResponse, parse } from './utils/index'; | ||
var MAX_AJAX_RETRY = 3; | ||
@@ -103,3 +103,3 @@ var AJAX_RETRY_DELAY = 1000; | ||
var value = fn(); | ||
if (!value) throw new TypeError('Can not load data'); | ||
if (!value) throw new TypeError('Could not load data'); | ||
return parse(value); | ||
@@ -130,18 +130,16 @@ }); | ||
if (!label) return Promise.reject(new TypeError('`label` is absent')); | ||
return Promise.resolve(data).then(function (_) { | ||
var expires_time = 0; // eslint-disable-line semi | ||
var _ = data; | ||
var expires_time = 0; // eslint-disable-line semi | ||
if (_.expires_in) { | ||
var expin = Number(_.expires_in); | ||
if (isNaN(expin)) throw new TypeError('Wrong `expires_in` value'); | ||
expires_time = Date.now() + (expin || 0) * 1e3; | ||
} | ||
if (!_.expires_time) expires_time = getExpiresTime(_.expires_in, Date.now()); // eslint-disable-next-line prefer-destructuring | ||
else expires_time = _.expires_time; | ||
var token = _objectSpread({}, _, { | ||
expires_time: expires_time | ||
}); | ||
var token = _objectSpread({}, _, { | ||
expires_time: expires_time | ||
}); | ||
_this3.storage.setItem(label, JSON.stringify(token)); | ||
return Promise.resolve(token).then(function (tokenData) { | ||
_this3.storage.setItem(label, JSON.stringify(tokenData)); | ||
return token; | ||
return tokenData; | ||
}); | ||
@@ -175,3 +173,3 @@ } | ||
return this.load(label).then(function (maybeValidTokens) { | ||
var expired = isExpired(maybeValidTokens, _this5.leeway); | ||
var expired = isExpired(maybeValidTokens, Date.now(), _this5.leeway); | ||
if (!expired) return maybeValidTokens; | ||
@@ -189,3 +187,4 @@ var refresh_token = maybeValidTokens.refresh_token; // eslint-disable-next-line promise/no-nesting | ||
access_token: _.access_token, | ||
expires_in: _.expires_in | ||
expires_in: _.expires_in, | ||
expires_time: getExpiresTime(_.expires_in, Date.now()) | ||
})); | ||
@@ -192,0 +191,0 @@ }); |
@@ -26,8 +26,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _accountEndpoint = function _accountEndpoint(conf) { | ||
if (!_this.endpoint && !conf.accountEndpoint) throw new TypeError('Can not resolve account endpoint'); | ||
if (!_this.endpoint && !conf.accountEndpoint) throw new TypeError('Could not resolve account endpoint'); | ||
return conf.accountEndpoint && typeof conf.accountEndpoint === 'function' ? conf.accountEndpoint() : conf.accountEndpoint || "".concat(_this.endpoint, "/accounts"); | ||
}; | ||
}; // eslint-disable-line semi | ||
var _authnEndpoint = function _authnEndpoint(conf) { | ||
if (!_this.endpoint && !conf.authnEndpoint) throw new TypeError('Can not resolve authentication endpoint'); | ||
if (!_this.endpoint && !conf.authnEndpoint) throw new TypeError('Could not resolve authentication endpoint'); | ||
return conf.authnEndpoint && typeof conf.authnEndpoint === 'function' ? conf.authnEndpoint() : conf.authnEndpoint || "".concat(_this.endpoint, "/authn"); | ||
@@ -34,0 +35,0 @@ }; |
@@ -28,7 +28,8 @@ export var fetchRetry = function fetchRetry(requestFn, opts) { | ||
export var isExpired = function isExpired(data) { | ||
var leeway = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3e3; | ||
export var isExpired = function isExpired(data, now) { | ||
var leeway = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3e3; | ||
if (!now || isNaN(now)) throw new TypeError('`now` is absent'); | ||
var _isExpired = function _isExpired(x) { | ||
return !x || !x.expires_time || Date.now() > Number(x.expires_time) - leeway; | ||
return !x || !x.expires_time || now > Number(x.expires_time) - leeway; | ||
}; | ||
@@ -38,2 +39,9 @@ | ||
}; | ||
export var getExpiresTime = function getExpiresTime(expires_in, now) { | ||
if (typeof expires_in === 'undefined') throw new TypeError('`expires_in` is absent'); | ||
if (!now || isNaN(now)) throw new TypeError('`now` is absent'); | ||
var expin = Number(expires_in); | ||
if (isNaN(expin) || expin <= 0) throw new TypeError('Wrong `expires_in` value'); | ||
return now + (expin || 0) * 1e3; | ||
}; | ||
export var validResponse = function validResponse(response) { | ||
@@ -57,3 +65,3 @@ if (response.status && response.status >= 200 && response.status < 300) { | ||
var it = typeof fn === 'function' ? fn() : fn; | ||
if (typeof it !== 'string') throw new TypeError('Can not parse'); | ||
if (typeof it !== 'string') throw new TypeError('Could not parse'); | ||
return new Promise(function (resolve, reject) { | ||
@@ -60,0 +68,0 @@ try { |
{ | ||
"name": "@netology-group/account", | ||
"version": "2.3.0-rc.2", | ||
"version": "2.3.0", | ||
"description": "account client", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -37,3 +37,3 @@ Account client | ||
#### "Me" mode | ||
#### Clarify label | ||
@@ -40,0 +40,0 @@ `id` & `label` modes require a label to be known. But you actually may not. |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
34157
368
0