UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

67 lines (66 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeAlias = void 0; const printTree_1 = require("tree-dump/lib/printTree"); const classes_1 = require("../type/classes"); const toText_1 = require("../typescript/toText"); const json_schema_1 = require("../json-schema"); const TypeExportContext_1 = require("./TypeExportContext"); class TypeAlias { constructor(system, id, type) { this.system = system; this.id = id; this.type = type; } getType() { return this.type; } resolve() { return this.system.resolve(this.id); } toString(tab = '') { return this.id + (0, printTree_1.printTree)(tab, [(tab) => this.type.toString(tab)]); } toTypeScriptAst() { const type = this.type; if (type instanceof classes_1.ObjType) { const ast = this.type.toTypeScriptAst(); const node = { node: 'InterfaceDeclaration', name: this.id, members: ast.members, }; return node; } else { const node = { node: 'TypeAliasDeclaration', name: this.id, type: type.toTypeScriptAst(), }; // TODO: Figure out if this is still needed, and possibly bring it back. // augmentWithComment(type, node); return node; } } toTypeScript() { return (0, toText_1.toText)(this.toTypeScriptAst()); } toJsonSchema() { const node = { $id: this.id, $ref: '#/$defs/' + this.id, $defs: {}, }; const ctx = new TypeExportContext_1.TypeExportContext(); ctx.visitRef(this.id); node.$defs[this.id] = (0, json_schema_1.typeToJsonSchema)(this.type, ctx); let ref; while ((ref = ctx.nextMentionedRef())) { ctx.visitRef(ref); node.$defs[ref] = (0, json_schema_1.typeToJsonSchema)(this.system.resolve(ref).type, ctx); } return node; } } exports.TypeAlias = TypeAlias;