UNPKG

@splitsoftware/splitio-commons

Version:
57 lines (56 loc) 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trackMethodFactory = void 0; var thenable_1 = require("../utils/promise/thenable"); var key_1 = require("../utils/key"); var trafficTypeExistence_1 = require("../utils/inputValidation/trafficTypeExistence"); var inputValidation_1 = require("../utils/inputValidation"); var constants_1 = require("../utils/constants"); var mode_1 = require("../utils/settingsValidation/mode"); /** * Creates a standalone `track` function with input validation. * Reusable by FF SDK client, Configs SDK, and thin-client SDK. */ function trackMethodFactory(params, warnTTExistence) { if (warnTTExistence === void 0) { warnTTExistence = true; } var settings = params.settings, definitions = params.storage.definitions, telemetryTracker = params.telemetryTracker, eventTracker = params.eventTracker, readinessManager = params.sdkReadinessManager.readinessManager; var log = settings.log, mode = settings.mode; var isAsync = (0, mode_1.isConsumerMode)(mode); return function track(maybeKey, maybeTT, maybeEvent, maybeEventValue, maybeProperties) { // Input validation var key = (0, inputValidation_1.validateKey)(log, maybeKey, constants_1.TRACK_FN_LABEL); var trafficTypeName = (0, inputValidation_1.validateTrafficType)(log, maybeTT, constants_1.TRACK_FN_LABEL); var eventTypeId = (0, inputValidation_1.validateEvent)(log, maybeEvent, constants_1.TRACK_FN_LABEL); var value = (0, inputValidation_1.validateEventValue)(log, maybeEventValue, constants_1.TRACK_FN_LABEL); var _a = (0, inputValidation_1.validateEventProperties)(log, maybeProperties, constants_1.TRACK_FN_LABEL), properties = _a.properties, size = _a.size; var isNotDestroyed = (0, inputValidation_1.validateIfNotDestroyed)(log, readinessManager, constants_1.TRACK_FN_LABEL); if (!(isNotDestroyed && key && trafficTypeName && eventTypeId && value !== false && properties !== false)) { return isAsync ? Promise.resolve(false) : false; } // Core logic var stopTelemetryTracker = telemetryTracker.trackEval(constants_1.TRACK); var eventData = { eventTypeId: eventTypeId, trafficTypeName: trafficTypeName, value: value, timestamp: Date.now(), key: (0, key_1.getMatching)(key), properties: properties }; // This may be async but we only warn, we don't actually care if it is valid or not in terms of queueing the event. if (warnTTExistence) (0, trafficTypeExistence_1.validateTrafficTypeExistence)(log, readinessManager, definitions, mode, trafficTypeName, constants_1.TRACK_FN_LABEL); var result = eventTracker.track(eventData, size); if ((0, thenable_1.thenable)(result)) { return result.then(function (result) { stopTelemetryTracker(); return result; }); } else { stopTelemetryTracker(); return result; } }; } exports.trackMethodFactory = trackMethodFactory;