featurehub-repository
Version:
Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.
99 lines • 3.41 kB
JavaScript
import { ClientFeatureRepository } from './client_feature_repository';
import { ClientEvalFeatureContext, ServerEvalFeatureContext } from './client_context';
import { FeatureHubPollingClient } from './polling_sdk';
export class FHLog {
constructor() {
this.log = (...args) => { console.log(args); };
this.error = (...args) => { console.error(args); };
}
}
export const fhLog = new FHLog();
export 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 ClientEvalFeatureContext(repository, this, edgeService(this._repository, this)) :
new 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) => 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 ClientFeatureRepository();
}
return this._repository;
}
url() {
return this._url;
}
}
EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => new FeatureHubPollingClient(repository, config, 6000);
//# sourceMappingURL=feature_hub_config.js.map