analytics-sdk-generator
Version:
Generates an SDK from an analytics descriptor file
181 lines • 9.07 kB
JavaScript
"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScreenAnalyticsFunction = exports.ScreenSpecificTrackAnalyticsFunction = exports.TrackAnalyticsFunction = exports.AnalyticsFunction = void 0;
var typescript_1 = __importStar(require("typescript"));
var BaseEvent = /** @class */ (function () {
function BaseEvent() {
}
BaseEvent.prototype.comment = function (event) {
var nodes = [];
if (event.description) {
nodes.push(typescript_1.factory.createJSDocComment(event.description));
}
return nodes;
};
BaseEvent.prototype.asNamedExport = function (event, options) {
return [typescript_1.factory.createVariableStatement([typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.factory.createVariableDeclarationList([typescript_1.factory.createVariableDeclaration(typescript_1.factory.createIdentifier(event.escapeKey()), undefined, undefined, new AnalyticsFunction(event).toAST(__assign({}, options)))], typescript_1.default.NodeFlags.Const))];
};
return BaseEvent;
}());
var AnalyticsFunction = /** @class */ (function () {
function AnalyticsFunction(definition) {
this.event = definition;
}
AnalyticsFunction.prototype.parameter = function (name, type, optional) {
if (optional === void 0) { optional = false; }
return typescript_1.factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1.factory.createIdentifier(name), optional ? typescript_1.factory.createToken(typescript_1.default.SyntaxKind.QuestionToken) : undefined, type, undefined);
};
AnalyticsFunction.prototype.propsParameter = function (properties, options) {
if (properties.properties.length == 0) {
return this.parameter("props", typescript_1.factory.createLiteralTypeNode(typescript_1.factory.createNull()), true);
}
else {
return this.parameter("props", properties.toAST(options), false);
}
};
AnalyticsFunction.prototype.sourceParameter = function (options) {
return this.parameter("source", typescript_1.factory.createTypeLiteralNode(this.event.sourceToObjectType().toAST(options).members), true);
};
AnalyticsFunction.prototype.toAST = function (options) {
var implementation;
var params = [
typescript_1.factory.createStringLiteral(this.event.type),
typescript_1.factory.createStringLiteral(this.event.name),
typescript_1.factory.createIdentifier("props"),
this.event.sourceToObjectType().toPartialLiteralAST("source")
];
if (options.hasImplementation) {
implementation = this.specifiedImplementation(options, params);
}
else {
implementation = this.emptyImplementation(params);
}
var asynchronous = options.methodsAsync ? [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.AsyncKeyword)] : undefined;
return typescript_1.factory.createArrowFunction(asynchronous, undefined, [
this.propsParameter(this.event.properties, options),
this.sourceParameter(options),
], undefined, typescript_1.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.factory.createBlock([implementation], true));
};
AnalyticsFunction.prototype.specifiedImplementation = function (options, params) {
if (options.methodsAsync) {
return typescript_1.factory.createExpressionStatement(typescript_1.factory.createAwaitExpression(typescript_1.factory.createCallExpression(typescript_1.factory.createIdentifier("implementation"), undefined, params)));
}
else {
return typescript_1.factory.createExpressionStatement(typescript_1.factory.createCallExpression(typescript_1.factory.createIdentifier("implementation"), undefined, params));
}
};
AnalyticsFunction.prototype.emptyImplementation = function (params) {
return typescript_1.factory.createCallExpression(typescript_1.factory.createPropertyAccessExpression(typescript_1.factory.createIdentifier("console"), typescript_1.factory.createIdentifier("log")), undefined, params);
};
return AnalyticsFunction;
}());
exports.AnalyticsFunction = AnalyticsFunction;
var TrackAnalyticsFunction = /** @class */ (function (_super) {
__extends(TrackAnalyticsFunction, _super);
function TrackAnalyticsFunction(track) {
var _this = _super.call(this) || this;
_this.track = track;
return _this;
}
TrackAnalyticsFunction.prototype.toAST = function (options) {
var comment = this.comment(this.track);
var main = this.asNamedExport(this.track, options);
return comment.concat(main);
};
return TrackAnalyticsFunction;
}(BaseEvent));
exports.TrackAnalyticsFunction = TrackAnalyticsFunction;
var ScreenSpecificTrackAnalyticsFunction = /** @class */ (function (_super) {
__extends(ScreenSpecificTrackAnalyticsFunction, _super);
function ScreenSpecificTrackAnalyticsFunction(track, screen) {
var _this = _super.call(this) || this;
_this.track = track;
_this.screen = screen;
return _this;
}
ScreenSpecificTrackAnalyticsFunction.prototype.toAST = function (options) {
var comment = this.comment(this.track);
var main = this.asNamedExport(this.track, options);
return comment.concat(main);
};
return ScreenSpecificTrackAnalyticsFunction;
}(BaseEvent));
exports.ScreenSpecificTrackAnalyticsFunction = ScreenSpecificTrackAnalyticsFunction;
var ScreenAnalyticsFunction = /** @class */ (function (_super) {
__extends(ScreenAnalyticsFunction, _super);
function ScreenAnalyticsFunction(screen) {
var _this = _super.call(this) || this;
_this.screen = screen;
return _this;
}
ScreenAnalyticsFunction.prototype.toAST = function (options) {
var comment = this.comment(this.screen);
var main = [
typescript_1.factory.createExportAssignment(undefined, undefined, undefined, new AnalyticsFunction(this.screen).toAST(__assign({}, options)))
];
var tracks = this.tracks(options);
return comment.concat(main).concat(tracks);
};
ScreenAnalyticsFunction.prototype.tracks = function (options) {
var nodes = [];
if (this.screen.tracks.length > 0) {
for (var _i = 0, _a = this.screen.tracks; _i < _a.length; _i++) {
var track = _a[_i];
var ast = new ScreenSpecificTrackAnalyticsFunction(track, this.screen).toAST(options);
for (var _b = 0, ast_1 = ast; _b < ast_1.length; _b++) {
var n = ast_1[_b];
nodes.push(n);
}
}
}
return nodes;
};
return ScreenAnalyticsFunction;
}(BaseEvent));
exports.ScreenAnalyticsFunction = ScreenAnalyticsFunction;
//# sourceMappingURL=Functions.js.map