analytics-sdk-generator
Version:
Generates an SDK from an analytics descriptor file
384 lines (383 loc) • 16.4 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 __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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedType = exports.UnionType = exports.Constant = exports.TypeReference = exports.ObjectType = exports.ObjectProperty = exports.ArrayType = exports.ComplexType = exports.BooleanType = exports.NumberType = exports.DateFormattedStringType = exports.FormattedStringType = exports.StringType = exports.AnyStringType = exports.PrimitiveType = exports.NonEnumeratedSimpleType = exports.EnumeratedSimpleType = exports.SimpleType = exports.BaseType = void 0;
var typescript_1 = __importStar(require("typescript"));
var TypeMapper_1 = __importDefault(require("./TypeMapper"));
var BaseType = /** @class */ (function () {
function BaseType(definition) {
Object.assign(this, definition);
}
BaseType.prototype.toAST = function (options) {
return null;
};
BaseType.prototype.toPartialLiteralAST = function (value) {
return typescript_1.factory.createStringLiteral("hardcodedlol");
};
return BaseType;
}());
exports.BaseType = BaseType;
var SimpleType = /** @class */ (function (_super) {
__extends(SimpleType, _super);
function SimpleType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return SimpleType;
}(BaseType));
exports.SimpleType = SimpleType;
var EnumeratedSimpleType = /** @class */ (function (_super) {
__extends(EnumeratedSimpleType, _super);
function EnumeratedSimpleType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return EnumeratedSimpleType;
}(BaseType));
exports.EnumeratedSimpleType = EnumeratedSimpleType;
var NonEnumeratedSimpleType = /** @class */ (function (_super) {
__extends(NonEnumeratedSimpleType, _super);
function NonEnumeratedSimpleType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return NonEnumeratedSimpleType;
}(BaseType));
exports.NonEnumeratedSimpleType = NonEnumeratedSimpleType;
var PrimitiveType = /** @class */ (function (_super) {
__extends(PrimitiveType, _super);
function PrimitiveType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return PrimitiveType;
}(BaseType));
exports.PrimitiveType = PrimitiveType;
var AnyStringType = /** @class */ (function (_super) {
__extends(AnyStringType, _super);
function AnyStringType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return AnyStringType;
}(PrimitiveType));
exports.AnyStringType = AnyStringType;
var StringType = /** @class */ (function (_super) {
__extends(StringType, _super);
function StringType() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringType.prototype.toAST = function (options) {
return typescript_1.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.StringKeyword);
};
StringType.prototype.toPartialLiteralAST = function (value) {
return typescript_1.factory.createStringLiteral(value);
};
return StringType;
}(AnyStringType));
exports.StringType = StringType;
var FormattedStringType = /** @class */ (function (_super) {
__extends(FormattedStringType, _super);
function FormattedStringType() {
return _super !== null && _super.apply(this, arguments) || this;
}
FormattedStringType.addFormat = function (key, format) {
this.formats[key] = format;
};
FormattedStringType.toSpecificType = function (definition) {
if (definition.format in this.formats) {
return new this.formats[definition.format](definition);
}
else {
return new StringType(definition);
}
};
FormattedStringType.formats = {};
return FormattedStringType;
}(AnyStringType));
exports.FormattedStringType = FormattedStringType;
var DateFormattedStringType = /** @class */ (function (_super) {
__extends(DateFormattedStringType, _super);
function DateFormattedStringType() {
return _super !== null && _super.apply(this, arguments) || this;
}
DateFormattedStringType.prototype.toAST = function (options) {
return typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier("Date"), undefined);
};
return DateFormattedStringType;
}(FormattedStringType));
exports.DateFormattedStringType = DateFormattedStringType;
FormattedStringType.addFormat('date-time', DateFormattedStringType);
var NumberType = /** @class */ (function (_super) {
__extends(NumberType, _super);
function NumberType() {
return _super !== null && _super.apply(this, arguments) || this;
}
NumberType.prototype.toAST = function (options) {
return typescript_1.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.NumberKeyword);
};
NumberType.prototype.toPartialLiteralAST = function (value) {
return typescript_1.factory.createNumericLiteral(value);
};
NumberType.toSpecificType = function (definition) {
return new this(definition);
};
return NumberType;
}(PrimitiveType));
exports.NumberType = NumberType;
var BooleanType = /** @class */ (function (_super) {
__extends(BooleanType, _super);
function BooleanType() {
return _super !== null && _super.apply(this, arguments) || this;
}
BooleanType.prototype.toAST = function (options) {
return typescript_1.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.BooleanKeyword);
};
BooleanType.toSpecificType = function (definition) {
return new this(definition);
};
BooleanType.prototype.toPartialLiteralAST = function (value) {
return !!value ? typescript_1.factory.createTrue() : typescript_1.factory.createFalse();
};
return BooleanType;
}(PrimitiveType));
exports.BooleanType = BooleanType;
var ComplexType = /** @class */ (function (_super) {
__extends(ComplexType, _super);
function ComplexType() {
return _super !== null && _super.apply(this, arguments) || this;
}
return ComplexType;
}(BaseType));
exports.ComplexType = ComplexType;
var ArrayType = /** @class */ (function (_super) {
__extends(ArrayType, _super);
function ArrayType(definition) {
var _this = _super.call(this, {}) || this;
_this.type = TypeMapper_1.default.toSpecificType(definition.items);
return _this;
}
ArrayType.prototype.toAST = function (options) {
return typescript_1.factory.createArrayTypeNode(this.type.toAST(options));
};
ArrayType.toSpecificType = function (definition) {
return new this(definition);
};
ArrayType.prototype.toPartialLiteralAST = function (value) {
throw new Error("Not Implemented");
typescript_1.factory.createArrayLiteralExpression([
typescript_1.factory.createTrue(),
typescript_1.factory.createFalse()
], false);
};
return ArrayType;
}(ComplexType));
exports.ArrayType = ArrayType;
var ObjectProperty = /** @class */ (function () {
function ObjectProperty(name, type, required) {
this.name = name;
this.type = type;
this.required = required;
}
ObjectProperty.prototype.toPartialLiteralAST = function () {
if (this.type instanceof Constant) {
return typescript_1.factory.createPropertyAssignment(typescript_1.factory.createIdentifier(this.name), this.type.toPartialLiteralAST());
}
else if (this.type instanceof ObjectType) {
return typescript_1.factory.createPropertyAssignment(typescript_1.factory.createIdentifier(this.name), this.type.toPartialLiteralAST());
}
else {
throw new Error("WUT");
}
};
ObjectProperty.prototype.toAST = function (options) {
var identifier;
if (this.name.includes('-') ||
this.name.includes('&') ||
this.name.includes(' ')) {
identifier = typescript_1.factory.createStringLiteral(this.name);
}
else {
identifier = typescript_1.factory.createIdentifier(this.name);
}
return typescript_1.factory.createPropertySignature(undefined, identifier, !this.required ? typescript_1.factory.createToken(typescript_1.default.SyntaxKind.QuestionToken) : undefined, this.type.toAST(options));
};
return ObjectProperty;
}());
exports.ObjectProperty = ObjectProperty;
var ObjectType = /** @class */ (function (_super) {
__extends(ObjectType, _super);
function ObjectType(definition) {
var _this = _super.call(this, {}) || this;
if (!('required' in definition)) {
definition['required'] = [];
}
_this.properties = Object.entries(definition.properties).map(function (p) {
var name = p[0];
var def = p[1];
return new ObjectProperty(name, TypeMapper_1.default.toSpecificType(def), definition.required.includes(name));
});
return _this;
}
ObjectType.prototype.toAST = function (options) {
return typescript_1.factory.createTypeLiteralNode(this.properties.map(function (p) { return p.toAST(options); }));
};
ObjectType.toSpecificType = function (definition) {
return new this(definition);
};
ObjectType.prototype.toPartialLiteralAST = function (overwrites, defaults, options) {
var filter = function (p) {
return p.type instanceof Constant || p.type instanceof ObjectType;
};
var props = this.properties.filter(filter).map(function (p) { return p.toPartialLiteralAST(); });
if (typeof (overwrites) == 'string') {
props.push(typescript_1.factory.createSpreadAssignment(typescript_1.factory.createIdentifier(overwrites)));
}
if (typeof (defaults) == 'string') {
props = [typescript_1.factory.createSpreadAssignment(typescript_1.factory.createIdentifier(defaults))].concat(props);
}
return typescript_1.factory.createObjectLiteralExpression(props, true);
};
return ObjectType;
}(ComplexType));
exports.ObjectType = ObjectType;
var TypeReference = /** @class */ (function (_super) {
__extends(TypeReference, _super);
function TypeReference(definition) {
var _this = _super.call(this, {}) || this;
_this.reference = definition['$ref'];
return _this;
}
TypeReference.prototype.jsonRefToName = function (options) {
var fqn = this.reference.split("/");
if (options === null || options === void 0 ? void 0 : options.importMappings) {
return typescript_1.factory.createQualifiedName(typescript_1.factory.createIdentifier(options.importMappings[fqn[1]][1]), typescript_1.factory.createIdentifier(fqn[fqn.length - 1]));
}
else {
return typescript_1.factory.createIdentifier(fqn.pop());
}
};
TypeReference.prototype.toAST = function (options) {
return typescript_1.factory.createTypeReferenceNode(this.jsonRefToName(options), undefined);
};
TypeReference.toSpecificType = function (definition) {
return new this(definition);
};
return TypeReference;
}(BaseType));
exports.TypeReference = TypeReference;
var Constant = /** @class */ (function (_super) {
__extends(Constant, _super);
function Constant(value) {
var _this = _super.call(this, {}) || this;
_this.value = value;
return _this;
}
Constant.toSpecificType = function (definition) {
return new this(definition);
};
Constant.prototype.setType = function (type) {
this.type = type;
return this;
};
Constant.prototype.toPartialLiteralAST = function () {
return (this.type).toPartialLiteralAST(this.value);
};
Constant.prototype.toAST = function (options) {
return typescript_1.factory.createLiteralTypeNode(this.type.toPartialLiteralAST(this.value));
/*
switch(this.type) {
case 'number':
return factory.createLiteralTypeNode(factory.createNumericLiteral(this.value))
case 'string':
default:
return factory.createLiteralTypeNode(factory.createStringLiteral(this.value))
}
*/
};
return Constant;
}(BaseType));
exports.Constant = Constant;
var UnionType = /** @class */ (function (_super) {
__extends(UnionType, _super);
function UnionType(definition) {
var _this = _super.call(this, {}) || this;
_this.options = definition.options;
return _this;
}
/*
static toSpecificType(definition: InputTypes.UnionDefinition) {
if(definition.oneOf.length == 0) {
throw new Error("Unions must have atleast one option")
} else if (definition.oneOf.length == 1){
return TypeMapper.toSpecificType(definition.oneOf[0])
} else {
return new UnionType({
options: definition.oneOf.map( o => TypeMapper.toSpecificType(o) )
})
}
}
*/
UnionType.prototype.toAST = function (options) {
return typescript_1.factory.createUnionTypeNode(this.options.map(function (o) { return o.toAST(options); }));
};
return UnionType;
}(BaseType));
exports.UnionType = UnionType;
var NamedType = /** @class */ (function () {
function NamedType(name, type, description) {
this.type = type;
this.name = name;
this.description = description;
}
NamedType.prototype.toAST = function (options) {
var nodes = [];
if (this.description) {
nodes.push(typescript_1.factory.createJSDocComment(this.description));
}
nodes.push(typescript_1.factory.createTypeAliasDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.factory.createIdentifier(this.name), undefined, this.type.toAST(options)));
return nodes;
};
NamedType.prototype.toPartialLiteralAST = function () {
var nodes = [];
if (this.description) {
nodes.push(typescript_1.factory.createJSDocComment(this.description));
}
nodes.push(typescript_1.factory.createVariableStatement(undefined, typescript_1.factory.createVariableDeclarationList([typescript_1.factory.createVariableDeclaration(typescript_1.factory.createIdentifier(this.name), undefined, undefined, this.type.toPartialLiteralAST())], typescript_1.default.NodeFlags.Const)));
return nodes;
};
return NamedType;
}());
exports.NamedType = NamedType;
//# sourceMappingURL=Types.js.map