UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.

65 lines 2.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlaygroundService = void 0; const api_token_1 = require("../../lib/types/models/api-token"); const offline_unleash_client_1 = require("../util/offline-unleash-client"); class PlaygroundService { constructor(config, { featureToggleServiceV2, segmentService, }) { this.logger = config.getLogger('services/playground-service.ts'); this.featureToggleService = featureToggleServiceV2; this.segmentService = segmentService; } async evaluateQuery(projects, environment, context) { const [features, segments] = await Promise.all([ this.featureToggleService.getClientFeatures({ project: projects === api_token_1.ALL ? undefined : projects, environment, }, true), this.segmentService.getActive(), ]); const [head, ...rest] = features; if (!head) { return []; } else { const client = await (0, offline_unleash_client_1.offlineUnleashClient)({ features: [head, ...rest], context, logError: this.logger.error, segments, }); const variantsMap = features.reduce((acc, feature) => { acc[feature.name] = feature.variants; return acc; }, {}); const clientContext = { ...context, currentTime: context.currentTime ? new Date(context.currentTime) : undefined, }; const output = await Promise.all(client .getFeatureToggleDefinitions() .map(async (feature) => { const strategyEvaluationResult = client.isEnabled(feature.name, clientContext); const isEnabled = strategyEvaluationResult.result === true && feature.enabled; return { isEnabled, isEnabledInCurrentEnvironment: feature.enabled, strategies: { result: strategyEvaluationResult.result, data: strategyEvaluationResult.strategies, }, projectId: await this.featureToggleService.getProjectId(feature.name), variant: client.getVariant(feature.name, clientContext), name: feature.name, variants: variantsMap[feature.name] || [], }; })); return output; } } } exports.PlaygroundService = PlaygroundService; //# sourceMappingURL=playground-service.js.map