UNPKG

@questlabs/core

Version:
129 lines (128 loc) 7 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.EntityFeatureFlag = void 0; const constants_1 = require("../../utils/constants"); const helperFunctions_1 = require("../../utils/helperFunctions"); class EntityFeatureFlag { constructor(questSdk) { this.questSdk = questSdk; } getAllFeatureFlagsForEntity({ entityId } = {}, options) { return __awaiter(this, void 0, void 0, function* () { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/featureFlags`; const queryParams = Object.assign(Object.assign({}, (userId && { userId })), options === null || options === void 0 ? void 0 : options._queryParams); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } addNewFeatureFlagForEntity({ entityId, flagName, description, isEnabled }, options) { return __awaiter(this, void 0, void 0, function* () { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, flagName }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/featureFlags`; const body = Object.assign(Object.assign(Object.assign({ flagName }, (description && { description })), (isEnabled && { isEnabled })), options === null || options === void 0 ? void 0 : options._body); const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams, body, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } toggleFeatureFlagForEntity({ entityId, flagName }, options) { return __awaiter(this, void 0, void 0, function* () { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, flagName }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/featureFlags/${flagName}/toggle`; const queryParams = Object.assign(Object.assign(Object.assign({}, (userId && { userId })), options === null || options === void 0 ? void 0 : options._queryParams), { flagName }); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams, body: options === null || options === void 0 ? void 0 : options._body, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } terminateFeatureFlagForEntity({ entityId, flagName }, options) { return __awaiter(this, void 0, void 0, function* () { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, flagName }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/featureFlags/${flagName}/terminate`; const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } getAllFeatureFlagsStream({ entityId } = {}, options) { try { const defaultEntityId = entityId || this.questSdk.getEntityId(); const { apiKey, apiSecret } = this.questSdk.getApiKeys(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId }); const url = `${this.questSdk.getBaseUrl()}/entities/${defaultEntityId}/featureFlags/stream?apiKey=${apiKey}&apiSecret=${apiSecret}`; const eventSource = new EventSource(url); return eventSource; } catch (error) { this.questSdk.captureSentryException(error); throw error; } } } exports.EntityFeatureFlag = EntityFeatureFlag;