UNPKG

featurehub-repository

Version:

Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.

104 lines 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EdgeFeatureHubConfig = exports.fhLog = exports.FHLog = void 0; const client_feature_repository_1 = require("./client_feature_repository"); const client_context_1 = require("./client_context"); const polling_sdk_1 = require("./polling_sdk"); class FHLog { constructor() { this.log = (...args) => { console.log(args); }; this.error = (...args) => { console.error(args); }; } } exports.FHLog = FHLog; exports.fhLog = new FHLog(); class EdgeFeatureHubConfig { constructor(host, apiKey) { this._edgeServices = []; this._apiKey = apiKey; this._host = host; if (apiKey == null || host == null) { throw new Error('apiKey and host must not be null'); } this._apiKeys = [apiKey]; this._clientEval = this._apiKey.includes('*'); if (!this._host.endsWith('/')) { this._host += '/'; } if (this._host.endsWith('/features/')) { this._host = this._host.substring(0, this._host.length - '/features/'.length); } this._url = this._host + 'features/' + this._apiKey; } addReadynessListener(listener) { this.repository().addReadynessListener(listener); } addAnalyticCollector(collector) { this.repository().addAnalyticCollector(collector); } addValueInterceptor(interceptor) { this.repository().addValueInterceptor(interceptor); } get readyness() { return this.repository().readyness; } apiKey(apiKey) { this._apiKeys.push(apiKey); return this; } clientEvaluated() { return this._apiKey.includes('*'); } getApiKeys() { return Object.assign([], this._apiKeys); } getHost() { return this._host; } newContext(repository, edgeService) { repository = repository || this.repository(); edgeService = edgeService || this.edgeServiceProvider(); return this._clientEval ? new client_context_1.ClientEvalFeatureContext(repository, this, edgeService(this._repository, this)) : new client_context_1.ServerEvalFeatureContext(repository, this, () => this._createEdgeService(edgeService)); } _createEdgeService(edgeServSupplier) { const es = edgeServSupplier(this._repository, this); this._edgeServices.push(es); return es; } close() { this._edgeServices.forEach((es) => { es.close(); }); } init() { this.repository(); this._createEdgeService(this.edgeServiceProvider()).poll().catch((e) => exports.fhLog.error(`Failed to connect to FeatureHub Edge ${e}`)); return this; } edgeServiceProvider(edgeServ) { if (edgeServ != null) { this._edgeService = edgeServ; } else if (this._edgeService == null) { this._edgeService = EdgeFeatureHubConfig.defaultEdgeServiceSupplier; } return this._edgeService; } repository(repository) { if (repository != null) { this._repository = repository; } else if (this._repository == null) { this._repository = new client_feature_repository_1.ClientFeatureRepository(); } return this._repository; } url() { return this._url; } } exports.EdgeFeatureHubConfig = EdgeFeatureHubConfig; EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => new polling_sdk_1.FeatureHubPollingClient(repository, config, 6000); //# sourceMappingURL=feature_hub_config.js.map