UNPKG

featurehub-repository

Version:

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

165 lines 5.52 kB
"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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientEvalFeatureContext = exports.ServerEvalFeatureContext = exports.BaseClientContext = void 0; const feature_hub_config_1 = require("./feature_hub_config"); class BaseClientContext { constructor(repository, config) { this._attributes = new Map(); this._repository = repository; this._config = config; } userKey(value) { this._attributes.set('userkey', [value]); return this; } sessionKey(value) { this._attributes.set('session', [value]); return this; } country(value) { this._attributes.set('country', [value]); return this; } device(value) { this._attributes.set('device', [value]); return this; } platform(value) { this._attributes.set('platform', [value]); return this; } version(version) { this._attributes.set('version', [version]); return this; } attribute_value(key, value) { this._attributes.set(key, [value]); return this; } attribute_values(key, values) { this._attributes.set(key, values); return this; } clear() { this._attributes.clear(); return this; } getAttr(key, defaultValue) { if (this._attributes.has(key)) { return this._attributes.get(key)[0]; } return defaultValue; } defaultPercentageKey() { return this._attributes.has('session') ? this.getAttr('session') : this.getAttr('userkey'); } isEnabled(name) { return this.feature(name).isEnabled(); } isSet(name) { return this.feature(name).isSet(); } getNumber(name) { return this.feature(name).getNumber(); } getString(name) { return this.feature(name).getString(); } getJson(name) { const val = this.feature(name).getRawJson(); return val === undefined ? undefined : JSON.parse(val); } getRawJson(name) { return this.feature(name).getRawJson(); } getFlag(name) { return this.feature(name).getFlag(); } getBoolean(name) { return this.feature(name).getBoolean(); } repository() { return this._repository; } logAnalyticsEvent(action, other, user) { if (user == null) { user = this.getAttr('userkey'); } if (user != null) { if (other == null) { other = new Map(); } other.set('cid', user); } this._repository.logAnalyticsEvent(action, other); } } exports.BaseClientContext = BaseClientContext; class ServerEvalFeatureContext extends BaseClientContext { constructor(repository, config, edgeServiceSupplier) { super(repository, config); this._edgeServiceSupplier = edgeServiceSupplier; } build() { return __awaiter(this, void 0, void 0, function* () { const newHeader = Array.from(this._attributes.entries()).map((key) => key[0] + '=' + encodeURIComponent(key[1].join(','))).sort().join(','); if (newHeader !== this._xHeader) { this._xHeader = newHeader; this._repository.notReady(); if (this._currentEdge != null && this._currentEdge.requiresReplacementOnHeaderChange()) { this._currentEdge.close(); this._currentEdge = null; } } if (this._currentEdge == null) { this._currentEdge = this._edgeServiceSupplier(); } yield this._currentEdge.contextChange(this._xHeader).catch((e) => feature_hub_config_1.fhLog.error(`Failed to connect to FeatureHub Edge to refresh context ${e}`)); return this; }); } close() { if (this._currentEdge) { this._currentEdge.close(); } } edgeService() { return this._currentEdge; } feature(name) { return this._repository.feature(name); } } exports.ServerEvalFeatureContext = ServerEvalFeatureContext; class ClientEvalFeatureContext extends BaseClientContext { constructor(repository, config, edgeService) { super(repository, config); this._edgeService = edgeService; } build() { return __awaiter(this, void 0, void 0, function* () { this._edgeService.poll(); return this; }); } close() { this._edgeService.close(); } edgeService() { return this._edgeService; } feature(name) { return this._repository.feature(name).withContext(this); } } exports.ClientEvalFeatureContext = ClientEvalFeatureContext; //# sourceMappingURL=client_context.js.map