@scrimmage/rewards
Version:
Simple rewards for your app or website
110 lines (109 loc) • 4.47 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigService = void 0;
var inversify_1 = require("inversify");
var axios_1 = require("axios");
var defaultRewarderConfig = {
logLevel: 'debug',
services: {
api: 'api',
p2e: 'p2e',
fed: 'fed',
nbc: 'nbc',
},
logger: console,
httpClient: axios_1.default,
secure: true,
validateApiServerEndpoint: true,
};
var ConfigService = /** @class */ (function () {
function ConfigService() {
}
ConfigService.prototype.setConfig = function (config) {
var apiServerEndpoint = config.apiServerEndpoint, rest = __rest(config, ["apiServerEndpoint"]);
if (!apiServerEndpoint) {
throw new Error('apiServerEndpoint is required');
}
this.validateProtocol(apiServerEndpoint, config.secure);
var apiEndpoint = apiServerEndpoint.endsWith('/')
? apiServerEndpoint.slice(0, -1)
: apiServerEndpoint;
this.rewarderConfig = __assign(__assign(__assign({}, defaultRewarderConfig), rest), { apiServerEndpoint: apiEndpoint, privateKeys: [
{
alias: 'default',
value: config.privateKey,
},
] });
};
ConfigService.prototype.isConfigured = function () {
return Boolean(this.rewarderConfig);
};
ConfigService.prototype.getConfigOrThrow = function () {
if (this.isConfigured()) {
return this.rewarderConfig;
}
else {
throw new Error('Rewarder not initiated');
}
};
ConfigService.prototype.getPrivateKeyOrThrow = function (alias) {
if (alias === void 0) { alias = 'default'; }
var config = this.getConfigOrThrow();
var privateKey = config.privateKeys.find(function (key) { return key.alias === alias; });
if (!privateKey) {
throw new Error("Private key with alias [".concat(alias, "] not found"));
}
return privateKey.value;
};
ConfigService.prototype.getServiceUrl = function (service) {
var config = this.getConfigOrThrow();
return "".concat(config.apiServerEndpoint, "/").concat(service);
};
ConfigService.prototype.getNamespaceOrThrow = function () {
var config = this.getConfigOrThrow();
return config.namespace;
};
ConfigService.prototype.getHttpClientOrThrow = function () {
var config = this.getConfigOrThrow();
return config.httpClient;
};
ConfigService.prototype.validateProtocol = function (url, secure) {
var protocolRegex = secure ? /^https:\/\/.+/ : /^https?:\/\/.+/;
if (!protocolRegex.test(url)) {
throw new Error("Service URL must start with http".concat(secure ? 's' : '', "://"));
}
};
ConfigService = __decorate([
(0, inversify_1.injectable)()
], ConfigService);
return ConfigService;
}());
exports.ConfigService = ConfigService;