1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var JsonPointer = tslib_1.__importStar(require("../jsonPointer"));
|
5 | var typeNameConvertor_1 = require("./typeNameConvertor");
|
6 | var SchemaConvertor = (function () {
|
7 | function SchemaConvertor(processor, convertor) {
|
8 | if (convertor === void 0) { convertor = typeNameConvertor_1.DefaultTypeNameConvertor; }
|
9 | this.processor = processor;
|
10 | this.convertor = convertor;
|
11 | }
|
12 | SchemaConvertor.prototype.getLastTypeName = function (id) {
|
13 | var names = this.convertor(id);
|
14 | if (names.length > 0) {
|
15 | return names[names.length - 1];
|
16 | }
|
17 | else {
|
18 | return '';
|
19 | }
|
20 | };
|
21 | SchemaConvertor.prototype.buildSchemaMergedMap = function (schemas, typeMarker) {
|
22 | var map = {};
|
23 | try {
|
24 | for (var schemas_1 = tslib_1.__values(schemas), schemas_1_1 = schemas_1.next(); !schemas_1_1.done; schemas_1_1 = schemas_1.next()) {
|
25 | var type = schemas_1_1.value;
|
26 | var names = this.convertor(type.id);
|
27 | var parent_1 = JsonPointer.get(map, names, true);
|
28 | if (parent_1 == null) {
|
29 | JsonPointer.set(map, names, (_a = {}, _a[typeMarker] = type, _a));
|
30 | }
|
31 | else {
|
32 | parent_1[typeMarker] = type;
|
33 | }
|
34 | }
|
35 | }
|
36 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
37 | finally {
|
38 | try {
|
39 | if (schemas_1_1 && !schemas_1_1.done && (_b = schemas_1.return)) _b.call(schemas_1);
|
40 | }
|
41 | finally { if (e_1) throw e_1.error; }
|
42 | }
|
43 | if (Object.keys(map).length === 0) {
|
44 | throw new Error('There is no schema in the input contents.');
|
45 | }
|
46 | return map;
|
47 | var e_1, _b, _a;
|
48 | };
|
49 | SchemaConvertor.prototype.start = function () {
|
50 | this.processor.clear();
|
51 | };
|
52 | SchemaConvertor.prototype.end = function () {
|
53 | return this.processor.toDefinition();
|
54 | };
|
55 | SchemaConvertor.prototype.startNest = function (name) {
|
56 | var processor = this.processor;
|
57 | if (processor.indentLevel === 0) {
|
58 | processor.output('declare ');
|
59 | }
|
60 | processor.output('namespace ').outputType(name, true).outputLine(' {');
|
61 | processor.increaseIndent();
|
62 | };
|
63 | SchemaConvertor.prototype.endNest = function () {
|
64 | var processor = this.processor;
|
65 | processor.decreaseIndent();
|
66 | processor.outputLine('}');
|
67 | };
|
68 | SchemaConvertor.prototype.startInterfaceNest = function (id) {
|
69 | var name = this.getLastTypeName(id);
|
70 | this.processor.output('export interface ').outputType(name).output(' ');
|
71 | this.startTypeNest();
|
72 | };
|
73 | SchemaConvertor.prototype.endInterfaceNest = function () {
|
74 | this.endTypeNest(false);
|
75 | this.processor.outputLine();
|
76 | };
|
77 | SchemaConvertor.prototype.outputExportType = function (id) {
|
78 | var name = this.getLastTypeName(id);
|
79 | this.processor.output('export type ').outputType(name).output(' = ');
|
80 | };
|
81 | SchemaConvertor.prototype.startTypeNest = function () {
|
82 | this.processor.outputLine('{');
|
83 | this.processor.increaseIndent();
|
84 | };
|
85 | SchemaConvertor.prototype.endTypeNest = function (terminate) {
|
86 | this.processor.decreaseIndent();
|
87 | this.processor.output('}');
|
88 | if (terminate) {
|
89 | this.processor.outputLine(';');
|
90 | }
|
91 | };
|
92 | SchemaConvertor.prototype.outputRawValue = function (value, isEndOfLine) {
|
93 | if (isEndOfLine === void 0) { isEndOfLine = false; }
|
94 | this.processor.output(value);
|
95 | if (isEndOfLine) {
|
96 | this.processor.outputLine();
|
97 | }
|
98 | };
|
99 | SchemaConvertor.prototype.outputComments = function (schema) {
|
100 | var content = schema.content;
|
101 | var comments = [];
|
102 | if ('$comment' in content) {
|
103 | comments.push(content.$comment);
|
104 | }
|
105 | comments.push(content.description);
|
106 | if ('example' in content || 'examples' in content) {
|
107 | comments.push('example:');
|
108 | if ('example' in content) {
|
109 | comments.push(content.example);
|
110 | }
|
111 | if ('examples' in content) {
|
112 | comments.push.apply(comments, tslib_1.__spread(content.examples));
|
113 | }
|
114 | }
|
115 | (_a = this.processor).outputJSDoc.apply(_a, tslib_1.__spread(comments));
|
116 | var _a;
|
117 | };
|
118 | SchemaConvertor.prototype.outputPropertyName = function (_schema, propertyName, required) {
|
119 | var optionalProperty = required == null || required.indexOf(propertyName) < 0;
|
120 | this.processor.outputKey(propertyName, optionalProperty).output(': ');
|
121 | };
|
122 | SchemaConvertor.prototype.outputArrayedType = function (schema, types, output, terminate, outputOptional) {
|
123 | var _this = this;
|
124 | if (outputOptional === void 0) { outputOptional = true; }
|
125 | if (!terminate) {
|
126 | this.processor.output('(');
|
127 | }
|
128 | types.forEach(function (t, index) {
|
129 | output(t, index);
|
130 | if (index < types.length - 1) {
|
131 | _this.processor.output(' | ');
|
132 | }
|
133 | });
|
134 | if (!terminate) {
|
135 | this.processor.output(')');
|
136 | }
|
137 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
138 | };
|
139 | SchemaConvertor.prototype.outputTypeIdName = function (schema, currentSchema, terminate, outputOptional) {
|
140 | var _this = this;
|
141 | if (terminate === void 0) { terminate = true; }
|
142 | if (outputOptional === void 0) { outputOptional = true; }
|
143 | var typeName = this.getTypename(schema.id, currentSchema);
|
144 | typeName.forEach(function (type, index) {
|
145 | var isLast = index === typeName.length - 1;
|
146 | _this.processor.outputType(type, isLast ? false : true);
|
147 | if (!isLast) {
|
148 | _this.processor.output('.');
|
149 | }
|
150 | });
|
151 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
152 | };
|
153 | SchemaConvertor.prototype.getTypename = function (id, baseSchema) {
|
154 | var result = this.convertor(id);
|
155 | var baseId = baseSchema.id;
|
156 | if (baseId) {
|
157 | var baseTypes = this.convertor(baseId).slice(0, -1);
|
158 | try {
|
159 | for (var baseTypes_1 = tslib_1.__values(baseTypes), baseTypes_1_1 = baseTypes_1.next(); !baseTypes_1_1.done; baseTypes_1_1 = baseTypes_1.next()) {
|
160 | var type = baseTypes_1_1.value;
|
161 | if (result[0] === type) {
|
162 | result.shift();
|
163 | }
|
164 | else {
|
165 | break;
|
166 | }
|
167 | }
|
168 | }
|
169 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
170 | finally {
|
171 | try {
|
172 | if (baseTypes_1_1 && !baseTypes_1_1.done && (_a = baseTypes_1.return)) _a.call(baseTypes_1);
|
173 | }
|
174 | finally { if (e_2) throw e_2.error; }
|
175 | }
|
176 | if (result.length === 0) {
|
177 | return [this.getLastTypeName(id)];
|
178 | }
|
179 | }
|
180 | return result;
|
181 | var e_2, _a;
|
182 | };
|
183 | SchemaConvertor.prototype.outputPrimitiveTypeName = function (schema, typeName, terminate, outputOptional) {
|
184 | if (terminate === void 0) { terminate = true; }
|
185 | if (outputOptional === void 0) { outputOptional = true; }
|
186 | this.processor.outputType(typeName, true);
|
187 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
188 | };
|
189 | SchemaConvertor.prototype.outputStringTypeName = function (schema, typeName, terminate, outputOptional) {
|
190 | if (outputOptional === void 0) { outputOptional = true; }
|
191 | if (typeName) {
|
192 | this.processor.output(typeName);
|
193 | }
|
194 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
195 | };
|
196 | SchemaConvertor.prototype.outputTypeNameTrailer = function (schema, terminate, outputOptional) {
|
197 | if (terminate) {
|
198 | this.processor.output(';');
|
199 | }
|
200 | if (outputOptional) {
|
201 | this.outputOptionalInformation(schema, terminate);
|
202 | }
|
203 | if (terminate) {
|
204 | this.processor.outputLine();
|
205 | }
|
206 | };
|
207 | SchemaConvertor.prototype.outputOptionalInformation = function (schema, terminate) {
|
208 | var format = schema.content.format;
|
209 | var pattern = schema.content.pattern;
|
210 | if (!format && !pattern) {
|
211 | return;
|
212 | }
|
213 | if (terminate) {
|
214 | this.processor.output(' //');
|
215 | }
|
216 | else {
|
217 | this.processor.output(' /*');
|
218 | }
|
219 | if (format) {
|
220 | this.processor.output(' ').output(format);
|
221 | }
|
222 | if (pattern) {
|
223 | this.processor.output(' ').output(pattern);
|
224 | }
|
225 | if (!terminate) {
|
226 | this.processor.output(' */ ');
|
227 | }
|
228 | };
|
229 | return SchemaConvertor;
|
230 | }());
|
231 | exports.default = SchemaConvertor;
|