@f3liz/rescript-autogen-openapi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -0
- package/README.md +98 -0
- package/lib/es6/src/Codegen.mjs +423 -0
- package/lib/es6/src/Types.mjs +20 -0
- package/lib/es6/src/core/CodegenUtils.mjs +186 -0
- package/lib/es6/src/core/DocOverride.mjs +399 -0
- package/lib/es6/src/core/FileSystem.mjs +78 -0
- package/lib/es6/src/core/IRBuilder.mjs +201 -0
- package/lib/es6/src/core/OpenAPIParser.mjs +168 -0
- package/lib/es6/src/core/Pipeline.mjs +150 -0
- package/lib/es6/src/core/ReferenceResolver.mjs +41 -0
- package/lib/es6/src/core/Result.mjs +378 -0
- package/lib/es6/src/core/SchemaIR.mjs +355 -0
- package/lib/es6/src/core/SchemaIRParser.mjs +490 -0
- package/lib/es6/src/core/SchemaRefResolver.mjs +146 -0
- package/lib/es6/src/core/SchemaRegistry.mjs +92 -0
- package/lib/es6/src/core/SpecDiffer.mjs +251 -0
- package/lib/es6/src/core/SpecMerger.mjs +237 -0
- package/lib/es6/src/generators/ComponentSchemaGenerator.mjs +125 -0
- package/lib/es6/src/generators/DiffReportGenerator.mjs +155 -0
- package/lib/es6/src/generators/EndpointGenerator.mjs +172 -0
- package/lib/es6/src/generators/IRToSuryGenerator.mjs +233 -0
- package/lib/es6/src/generators/IRToTypeGenerator.mjs +241 -0
- package/lib/es6/src/generators/IRToTypeScriptGenerator.mjs +143 -0
- package/lib/es6/src/generators/ModuleGenerator.mjs +285 -0
- package/lib/es6/src/generators/SchemaCodeGenerator.mjs +77 -0
- package/lib/es6/src/generators/ThinWrapperGenerator.mjs +97 -0
- package/lib/es6/src/generators/TypeScriptDtsGenerator.mjs +172 -0
- package/lib/es6/src/generators/TypeScriptWrapperGenerator.mjs +145 -0
- package/lib/es6/src/types/CodegenError.mjs +79 -0
- package/lib/es6/src/types/Config.mjs +42 -0
- package/lib/es6/src/types/GenerationContext.mjs +24 -0
- package/package.json +44 -0
- package/rescript.json +20 -0
- package/src/Codegen.res +222 -0
- package/src/Types.res +195 -0
- package/src/core/CodegenUtils.res +130 -0
- package/src/core/DocOverride.res +504 -0
- package/src/core/FileSystem.res +62 -0
- package/src/core/IRBuilder.res +66 -0
- package/src/core/OpenAPIParser.res +144 -0
- package/src/core/Pipeline.res +51 -0
- package/src/core/ReferenceResolver.res +41 -0
- package/src/core/Result.res +187 -0
- package/src/core/SchemaIR.res +258 -0
- package/src/core/SchemaIRParser.res +360 -0
- package/src/core/SchemaRefResolver.res +143 -0
- package/src/core/SchemaRegistry.res +107 -0
- package/src/core/SpecDiffer.res +270 -0
- package/src/core/SpecMerger.res +245 -0
- package/src/generators/ComponentSchemaGenerator.res +127 -0
- package/src/generators/DiffReportGenerator.res +152 -0
- package/src/generators/EndpointGenerator.res +172 -0
- package/src/generators/IRToSuryGenerator.res +199 -0
- package/src/generators/IRToTypeGenerator.res +199 -0
- package/src/generators/IRToTypeScriptGenerator.res +72 -0
- package/src/generators/ModuleGenerator.res +362 -0
- package/src/generators/SchemaCodeGenerator.res +83 -0
- package/src/generators/ThinWrapperGenerator.res +124 -0
- package/src/generators/TypeScriptDtsGenerator.res +193 -0
- package/src/generators/TypeScriptWrapperGenerator.res +166 -0
- package/src/types/CodegenError.res +82 -0
- package/src/types/Config.res +89 -0
- package/src/types/GenerationContext.res +23 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
4
|
+
|
|
5
|
+
function isOptional(irType) {
|
|
6
|
+
if (typeof irType !== "object") {
|
|
7
|
+
return false;
|
|
8
|
+
} else {
|
|
9
|
+
return irType.TAG === "Option";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function unwrapOption(irType) {
|
|
14
|
+
if (typeof irType !== "object" || irType.TAG !== "Option") {
|
|
15
|
+
return irType;
|
|
16
|
+
} else {
|
|
17
|
+
return irType._0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function makeOptional(irType) {
|
|
22
|
+
if (typeof irType !== "object" || irType.TAG !== "Option") {
|
|
23
|
+
return {
|
|
24
|
+
TAG: "Option",
|
|
25
|
+
_0: irType
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
return irType;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isSimpleType(_irType) {
|
|
33
|
+
while (true) {
|
|
34
|
+
let irType = _irType;
|
|
35
|
+
if (typeof irType !== "object") {
|
|
36
|
+
return irType !== "Unknown";
|
|
37
|
+
}
|
|
38
|
+
switch (irType.TAG) {
|
|
39
|
+
case "Array" :
|
|
40
|
+
_irType = irType.items;
|
|
41
|
+
continue;
|
|
42
|
+
case "Object" :
|
|
43
|
+
case "Union" :
|
|
44
|
+
case "Intersection" :
|
|
45
|
+
return false;
|
|
46
|
+
case "Option" :
|
|
47
|
+
_irType = irType._0;
|
|
48
|
+
continue;
|
|
49
|
+
default:
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function complexityScore(_irType) {
|
|
56
|
+
while (true) {
|
|
57
|
+
let irType = _irType;
|
|
58
|
+
if (typeof irType !== "object") {
|
|
59
|
+
return 1;
|
|
60
|
+
}
|
|
61
|
+
switch (irType.TAG) {
|
|
62
|
+
case "Array" :
|
|
63
|
+
return 1 + complexityScore(irType.items) | 0;
|
|
64
|
+
case "Object" :
|
|
65
|
+
let propsScore = Stdlib_Array.reduce(irType.properties.map(param => complexityScore(param[1])), 0, (acc, score) => acc + score | 0);
|
|
66
|
+
return 5 + propsScore | 0;
|
|
67
|
+
case "Union" :
|
|
68
|
+
let typesScore = Stdlib_Array.reduce(irType._0.map(complexityScore), 0, (acc, score) => acc + score | 0);
|
|
69
|
+
return 2 + typesScore | 0;
|
|
70
|
+
case "Intersection" :
|
|
71
|
+
let typesScore$1 = Stdlib_Array.reduce(irType._0.map(complexityScore), 0, (acc, score) => acc + score | 0);
|
|
72
|
+
return 3 + typesScore$1 | 0;
|
|
73
|
+
case "Option" :
|
|
74
|
+
_irType = irType._0;
|
|
75
|
+
continue;
|
|
76
|
+
default:
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function extractComplexTypes(baseName, irType, thresholdOpt) {
|
|
83
|
+
let threshold = thresholdOpt !== undefined ? thresholdOpt : 10;
|
|
84
|
+
let score = complexityScore(irType);
|
|
85
|
+
if (score <= threshold) {
|
|
86
|
+
return [
|
|
87
|
+
irType,
|
|
88
|
+
[]
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
if (typeof irType !== "object") {
|
|
92
|
+
return [
|
|
93
|
+
irType,
|
|
94
|
+
[]
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
switch (irType.TAG) {
|
|
98
|
+
case "Array" :
|
|
99
|
+
let match = extractComplexTypes(baseName + `_Item`, irType.items, threshold);
|
|
100
|
+
return [
|
|
101
|
+
{
|
|
102
|
+
TAG: "Array",
|
|
103
|
+
items: match[0],
|
|
104
|
+
constraints: irType.constraints
|
|
105
|
+
},
|
|
106
|
+
match[1]
|
|
107
|
+
];
|
|
108
|
+
case "Object" :
|
|
109
|
+
let match$1 = Stdlib_Array.reduce(irType.properties.map(param => {
|
|
110
|
+
let propName = param[0];
|
|
111
|
+
let propBaseName = baseName + `_` + propName;
|
|
112
|
+
let match = extractComplexTypes(propBaseName, param[1], threshold);
|
|
113
|
+
return [
|
|
114
|
+
[
|
|
115
|
+
propName,
|
|
116
|
+
match[0],
|
|
117
|
+
param[2]
|
|
118
|
+
],
|
|
119
|
+
match[1]
|
|
120
|
+
];
|
|
121
|
+
}), [
|
|
122
|
+
[],
|
|
123
|
+
[]
|
|
124
|
+
], (param, param$1) => [
|
|
125
|
+
param[0].concat([param$1[0]]),
|
|
126
|
+
param[1].concat(param$1[1])
|
|
127
|
+
]);
|
|
128
|
+
let newType_0 = match$1[0];
|
|
129
|
+
let newType_1 = irType.additionalProperties;
|
|
130
|
+
let newType = {
|
|
131
|
+
TAG: "Object",
|
|
132
|
+
properties: newType_0,
|
|
133
|
+
additionalProperties: newType_1
|
|
134
|
+
};
|
|
135
|
+
return [
|
|
136
|
+
{
|
|
137
|
+
TAG: "Reference",
|
|
138
|
+
_0: `#/components/schemas/` + baseName
|
|
139
|
+
},
|
|
140
|
+
match$1[1].concat([{
|
|
141
|
+
name: baseName,
|
|
142
|
+
description: undefined,
|
|
143
|
+
type_: newType
|
|
144
|
+
}])
|
|
145
|
+
];
|
|
146
|
+
case "Union" :
|
|
147
|
+
let match$2 = Stdlib_Array.reduce(irType._0.map((type_, i) => extractComplexTypes(baseName + `_Variant` + i.toString(), type_, threshold)), [
|
|
148
|
+
[],
|
|
149
|
+
[]
|
|
150
|
+
], (param, param$1) => [
|
|
151
|
+
param[0].concat([param$1[0]]),
|
|
152
|
+
param[1].concat(param$1[1])
|
|
153
|
+
]);
|
|
154
|
+
return [
|
|
155
|
+
{
|
|
156
|
+
TAG: "Union",
|
|
157
|
+
_0: match$2[0]
|
|
158
|
+
},
|
|
159
|
+
match$2[1]
|
|
160
|
+
];
|
|
161
|
+
case "Option" :
|
|
162
|
+
let match$3 = extractComplexTypes(baseName, irType._0, threshold);
|
|
163
|
+
return [
|
|
164
|
+
{
|
|
165
|
+
TAG: "Option",
|
|
166
|
+
_0: match$3[0]
|
|
167
|
+
},
|
|
168
|
+
match$3[1]
|
|
169
|
+
];
|
|
170
|
+
default:
|
|
171
|
+
return [
|
|
172
|
+
irType,
|
|
173
|
+
[]
|
|
174
|
+
];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function equals(_a, _b) {
|
|
179
|
+
while (true) {
|
|
180
|
+
let b = _b;
|
|
181
|
+
let a = _a;
|
|
182
|
+
if (typeof a !== "object") {
|
|
183
|
+
switch (a) {
|
|
184
|
+
case "Boolean" :
|
|
185
|
+
if (typeof b !== "object") {
|
|
186
|
+
return b === "Boolean";
|
|
187
|
+
} else {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
case "Null" :
|
|
191
|
+
if (typeof b !== "object") {
|
|
192
|
+
return b === "Null";
|
|
193
|
+
} else {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
case "Unknown" :
|
|
197
|
+
if (typeof b !== "object") {
|
|
198
|
+
return b === "Unknown";
|
|
199
|
+
} else {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
switch (a.TAG) {
|
|
205
|
+
case "String" :
|
|
206
|
+
if (typeof b !== "object") {
|
|
207
|
+
return false;
|
|
208
|
+
} else {
|
|
209
|
+
return b.TAG === "String";
|
|
210
|
+
}
|
|
211
|
+
case "Number" :
|
|
212
|
+
if (typeof b !== "object") {
|
|
213
|
+
return false;
|
|
214
|
+
} else {
|
|
215
|
+
return b.TAG === "Number";
|
|
216
|
+
}
|
|
217
|
+
case "Integer" :
|
|
218
|
+
if (typeof b !== "object") {
|
|
219
|
+
return false;
|
|
220
|
+
} else {
|
|
221
|
+
return b.TAG === "Integer";
|
|
222
|
+
}
|
|
223
|
+
case "Array" :
|
|
224
|
+
if (typeof b !== "object") {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (b.TAG !== "Array") {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
_b = b.items;
|
|
231
|
+
_a = a.items;
|
|
232
|
+
continue;
|
|
233
|
+
case "Literal" :
|
|
234
|
+
if (typeof b !== "object") {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
if (b.TAG !== "Literal") {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
let litB = b._0;
|
|
241
|
+
let litA = a._0;
|
|
242
|
+
if (typeof litA !== "object") {
|
|
243
|
+
return typeof litB !== "object";
|
|
244
|
+
}
|
|
245
|
+
switch (litA.TAG) {
|
|
246
|
+
case "StringLiteral" :
|
|
247
|
+
if (typeof litB !== "object" || litB.TAG !== "StringLiteral") {
|
|
248
|
+
return false;
|
|
249
|
+
} else {
|
|
250
|
+
return litA._0 === litB._0;
|
|
251
|
+
}
|
|
252
|
+
case "NumberLiteral" :
|
|
253
|
+
if (typeof litB !== "object" || litB.TAG !== "NumberLiteral") {
|
|
254
|
+
return false;
|
|
255
|
+
} else {
|
|
256
|
+
return litA._0 === litB._0;
|
|
257
|
+
}
|
|
258
|
+
case "BooleanLiteral" :
|
|
259
|
+
if (typeof litB !== "object" || litB.TAG !== "BooleanLiteral") {
|
|
260
|
+
return false;
|
|
261
|
+
} else {
|
|
262
|
+
return litA._0 === litB._0;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
case "Reference" :
|
|
266
|
+
if (typeof b !== "object" || b.TAG !== "Reference") {
|
|
267
|
+
return false;
|
|
268
|
+
} else {
|
|
269
|
+
return a._0 === b._0;
|
|
270
|
+
}
|
|
271
|
+
case "Option" :
|
|
272
|
+
if (typeof b !== "object") {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
if (b.TAG !== "Option") {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
_b = b._0;
|
|
279
|
+
_a = a._0;
|
|
280
|
+
continue;
|
|
281
|
+
default:
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function toString(irType) {
|
|
289
|
+
if (typeof irType !== "object") {
|
|
290
|
+
switch (irType) {
|
|
291
|
+
case "Boolean" :
|
|
292
|
+
return "Boolean";
|
|
293
|
+
case "Null" :
|
|
294
|
+
return "Null";
|
|
295
|
+
case "Unknown" :
|
|
296
|
+
return "Unknown";
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
switch (irType.TAG) {
|
|
300
|
+
case "String" :
|
|
301
|
+
return "String";
|
|
302
|
+
case "Number" :
|
|
303
|
+
return "Number";
|
|
304
|
+
case "Integer" :
|
|
305
|
+
return "Integer";
|
|
306
|
+
case "Array" :
|
|
307
|
+
return `Array<` + toString(irType.items) + `>`;
|
|
308
|
+
case "Object" :
|
|
309
|
+
let props = irType.properties.map(param => {
|
|
310
|
+
let req = param[2] ? "" : "?";
|
|
311
|
+
return param[0] + req + `: ` + toString(param[1]);
|
|
312
|
+
}).join(", ");
|
|
313
|
+
return `{ ` + props + ` }`;
|
|
314
|
+
case "Literal" :
|
|
315
|
+
let s = irType._0;
|
|
316
|
+
if (typeof s !== "object") {
|
|
317
|
+
return "null";
|
|
318
|
+
}
|
|
319
|
+
switch (s.TAG) {
|
|
320
|
+
case "StringLiteral" :
|
|
321
|
+
return `"` + s._0 + `"`;
|
|
322
|
+
case "NumberLiteral" :
|
|
323
|
+
return s._0.toString();
|
|
324
|
+
case "BooleanLiteral" :
|
|
325
|
+
if (s._0) {
|
|
326
|
+
return "true";
|
|
327
|
+
} else {
|
|
328
|
+
return "false";
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
case "Union" :
|
|
332
|
+
let typeStrs = irType._0.map(toString).join(" | ");
|
|
333
|
+
return `(` + typeStrs + `)`;
|
|
334
|
+
case "Intersection" :
|
|
335
|
+
let typeStrs$1 = irType._0.map(toString).join(" & ");
|
|
336
|
+
return `(` + typeStrs$1 + `)`;
|
|
337
|
+
case "Reference" :
|
|
338
|
+
return irType._0;
|
|
339
|
+
case "Option" :
|
|
340
|
+
return `Option<` + toString(irType._0) + `>`;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export {
|
|
346
|
+
isOptional,
|
|
347
|
+
unwrapOption,
|
|
348
|
+
makeOptional,
|
|
349
|
+
isSimpleType,
|
|
350
|
+
complexityScore,
|
|
351
|
+
extractComplexTypes,
|
|
352
|
+
equals,
|
|
353
|
+
toString,
|
|
354
|
+
}
|
|
355
|
+
/* No side effect */
|