@reactway/api-builder
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
export declare const REQUEST_QUEUE_LIMIT = 5; | ||
export declare const STORAGE_OAUTH_KEY = "OAuth"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.REQUEST_QUEUE_LIMIT = 5; | ||
exports.STORAGE_OAUTH_KEY = "OAuth"; | ||
//# sourceMappingURL=constants.js.map |
@@ -73,2 +73,4 @@ import { StrictEventEmitter } from "strict-event-emitter-types"; | ||
tokenRenewalEnabled?: boolean; | ||
storageKey?: string; | ||
storage?: Storage; | ||
} | ||
@@ -75,0 +77,0 @@ export interface OAuthResponseDto { |
@@ -1,2 +0,3 @@ | ||
import { ApiRequestBinaryBody } from "./contracts"; | ||
import { ApiRequestBinaryBody, OAuthResponseDto } from "./contracts"; | ||
export declare function isBinaryBody(body: any): body is ApiRequestBinaryBody; | ||
export declare function isOAuthResponse(value: any): value is OAuthResponseDto; |
@@ -8,2 +8,7 @@ "use strict"; | ||
exports.isBinaryBody = isBinaryBody; | ||
// tslint:disable-next-line:no-any | ||
function isOAuthResponse(value) { | ||
return value != null && value.token_type != null && value.access_token != null; | ||
} | ||
exports.isOAuthResponse = isOAuthResponse; | ||
//# sourceMappingURL=helpers.js.map |
@@ -11,3 +11,3 @@ /// <reference types="node" /> | ||
constructor(configuration: OAuthIdentityConfiguration); | ||
private loginData; | ||
private oAuth; | ||
private renewalTimeoutId; | ||
@@ -22,5 +22,5 @@ /** | ||
private renewToken; | ||
private setLoginData; | ||
private setOAuthData; | ||
private renewalTime; | ||
} | ||
export {}; |
@@ -7,2 +7,4 @@ "use strict"; | ||
var contracts_1 = require("../contracts"); | ||
var constants_1 = require("../constants"); | ||
var helpers_1 = require("../helpers"); | ||
var IdentityEventEmitter = events_1.EventEmitter; | ||
@@ -18,2 +20,14 @@ var OAuthIdentity = /** @class */ (function (_super) { | ||
_this.timeBeforeExpires = 120; | ||
if (_this.configuration.storage == null) { | ||
return _this; | ||
} | ||
var storageKey = _this.configuration.storageKey != null ? _this.configuration.storageKey : constants_1.STORAGE_OAUTH_KEY; | ||
var storageOAuthItem = _this.configuration.storage.getItem(storageKey); | ||
if (storageOAuthItem == null) { | ||
return _this; | ||
} | ||
var parsedItem = JSON.parse(storageOAuthItem); | ||
if (helpers_1.isOAuthResponse(parsedItem)) { | ||
_this.oAuth = parsedItem; | ||
} | ||
return _this; | ||
@@ -45,3 +59,3 @@ } | ||
this.emit("login"); | ||
_a = this.setLoginData; | ||
_a = this.setOAuthData; | ||
return [4 /*yield*/, response.json()]; | ||
@@ -61,3 +75,3 @@ case 2: | ||
case 0: | ||
if (this.loginData == null) { | ||
if (this.oAuth == null) { | ||
throw new Error("Identity: login data is not set yet."); | ||
@@ -71,3 +85,3 @@ } | ||
grant_type: "refresh_token", | ||
refresh_token: this.loginData.refresh_token | ||
refresh_token: this.oAuth.refresh_token | ||
}) | ||
@@ -81,5 +95,9 @@ })]; | ||
} | ||
this.loginData = undefined; | ||
this.oAuth = undefined; | ||
clearTimeout(this.renewalTimeoutId); | ||
this.emit("logout"); | ||
if (this.configuration.storage == null) { | ||
return [2 /*return*/]; | ||
} | ||
this.configuration.storage.clear(); | ||
return [2 /*return*/]; | ||
@@ -94,3 +112,3 @@ } | ||
return tslib_1.__generator(this, function (_a) { | ||
if (this.loginData == null) { | ||
if (this.oAuth == null) { | ||
throw new Error("Identity: login data is not set yet."); | ||
@@ -102,3 +120,3 @@ } | ||
authHeader = { | ||
Authorization: this.loginData.token_type + " " + this.loginData.access_token | ||
Authorization: this.oAuth.token_type + " " + this.oAuth.access_token | ||
}; | ||
@@ -132,3 +150,3 @@ request.headers = tslib_1.__assign({}, request.headers, authHeader); | ||
} | ||
_a = this.setLoginData; | ||
_a = this.setOAuthData; | ||
return [4 /*yield*/, response.json()]; | ||
@@ -142,13 +160,17 @@ case 2: | ||
}; | ||
OAuthIdentity.prototype.setLoginData = function (loginData) { | ||
OAuthIdentity.prototype.setOAuthData = function (oAuthData) { | ||
var _this = this; | ||
if (loginData.expires_in == null) { | ||
if (oAuthData.expires_in == null) { | ||
throw Error("Not supported without expiration time."); | ||
} | ||
this.loginData = loginData; | ||
this.oAuth = oAuthData; | ||
if (this.configuration.storage != null) { | ||
var storageKey = this.configuration.storageKey != null ? this.configuration.storageKey : constants_1.STORAGE_OAUTH_KEY; | ||
this.configuration.storage.setItem(storageKey, JSON.stringify(oAuthData)); | ||
} | ||
// If response do not have `refresh_token` we are not using renewal mechanism. | ||
if (loginData.refresh_token == null) { | ||
if (oAuthData.refresh_token == null) { | ||
return; | ||
} | ||
var refreshToken = loginData.refresh_token; | ||
var refreshToken = oAuthData.refresh_token; | ||
// If response has `refresh_token` but we do not want to use renewal mechanism. | ||
@@ -162,3 +184,3 @@ if (this.configuration.tokenRenewalEnabled === false) { | ||
} | ||
var timeoutNumber = this.renewalTime(loginData.expires_in); | ||
var timeoutNumber = this.renewalTime(oAuthData.expires_in); | ||
this.renewalTimeoutId = window.setTimeout(function () { return _this.renewToken(refreshToken); }, timeoutNumber); | ||
@@ -165,0 +187,0 @@ }; |
{ | ||
"name": "@reactway/api-builder", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"description": "An easy api client builder for applications with identity.", | ||
@@ -29,2 +29,4 @@ "main": "dist/index.js", | ||
], | ||
"repository": "github:reactway/api-builder", | ||
"homepage": "https://github.com/reactway/api-builder", | ||
"license": "MIT", | ||
@@ -39,2 +41,3 @@ "devDependencies": { | ||
"jest-junit": "^6.3.0", | ||
"jest-localstorage-mock": "^2.4.0", | ||
"node-fetch": "^2.3.0", | ||
@@ -59,2 +62,5 @@ "simplr-tslint": "^1.0.0-alpha.14", | ||
], | ||
"setupFiles": [ | ||
"./config/jest/local-storage-mock.ts" | ||
], | ||
"collectCoverage": true, | ||
@@ -61,0 +67,0 @@ "testRegex": "/__tests__/.*\\.(test|spec).(ts|tsx)$", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
41532
544
1
14