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, namespaceName) {
|
8 | if (convertor === void 0) { convertor = typeNameConvertor_1.DefaultTypeNameConvertor; }
|
9 | this.processor = processor;
|
10 | this.convertor = convertor;
|
11 | this.replaceLevel = 0;
|
12 | this.ns = namespaceName == null ? undefined : namespaceName.split('/').filter(function (s) { return s.length > 0; });
|
13 | }
|
14 | SchemaConvertor.prototype.getLastTypeName = function (id) {
|
15 | var names = this.convertor(id);
|
16 | if (names.length > 0) {
|
17 | return names[names.length - 1];
|
18 | }
|
19 | else {
|
20 | return '';
|
21 | }
|
22 | };
|
23 | SchemaConvertor.prototype.buildSchemaMergedMap = function (schemas, typeMarker) {
|
24 | var e_1, _a, e_2, _b, _c;
|
25 | var map = {};
|
26 | var paths = [];
|
27 | var minLevel = Number.MAX_SAFE_INTEGER;
|
28 | try {
|
29 | for (var schemas_1 = tslib_1.__values(schemas), schemas_1_1 = schemas_1.next(); !schemas_1_1.done; schemas_1_1 = schemas_1.next()) {
|
30 | var type = schemas_1_1.value;
|
31 | var path = this.convertor(type.id);
|
32 | minLevel = Math.min(minLevel, path.length);
|
33 | paths.push({ path: path, type: type });
|
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 && (_a = schemas_1.return)) _a.call(schemas_1);
|
40 | }
|
41 | finally { if (e_1) throw e_1.error; }
|
42 | }
|
43 | this.replaceLevel = minLevel;
|
44 | try {
|
45 | for (var paths_1 = tslib_1.__values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
|
46 | var item = paths_1_1.value;
|
47 | var path = item.path;
|
48 | this.replaceNamespace(path);
|
49 | var parent_1 = JsonPointer.get(map, path, true);
|
50 | if (parent_1 == null) {
|
51 | JsonPointer.set(map, path, (_c = {}, _c[typeMarker] = item.type, _c));
|
52 | }
|
53 | else {
|
54 | parent_1[typeMarker] = item.type;
|
55 | }
|
56 | }
|
57 | }
|
58 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
59 | finally {
|
60 | try {
|
61 | if (paths_1_1 && !paths_1_1.done && (_b = paths_1.return)) _b.call(paths_1);
|
62 | }
|
63 | finally { if (e_2) throw e_2.error; }
|
64 | }
|
65 | if (Object.keys(map).length === 0) {
|
66 | throw new Error('There is no schema in the input contents.');
|
67 | }
|
68 | return map;
|
69 | };
|
70 | SchemaConvertor.prototype.replaceNamespace = function (paths) {
|
71 | if (this.ns == null) {
|
72 | return;
|
73 | }
|
74 | paths.splice(0, this.replaceLevel - 1);
|
75 | if (this.ns.length > 0) {
|
76 | paths.unshift.apply(paths, tslib_1.__spread(this.ns));
|
77 | }
|
78 | };
|
79 | SchemaConvertor.prototype.start = function () {
|
80 | this.processor.clear();
|
81 | };
|
82 | SchemaConvertor.prototype.end = function () {
|
83 | return this.processor.toDefinition();
|
84 | };
|
85 | SchemaConvertor.prototype.startNest = function (name) {
|
86 | var processor = this.processor;
|
87 | if (processor.indentLevel === 0) {
|
88 | processor.output('declare ');
|
89 | }
|
90 | processor.output('namespace ').outputType(name, true).outputLine(' {');
|
91 | processor.increaseIndent();
|
92 | };
|
93 | SchemaConvertor.prototype.endNest = function () {
|
94 | var processor = this.processor;
|
95 | processor.decreaseIndent();
|
96 | processor.outputLine('}');
|
97 | };
|
98 | SchemaConvertor.prototype.startInterfaceNest = function (id) {
|
99 | var processor = this.processor;
|
100 | if (processor.indentLevel === 0 && (this.ns == null || this.ns.length > 0)) {
|
101 | processor.output('declare ');
|
102 | }
|
103 | else {
|
104 | processor.output('export ');
|
105 | }
|
106 | var name = this.getLastTypeName(id);
|
107 | processor.output('interface ').outputType(name).output(' ');
|
108 | this.startTypeNest();
|
109 | };
|
110 | SchemaConvertor.prototype.endInterfaceNest = function () {
|
111 | this.endTypeNest(false);
|
112 | this.processor.outputLine();
|
113 | };
|
114 | SchemaConvertor.prototype.outputExportType = function (id) {
|
115 | var processor = this.processor;
|
116 | if (processor.indentLevel === 0 && (this.ns == null || this.ns.length > 0)) {
|
117 | processor.output('declare ');
|
118 | }
|
119 | else {
|
120 | processor.output('export ');
|
121 | }
|
122 | var name = this.getLastTypeName(id);
|
123 | processor.output('type ').outputType(name).output(' = ');
|
124 | };
|
125 | SchemaConvertor.prototype.startTypeNest = function () {
|
126 | this.processor.outputLine('{');
|
127 | this.processor.increaseIndent();
|
128 | };
|
129 | SchemaConvertor.prototype.endTypeNest = function (terminate) {
|
130 | this.processor.decreaseIndent();
|
131 | this.processor.output('}');
|
132 | if (terminate) {
|
133 | this.processor.outputLine(';');
|
134 | }
|
135 | };
|
136 | SchemaConvertor.prototype.outputRawValue = function (value, isEndOfLine) {
|
137 | if (isEndOfLine === void 0) { isEndOfLine = false; }
|
138 | this.processor.output(value);
|
139 | if (isEndOfLine) {
|
140 | this.processor.outputLine();
|
141 | }
|
142 | };
|
143 | SchemaConvertor.prototype.outputComments = function (schema) {
|
144 | var _a;
|
145 | var content = schema.content;
|
146 | var comments = [];
|
147 | if ('$comment' in content) {
|
148 | comments.push(content.$comment);
|
149 | }
|
150 | comments.push(content.title);
|
151 | comments.push(content.description);
|
152 | if ('example' in content || 'examples' in content) {
|
153 | comments.push('example:');
|
154 | if ('example' in content) {
|
155 | comments.push(content.example);
|
156 | }
|
157 | if ('examples' in content) {
|
158 | comments.push.apply(comments, tslib_1.__spread(content.examples));
|
159 | }
|
160 | }
|
161 | (_a = this.processor).outputJSDoc.apply(_a, tslib_1.__spread(comments));
|
162 | };
|
163 | SchemaConvertor.prototype.outputPropertyName = function (_schema, propertyName, required) {
|
164 | var optionalProperty = required == null || required.indexOf(propertyName) < 0;
|
165 | this.processor.outputKey(propertyName, optionalProperty).output(': ');
|
166 | };
|
167 | SchemaConvertor.prototype.outputPropertyAttribute = function (schema) {
|
168 | var content = schema.content;
|
169 | if ('readOnly' in content && content.readOnly) {
|
170 | this.processor.output('readonly ');
|
171 | }
|
172 | };
|
173 | SchemaConvertor.prototype.outputArrayedType = function (schema, types, output, terminate, outputOptional) {
|
174 | var _this = this;
|
175 | if (outputOptional === void 0) { outputOptional = true; }
|
176 | if (!terminate) {
|
177 | this.processor.output('(');
|
178 | }
|
179 | types.forEach(function (t, index) {
|
180 | output(t, index);
|
181 | if (index < types.length - 1) {
|
182 | _this.processor.output(' | ');
|
183 | }
|
184 | });
|
185 | if (!terminate) {
|
186 | this.processor.output(')');
|
187 | }
|
188 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
189 | };
|
190 | SchemaConvertor.prototype.outputTypeIdName = function (schema, currentSchema, terminate, outputOptional) {
|
191 | var _this = this;
|
192 | if (terminate === void 0) { terminate = true; }
|
193 | if (outputOptional === void 0) { outputOptional = true; }
|
194 | var typeName = this.getTypename(schema.id, currentSchema);
|
195 | typeName.forEach(function (type, index) {
|
196 | var isLast = index === typeName.length - 1;
|
197 | _this.processor.outputType(type, isLast ? false : true);
|
198 | if (!isLast) {
|
199 | _this.processor.output('.');
|
200 | }
|
201 | });
|
202 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
203 | };
|
204 | SchemaConvertor.prototype.getTypename = function (id, baseSchema) {
|
205 | var e_3, _a;
|
206 | var result = this.convertor(id);
|
207 | this.replaceNamespace(result);
|
208 | var baseId = baseSchema.id;
|
209 | if (baseId) {
|
210 | var baseTypes = this.convertor(baseId).slice(0, -1);
|
211 | try {
|
212 | for (var baseTypes_1 = tslib_1.__values(baseTypes), baseTypes_1_1 = baseTypes_1.next(); !baseTypes_1_1.done; baseTypes_1_1 = baseTypes_1.next()) {
|
213 | var type = baseTypes_1_1.value;
|
214 | if (result[0] === type) {
|
215 | result.shift();
|
216 | }
|
217 | else {
|
218 | break;
|
219 | }
|
220 | }
|
221 | }
|
222 | catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
223 | finally {
|
224 | try {
|
225 | if (baseTypes_1_1 && !baseTypes_1_1.done && (_a = baseTypes_1.return)) _a.call(baseTypes_1);
|
226 | }
|
227 | finally { if (e_3) throw e_3.error; }
|
228 | }
|
229 | if (result.length === 0) {
|
230 | return [this.getLastTypeName(id)];
|
231 | }
|
232 | }
|
233 | return result;
|
234 | };
|
235 | SchemaConvertor.prototype.outputPrimitiveTypeName = function (schema, typeName, terminate, outputOptional) {
|
236 | if (terminate === void 0) { terminate = true; }
|
237 | if (outputOptional === void 0) { outputOptional = true; }
|
238 | this.processor.outputType(typeName, true);
|
239 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
240 | };
|
241 | SchemaConvertor.prototype.outputStringTypeName = function (schema, typeName, terminate, outputOptional) {
|
242 | if (outputOptional === void 0) { outputOptional = true; }
|
243 | if (typeName) {
|
244 | this.processor.output(typeName);
|
245 | }
|
246 | this.outputTypeNameTrailer(schema, terminate, outputOptional);
|
247 | };
|
248 | SchemaConvertor.prototype.outputTypeNameTrailer = function (schema, terminate, outputOptional) {
|
249 | if (terminate) {
|
250 | this.processor.output(';');
|
251 | }
|
252 | if (outputOptional) {
|
253 | this.outputOptionalInformation(schema, terminate);
|
254 | }
|
255 | if (terminate) {
|
256 | this.processor.outputLine();
|
257 | }
|
258 | };
|
259 | SchemaConvertor.prototype.outputOptionalInformation = function (schema, terminate) {
|
260 | var format = schema.content.format;
|
261 | var pattern = schema.content.pattern;
|
262 | if (!format && !pattern) {
|
263 | return;
|
264 | }
|
265 | if (terminate) {
|
266 | this.processor.output(' //');
|
267 | }
|
268 | else {
|
269 | this.processor.output(' /*');
|
270 | }
|
271 | if (format) {
|
272 | this.processor.output(' ').output(format);
|
273 | }
|
274 | if (pattern) {
|
275 | this.processor.output(' ').output(pattern);
|
276 | }
|
277 | if (!terminate) {
|
278 | this.processor.output(' */ ');
|
279 | }
|
280 | };
|
281 | return SchemaConvertor;
|
282 | }());
|
283 | exports.default = SchemaConvertor;
|
284 |
|
\ | No newline at end of file |