@awhere/api
Version:
The awesome aWhere API for JavaScript.
441 lines • 18.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialType = void 0;
var APIKey_1 = __importDefault(require("../credential/APIKey"));
var BasicAuth_1 = __importDefault(require("../credential/BasicAuth"));
var OAuth_1 = __importDefault(require("../credential/OAuth"));
var BearerToken_1 = __importDefault(require("../credential/BearerToken"));
var CredentialType;
(function (CredentialType) {
CredentialType["apiKey"] = "apiKey";
CredentialType["oAuth"] = "oAuth";
CredentialType["basicAuth"] = "basicAuth";
CredentialType["bearerToken"] = "bearerToken";
})(CredentialType || (exports.CredentialType = CredentialType = {}));
var IdentityManager = /** @class */ (function () {
function IdentityManager() {
this._apiKeys = {};
this._oAuths = {};
this._basicAuths = {};
this._bearerTokens = {};
this._credentials = [];
this._credentialsIndexes = {};
}
Object.defineProperty(IdentityManager.prototype, "allAPIKeys", {
get: function () {
var apiKeys = [];
for (var _i = 0, _a = Object.keys(this._apiKeys); _i < _a.length; _i++) {
var key = _a[_i];
apiKeys.push(this._apiKeys[key]);
}
return apiKeys;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "allOAuths", {
get: function () {
var oAuths = [];
for (var _i = 0, _a = Object.keys(this._oAuths); _i < _a.length; _i++) {
var key = _a[_i];
oAuths.push(this._oAuths[key]);
}
return oAuths;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "allBasicAuths", {
get: function () {
var auths = [];
for (var _i = 0, _a = Object.keys(this._basicAuths); _i < _a.length; _i++) {
var key = _a[_i];
auths.push(this._basicAuths[key]);
}
return auths;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "allBearerTokens", {
get: function () {
var auths = [];
for (var _i = 0, _a = Object.keys(this._bearerTokens); _i < _a.length; _i++) {
var key = _a[_i];
auths.push(this._bearerTokens[key]);
}
return auths;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultAPIKey", {
get: function () {
if (this._defaultAPIKey) {
return this._apiKeys[this._defaultAPIKey];
}
else {
return undefined;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultOAuth", {
get: function () {
if (this._defaultOAuth) {
return this._oAuths[this._defaultOAuth];
}
else {
return undefined;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultBasicAuth", {
get: function () {
if (this._defaultBasicAuth) {
return this._basicAuths[this._defaultBasicAuth];
}
else {
return undefined;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultBearerToken", {
get: function () {
if (this._defaultBearerToken) {
return this._bearerTokens[this._defaultBearerToken];
}
else {
return undefined;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultCredentialType", {
get: function () {
return this._defaultCredentialType;
},
set: function (value) {
this._defaultCredentialType = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IdentityManager.prototype, "defaultCredential", {
get: function () {
var credential;
if (this._defaultCredentialType) {
switch (this._defaultCredentialType) {
case CredentialType.apiKey:
credential = this._defaultAPIKey ? this._apiKeys[this._defaultAPIKey] : undefined;
break;
case CredentialType.oAuth:
credential = this._defaultOAuth ? this._oAuths[this._defaultOAuth] : undefined;
break;
case CredentialType.basicAuth:
credential = this._defaultBasicAuth ? this._basicAuths[this._defaultBasicAuth] : undefined;
break;
case CredentialType.bearerToken:
credential = this._defaultBearerToken ? this._bearerTokens[this._defaultBearerToken] : undefined;
break;
}
}
return credential;
},
enumerable: false,
configurable: true
});
IdentityManager.prototype.setDefaultAPIKey = function (key) {
this._defaultAPIKey = key;
};
IdentityManager.prototype.setDefaultOAuth = function (appId) {
this._defaultOAuth = appId;
};
IdentityManager.prototype.setDefaultBasicAuth = function (auth) {
if (typeof auth === 'string') {
this._defaultBasicAuth = auth;
}
else if (auth instanceof BasicAuth_1.default) {
this._defaultBasicAuth = auth.username;
}
};
IdentityManager.prototype.setDefaultBearerToken = function (auth) {
if (typeof auth === 'string') {
this._defaultBearerToken = auth;
}
else if (auth instanceof BearerToken_1.default) {
this._defaultBearerToken = auth.token;
}
};
IdentityManager.prototype.setDefaultCredential = function (credential) {
if (credential instanceof APIKey_1.default) {
this._defaultCredentialType = CredentialType.apiKey;
this.setDefaultAPIKey(credential.key);
}
else if (credential instanceof OAuth_1.default) {
this._defaultCredentialType = CredentialType.oAuth;
this.setDefaultOAuth(credential.appId);
}
else if (credential instanceof BasicAuth_1.default) {
this._defaultCredentialType = CredentialType.basicAuth;
this.setDefaultBasicAuth(credential.username);
}
else if (credential instanceof BearerToken_1.default) {
this._defaultCredentialType = CredentialType.bearerToken;
this.setDefaultBearerToken(credential.token);
}
};
IdentityManager.prototype.getAPIKey = function (key) {
return this._apiKeys[key];
};
IdentityManager.prototype.registerAPIKeys = function (apiKeys) {
var _this = this;
apiKeys.forEach(function (apiKey) {
_this._apiKeys[apiKey.key] = apiKey;
});
};
IdentityManager.prototype.destroyAPIKeys = function (keys) {
var _this = this;
if (keys) {
keys.forEach(function (key) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
delete this._apiKeys[key];
return [2 /*return*/];
});
}); });
}
else {
this._apiKeys = {};
}
};
IdentityManager.prototype.getOAuth = function (appId) {
return this._oAuths[appId];
};
IdentityManager.prototype.registerOAuths = function (oAuths) {
var _this = this;
oAuths.forEach(function (oAuth) {
_this._oAuths[oAuth.appId] = oAuth;
});
};
IdentityManager.prototype.destroyOAuths = function (appIds) {
var _this = this;
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var _i, _a, key, error_1;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, , 8]);
if (!appIds) return [3 /*break*/, 1];
appIds.forEach(function (appId) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._oAuths[appId].destroy()];
case 1:
_a.sent();
delete this._oAuths[appId];
return [2 /*return*/];
}
});
}); });
return [3 /*break*/, 6];
case 1:
_i = 0, _a = Object.keys(this._oAuths);
_b.label = 2;
case 2:
if (!(_i < _a.length)) return [3 /*break*/, 5];
key = _a[_i];
return [4 /*yield*/, this._oAuths[key].destroy()];
case 3:
_b.sent();
_b.label = 4;
case 4:
_i++;
return [3 /*break*/, 2];
case 5:
this._oAuths = {};
_b.label = 6;
case 6:
resolve(undefined);
return [3 /*break*/, 8];
case 7:
error_1 = _b.sent();
reject(error_1);
return [3 /*break*/, 8];
case 8: return [2 /*return*/];
}
});
}); });
};
IdentityManager.prototype.getBasicAuth = function (username) {
return this._basicAuths[username];
};
IdentityManager.prototype.registerBasicAuths = function (basicAuths) {
var _this = this;
basicAuths.forEach(function (basicAuth) {
_this._basicAuths[basicAuth.username] = basicAuth;
});
};
IdentityManager.prototype.destroyBasicAuths = function (auths) {
var _this = this;
if (auths) {
auths.forEach(function (auth) {
if (typeof auth === 'string') {
delete _this._basicAuths[auth];
}
else if (auth instanceof BasicAuth_1.default) {
delete _this._basicAuths[auth.username];
}
});
}
else {
this._basicAuths = {};
}
};
IdentityManager.prototype.getBearerToken = function (token) {
return this._bearerTokens[token];
};
IdentityManager.prototype.registerBearerTokens = function (bearerTokens) {
var _this = this;
bearerTokens.forEach(function (bearerToken) {
_this._bearerTokens[bearerToken.token] = bearerToken;
});
};
IdentityManager.prototype.destroyBearerTokens = function (auths) {
var _this = this;
if (auths) {
auths.forEach(function (auth) {
if (typeof auth === 'string') {
delete _this._bearerTokens[auth];
}
else if (auth instanceof BearerToken_1.default) {
delete _this._bearerTokens[auth.token];
}
});
}
else {
this._bearerTokens = {};
}
};
IdentityManager.prototype.registers = function (credentials) {
var _this = this;
credentials.forEach(function (credential) {
var index = '';
if (credential instanceof APIKey_1.default) {
index = "".concat(CredentialType.apiKey, "|").concat(credential.key);
_this._apiKeys[credential.key] = credential;
}
else if (credential instanceof BasicAuth_1.default) {
index = "".concat(CredentialType.basicAuth, "|").concat(credential.username);
_this._basicAuths[credential.username] = credential;
}
else if (credential instanceof OAuth_1.default) {
index = "".concat(CredentialType.oAuth, "|").concat(credential.appId);
_this._oAuths[credential.appId] = credential;
}
else if (credential instanceof BearerToken_1.default) {
index = "".concat(CredentialType.bearerToken, "|").concat(credential.token);
_this._bearerTokens[credential.token] = credential;
}
if (!_this._credentialsIndexes[index]) {
_this._credentialsIndexes[index] = credential;
_this._credentials.push(credential);
}
else {
console.warn('this credential is already registered.');
}
});
};
IdentityManager.prototype.destroys = function (credentials) {
var _this = this;
if (credentials) {
credentials.forEach(function (credential) {
var index = '';
if (credential instanceof APIKey_1.default) {
index = "".concat(CredentialType.apiKey, "|").concat(credential.key);
delete _this._apiKeys[credential.key];
if (_this._defaultAPIKey === credential.key) {
_this._defaultAPIKey = undefined;
}
}
else if (credential instanceof BasicAuth_1.default) {
index = "".concat(CredentialType.basicAuth, "|").concat(credential.username);
delete _this._basicAuths[credential.username];
if (_this._defaultBasicAuth === credential.username) {
_this._defaultBasicAuth = undefined;
}
}
else if (credential instanceof OAuth_1.default) {
index = "".concat(CredentialType.oAuth, "|").concat(credential.appId);
delete _this._oAuths[credential.appId];
if (_this._defaultOAuth === credential.appId) {
_this._defaultOAuth = undefined;
}
}
else if (credential instanceof BearerToken_1.default) {
index = "".concat(CredentialType.bearerToken, "|").concat(credential.token);
delete _this._bearerTokens[credential.token];
if (_this._defaultBearerToken === credential.token) {
_this._defaultBearerToken = undefined;
}
}
delete _this._credentialsIndexes[index];
var idx = _this._credentials.indexOf(credential);
if (idx !== -1) {
_this._credentials = _this._credentials.slice(0).splice(idx, 1);
}
});
}
else {
this.destroys(this._credentials);
}
};
return IdentityManager;
}());
exports.default = IdentityManager;
//# sourceMappingURL=IdentityManager.js.map