UNPKG

analytics-sdk-generator

Version:

Generates an SDK from an analytics descriptor file

180 lines 6.73 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Track = exports.Screen = exports.Event = exports.Feature = void 0; var TypeMapper_1 = __importDefault(require("./TypeMapper")); var Types_1 = require("./Types"); var Feature = /** @class */ (function () { function Feature(name) { this.name = name; this.screens = []; this.tracks = []; } Feature.prototype.addScreen = function (screen) { this.screens.push(screen); }; Feature.prototype.addTrack = function (track) { this.tracks.push(track); }; return Feature; }()); exports.Feature = Feature; var Event = /** @class */ (function () { function Event(definition) { this.features = []; this.tracks = []; this.screens = []; if (!('key' in definition)) { throw new Error("'key' is required"); } if (!('type' in definition)) { throw new Error("'type' is required"); } this.additionalProperties = definition.additionalProperties || false; this.loginRequired = definition.loginRequired; this.type = definition.type; this.key = definition.key; this.name = definition.name || definition.key; this.properties = this.parseProperties(definition); if ('description' in definition) { this.description = definition.description; } } Event.prototype.parseProperties = function (definition) { var propertiesObjectDefinition = { type: "object", properties: definition.properties == undefined ? {} : definition.properties, required: definition.required || [], additionalProperties: definition.additionalProperties || false }; return TypeMapper_1.default.toSpecificType(propertiesObjectDefinition); }; Event.prototype.escapeKey = function () { return this.key.replace(" ", "").replace("&", "n"); }; Event.prototype.uniqueFeaturesAndScreens = function () { var featureNamesSet = new Set(); var screenNamesSet = new Set(); if ('screens' in this) { for (var _i = 0, _a = this.screens; _i < _a.length; _i++) { var s = _a[_i]; screenNamesSet.add(s.name); for (var _b = 0, _c = s.features; _b < _c.length; _b++) { var f = _c[_b]; featureNamesSet.add(f.name); } } } if ('features' in this) { for (var _d = 0, _e = this.features; _d < _e.length; _d++) { var f = _e[_d]; featureNamesSet.add(f.name); } } var featureNames = Array.from(featureNamesSet); var screenNames = Array.from(screenNamesSet); return { features: featureNames, screens: screenNames }; }; Event.prototype.toEnumAndRequired = function (options, fallback, useFallback) { if (useFallback === void 0) { useFallback = false; } var required = true; var type; if (options.length > 0 && !useFallback) { type = { type: 'string', enum: options }; if (options.length == 1) { required = false; } } else { type = { $ref: "#/$defs/" + fallback }; } return [required, type]; }; Event.prototype.sourceToObjectType = function () { var properties = {}; var required = []; for (var _i = 0, _a = ['widget', 'element', 'action']; _i < _a.length; _i++) { var prop = _a[_i]; properties[prop] = { type: 'string' }; } var screensAndFeatures = this.uniqueFeaturesAndScreens(); var _b = this.toEnumAndRequired(screensAndFeatures.screens, "ScreenNames"), screenRequired = _b[0], screenType = _b[1]; properties['screen'] = screenType; if (screenRequired) { required.push('screen'); } var _c = this.toEnumAndRequired(screensAndFeatures.features, "FeatureNames"), featureRequired = _c[0], featureType = _c[1]; properties['feature'] = featureType; if (featureRequired) { required.push('feature'); } var definition = { type: 'object', properties: properties, required: required }; return new Types_1.ObjectType(definition); }; return Event; }()); exports.Event = Event; var Screen = /** @class */ (function (_super) { __extends(Screen, _super); function Screen(definition) { return _super.call(this, __assign(__assign({}, definition), { type: "screen" })) || this; } return Screen; }(Event)); exports.Screen = Screen; var Track = /** @class */ (function (_super) { __extends(Track, _super); function Track(definition) { return _super.call(this, __assign(__assign({}, definition), { type: "track" })) || this; } Track.prototype.toScreenSpecific = function (screen) { var t = new Track({ key: this.key, name: this.name }); t.features = screen.features; t.screens = [screen]; t.properties = this.properties; return t; }; return Track; }(Event)); exports.Track = Track; //# sourceMappingURL=EventTypes.js.map