@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,490 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as SchemaIR from "./SchemaIR.mjs";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
5
|
+
|
|
6
|
+
function parseTypeString(rawType) {
|
|
7
|
+
switch (rawType) {
|
|
8
|
+
case "array" :
|
|
9
|
+
return {
|
|
10
|
+
TAG: "Array",
|
|
11
|
+
_0: "Unknown"
|
|
12
|
+
};
|
|
13
|
+
case "boolean" :
|
|
14
|
+
return "Boolean";
|
|
15
|
+
case "integer" :
|
|
16
|
+
return "Integer";
|
|
17
|
+
case "null" :
|
|
18
|
+
return "Null";
|
|
19
|
+
case "number" :
|
|
20
|
+
return "Number";
|
|
21
|
+
case "object" :
|
|
22
|
+
return "Object";
|
|
23
|
+
case "string" :
|
|
24
|
+
return "String";
|
|
25
|
+
default:
|
|
26
|
+
switch (rawType) {
|
|
27
|
+
case "Boolean" :
|
|
28
|
+
return "Boolean";
|
|
29
|
+
case "Integer" :
|
|
30
|
+
return "Integer";
|
|
31
|
+
case "Null" :
|
|
32
|
+
return "Null";
|
|
33
|
+
case "Number" :
|
|
34
|
+
return "Number";
|
|
35
|
+
case "Object" :
|
|
36
|
+
return "Object";
|
|
37
|
+
case "String" :
|
|
38
|
+
return "String";
|
|
39
|
+
default:
|
|
40
|
+
return "Unknown";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function addWarning(ctx, warning) {
|
|
46
|
+
ctx.warnings.push(warning);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
|
|
50
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
51
|
+
if (depth > 30) {
|
|
52
|
+
addWarning(ctx, {
|
|
53
|
+
TAG: "DepthLimitReached",
|
|
54
|
+
depth: depth,
|
|
55
|
+
path: ctx.path
|
|
56
|
+
});
|
|
57
|
+
return "Unknown";
|
|
58
|
+
}
|
|
59
|
+
let ref = schema.$ref;
|
|
60
|
+
if (ref !== undefined) {
|
|
61
|
+
return {
|
|
62
|
+
TAG: "Reference",
|
|
63
|
+
_0: ref
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
let isNullable = Stdlib_Option.getOr(schema.nullable, false);
|
|
67
|
+
let normalizedType = Stdlib_Option.map(schema.type, parseTypeString);
|
|
68
|
+
let baseType;
|
|
69
|
+
if (normalizedType !== undefined) {
|
|
70
|
+
if (typeof normalizedType !== "object") {
|
|
71
|
+
switch (normalizedType) {
|
|
72
|
+
case "String" :
|
|
73
|
+
let constraints_minLength = schema.minLength;
|
|
74
|
+
let constraints_maxLength = schema.maxLength;
|
|
75
|
+
let constraints_pattern = schema.pattern;
|
|
76
|
+
let constraints = {
|
|
77
|
+
minLength: constraints_minLength,
|
|
78
|
+
maxLength: constraints_maxLength,
|
|
79
|
+
pattern: constraints_pattern
|
|
80
|
+
};
|
|
81
|
+
baseType = {
|
|
82
|
+
TAG: "String",
|
|
83
|
+
constraints: constraints
|
|
84
|
+
};
|
|
85
|
+
break;
|
|
86
|
+
case "Number" :
|
|
87
|
+
let constraints_minimum = schema.minimum;
|
|
88
|
+
let constraints_maximum = schema.maximum;
|
|
89
|
+
let constraints$1 = {
|
|
90
|
+
minimum: constraints_minimum,
|
|
91
|
+
maximum: constraints_maximum,
|
|
92
|
+
multipleOf: undefined
|
|
93
|
+
};
|
|
94
|
+
baseType = {
|
|
95
|
+
TAG: "Number",
|
|
96
|
+
constraints: constraints$1
|
|
97
|
+
};
|
|
98
|
+
break;
|
|
99
|
+
case "Integer" :
|
|
100
|
+
let constraints_minimum$1 = schema.minimum;
|
|
101
|
+
let constraints_maximum$1 = schema.maximum;
|
|
102
|
+
let constraints$2 = {
|
|
103
|
+
minimum: constraints_minimum$1,
|
|
104
|
+
maximum: constraints_maximum$1,
|
|
105
|
+
multipleOf: undefined
|
|
106
|
+
};
|
|
107
|
+
baseType = {
|
|
108
|
+
TAG: "Integer",
|
|
109
|
+
constraints: constraints$2
|
|
110
|
+
};
|
|
111
|
+
break;
|
|
112
|
+
case "Boolean" :
|
|
113
|
+
baseType = "Boolean";
|
|
114
|
+
break;
|
|
115
|
+
case "Object" :
|
|
116
|
+
let schemas = schema.allOf;
|
|
117
|
+
if (schemas !== undefined) {
|
|
118
|
+
let types = schemas.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
119
|
+
baseType = {
|
|
120
|
+
TAG: "Intersection",
|
|
121
|
+
_0: types
|
|
122
|
+
};
|
|
123
|
+
} else {
|
|
124
|
+
let propsDict = schema.properties;
|
|
125
|
+
let properties;
|
|
126
|
+
if (propsDict !== undefined) {
|
|
127
|
+
let required = Stdlib_Option.getOr(schema.required, []);
|
|
128
|
+
properties = Object.entries(propsDict).map(param => {
|
|
129
|
+
let name = param[0];
|
|
130
|
+
let isRequired = required.includes(name);
|
|
131
|
+
let propType = parseJsonSchemaWithContext(ctx, depth + 1 | 0, param[1]);
|
|
132
|
+
return [
|
|
133
|
+
name,
|
|
134
|
+
propType,
|
|
135
|
+
isRequired
|
|
136
|
+
];
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
properties = [];
|
|
140
|
+
}
|
|
141
|
+
baseType = {
|
|
142
|
+
TAG: "Object",
|
|
143
|
+
properties: properties,
|
|
144
|
+
additionalProperties: undefined
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
case "Null" :
|
|
149
|
+
baseType = "Null";
|
|
150
|
+
break;
|
|
151
|
+
case "Unknown" :
|
|
152
|
+
baseType = "Unknown";
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
let itemSchema = schema.items;
|
|
157
|
+
let items = itemSchema !== undefined ? parseJsonSchemaWithContext(ctx, depth + 1 | 0, itemSchema) : "Unknown";
|
|
158
|
+
baseType = {
|
|
159
|
+
TAG: "Array",
|
|
160
|
+
items: items,
|
|
161
|
+
constraints: {
|
|
162
|
+
minItems: undefined,
|
|
163
|
+
maxItems: undefined,
|
|
164
|
+
uniqueItems: false
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
let match = schema.enum;
|
|
170
|
+
let match$1 = schema.properties;
|
|
171
|
+
let match$2 = schema.allOf;
|
|
172
|
+
let match$3 = schema.oneOf;
|
|
173
|
+
let match$4 = schema.anyOf;
|
|
174
|
+
if (match !== undefined) {
|
|
175
|
+
let literals = match.map(value => {
|
|
176
|
+
if (value === null) {
|
|
177
|
+
return {
|
|
178
|
+
TAG: "Literal",
|
|
179
|
+
_0: "NullLiteral"
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(value)) {
|
|
183
|
+
return "Unknown";
|
|
184
|
+
}
|
|
185
|
+
switch (typeof value) {
|
|
186
|
+
case "boolean" :
|
|
187
|
+
return {
|
|
188
|
+
TAG: "Literal",
|
|
189
|
+
_0: {
|
|
190
|
+
TAG: "BooleanLiteral",
|
|
191
|
+
_0: value
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
case "string" :
|
|
195
|
+
return {
|
|
196
|
+
TAG: "Literal",
|
|
197
|
+
_0: {
|
|
198
|
+
TAG: "StringLiteral",
|
|
199
|
+
_0: value
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
case "number" :
|
|
203
|
+
return {
|
|
204
|
+
TAG: "Literal",
|
|
205
|
+
_0: {
|
|
206
|
+
TAG: "NumberLiteral",
|
|
207
|
+
_0: value
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
case "object" :
|
|
211
|
+
return "Unknown";
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
baseType = {
|
|
215
|
+
TAG: "Union",
|
|
216
|
+
_0: literals
|
|
217
|
+
};
|
|
218
|
+
} else if (match$1 !== undefined) {
|
|
219
|
+
baseType = parseJsonSchemaWithContext(ctx, depth + 1 | 0, {
|
|
220
|
+
type: "Object",
|
|
221
|
+
properties: schema.properties,
|
|
222
|
+
items: schema.items,
|
|
223
|
+
required: schema.required,
|
|
224
|
+
enum: schema.enum,
|
|
225
|
+
$ref: schema.$ref,
|
|
226
|
+
allOf: schema.allOf,
|
|
227
|
+
oneOf: schema.oneOf,
|
|
228
|
+
anyOf: schema.anyOf,
|
|
229
|
+
description: schema.description,
|
|
230
|
+
format: schema.format,
|
|
231
|
+
minLength: schema.minLength,
|
|
232
|
+
maxLength: schema.maxLength,
|
|
233
|
+
minimum: schema.minimum,
|
|
234
|
+
maximum: schema.maximum,
|
|
235
|
+
pattern: schema.pattern,
|
|
236
|
+
nullable: schema.nullable
|
|
237
|
+
});
|
|
238
|
+
} else if (match$2 !== undefined) {
|
|
239
|
+
let types$1 = match$2.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
240
|
+
baseType = {
|
|
241
|
+
TAG: "Intersection",
|
|
242
|
+
_0: types$1
|
|
243
|
+
};
|
|
244
|
+
} else if (match$3 !== undefined) {
|
|
245
|
+
let types$2 = match$3.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
246
|
+
baseType = {
|
|
247
|
+
TAG: "Union",
|
|
248
|
+
_0: types$2
|
|
249
|
+
};
|
|
250
|
+
} else if (match$4 !== undefined) {
|
|
251
|
+
let types$3 = match$4.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
252
|
+
baseType = {
|
|
253
|
+
TAG: "Union",
|
|
254
|
+
_0: types$3
|
|
255
|
+
};
|
|
256
|
+
} else {
|
|
257
|
+
baseType = "Unknown";
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (isNullable) {
|
|
261
|
+
return {
|
|
262
|
+
TAG: "Option",
|
|
263
|
+
_0: baseType
|
|
264
|
+
};
|
|
265
|
+
} else {
|
|
266
|
+
return baseType;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function parseJsonSchema(depthOpt, schema) {
|
|
271
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
272
|
+
let ctx = {
|
|
273
|
+
warnings: [],
|
|
274
|
+
path: "root"
|
|
275
|
+
};
|
|
276
|
+
let irType = parseJsonSchemaWithContext(ctx, depth, schema);
|
|
277
|
+
return [
|
|
278
|
+
irType,
|
|
279
|
+
ctx.warnings
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function parseNamedSchema(name, schema) {
|
|
284
|
+
let ctx = {
|
|
285
|
+
warnings: [],
|
|
286
|
+
path: `components.schemas.` + name
|
|
287
|
+
};
|
|
288
|
+
let type_ = parseJsonSchemaWithContext(ctx, undefined, schema);
|
|
289
|
+
return [
|
|
290
|
+
{
|
|
291
|
+
name: name,
|
|
292
|
+
description: schema.description,
|
|
293
|
+
type_: type_
|
|
294
|
+
},
|
|
295
|
+
ctx.warnings
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function parseComponentSchemas(schemas) {
|
|
300
|
+
let namedSchemas = {};
|
|
301
|
+
let allWarnings = [];
|
|
302
|
+
Object.entries(schemas).forEach(param => {
|
|
303
|
+
let name = param[0];
|
|
304
|
+
let match = parseNamedSchema(name, param[1]);
|
|
305
|
+
namedSchemas[name] = match[0];
|
|
306
|
+
allWarnings.push(...match[1]);
|
|
307
|
+
});
|
|
308
|
+
return [
|
|
309
|
+
{
|
|
310
|
+
schemas: namedSchemas
|
|
311
|
+
},
|
|
312
|
+
allWarnings
|
|
313
|
+
];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function resolveReference(context, ref) {
|
|
317
|
+
let parts = ref.split("/");
|
|
318
|
+
let schemaName = parts[parts.length - 1 | 0];
|
|
319
|
+
if (schemaName !== undefined) {
|
|
320
|
+
return context.schemas[schemaName];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function inlineSimpleReferences(context, _irType, _depthOpt, _maxDepthOpt) {
|
|
325
|
+
while (true) {
|
|
326
|
+
let depthOpt = _depthOpt;
|
|
327
|
+
let maxDepthOpt = _maxDepthOpt;
|
|
328
|
+
let irType = _irType;
|
|
329
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
330
|
+
let maxDepth = maxDepthOpt !== undefined ? maxDepthOpt : 2;
|
|
331
|
+
if (depth >= maxDepth) {
|
|
332
|
+
return irType;
|
|
333
|
+
}
|
|
334
|
+
if (typeof irType !== "object") {
|
|
335
|
+
return irType;
|
|
336
|
+
}
|
|
337
|
+
switch (irType.TAG) {
|
|
338
|
+
case "Array" :
|
|
339
|
+
return {
|
|
340
|
+
TAG: "Array",
|
|
341
|
+
items: inlineSimpleReferences(context, irType.items, depth, maxDepth),
|
|
342
|
+
constraints: irType.constraints
|
|
343
|
+
};
|
|
344
|
+
case "Object" :
|
|
345
|
+
let newProperties = irType.properties.map(param => [
|
|
346
|
+
param[0],
|
|
347
|
+
inlineSimpleReferences(context, param[1], depth, maxDepth),
|
|
348
|
+
param[2]
|
|
349
|
+
]);
|
|
350
|
+
let newAdditionalProps = Stdlib_Option.map(irType.additionalProperties, type_ => inlineSimpleReferences(context, type_, depth, maxDepth));
|
|
351
|
+
return {
|
|
352
|
+
TAG: "Object",
|
|
353
|
+
properties: newProperties,
|
|
354
|
+
additionalProperties: newAdditionalProps
|
|
355
|
+
};
|
|
356
|
+
case "Union" :
|
|
357
|
+
return {
|
|
358
|
+
TAG: "Union",
|
|
359
|
+
_0: irType._0.map(type_ => inlineSimpleReferences(context, type_, depth, maxDepth))
|
|
360
|
+
};
|
|
361
|
+
case "Intersection" :
|
|
362
|
+
return {
|
|
363
|
+
TAG: "Intersection",
|
|
364
|
+
_0: irType._0.map(type_ => inlineSimpleReferences(context, type_, depth, maxDepth))
|
|
365
|
+
};
|
|
366
|
+
case "Reference" :
|
|
367
|
+
let schema = resolveReference(context, irType._0);
|
|
368
|
+
if (schema === undefined) {
|
|
369
|
+
return irType;
|
|
370
|
+
}
|
|
371
|
+
if (!SchemaIR.isSimpleType(schema.type_)) {
|
|
372
|
+
return irType;
|
|
373
|
+
}
|
|
374
|
+
_maxDepthOpt = maxDepth;
|
|
375
|
+
_depthOpt = depth + 1 | 0;
|
|
376
|
+
_irType = schema.type_;
|
|
377
|
+
continue;
|
|
378
|
+
case "Option" :
|
|
379
|
+
return {
|
|
380
|
+
TAG: "Option",
|
|
381
|
+
_0: inlineSimpleReferences(context, irType._0, depth, maxDepth)
|
|
382
|
+
};
|
|
383
|
+
default:
|
|
384
|
+
return irType;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function optimizeIR(irType) {
|
|
390
|
+
if (typeof irType !== "object") {
|
|
391
|
+
return irType;
|
|
392
|
+
}
|
|
393
|
+
switch (irType.TAG) {
|
|
394
|
+
case "Array" :
|
|
395
|
+
return {
|
|
396
|
+
TAG: "Array",
|
|
397
|
+
items: optimizeIR(irType.items),
|
|
398
|
+
constraints: irType.constraints
|
|
399
|
+
};
|
|
400
|
+
case "Object" :
|
|
401
|
+
let newProperties = irType.properties.map(param => [
|
|
402
|
+
param[0],
|
|
403
|
+
optimizeIR(param[1]),
|
|
404
|
+
param[2]
|
|
405
|
+
]);
|
|
406
|
+
let newAdditionalProps = Stdlib_Option.map(irType.additionalProperties, optimizeIR);
|
|
407
|
+
return {
|
|
408
|
+
TAG: "Object",
|
|
409
|
+
properties: newProperties,
|
|
410
|
+
additionalProperties: newAdditionalProps
|
|
411
|
+
};
|
|
412
|
+
case "Union" :
|
|
413
|
+
let flattened = irType._0.flatMap(t => {
|
|
414
|
+
let inner = optimizeIR(t);
|
|
415
|
+
if (typeof inner !== "object") {
|
|
416
|
+
return [inner];
|
|
417
|
+
} else if (inner.TAG === "Union") {
|
|
418
|
+
return inner._0;
|
|
419
|
+
} else {
|
|
420
|
+
return [inner];
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
let unique = [];
|
|
424
|
+
flattened.forEach(type_ => {
|
|
425
|
+
let typeStr = SchemaIR.toString(type_);
|
|
426
|
+
let exists = unique.some(t => SchemaIR.toString(t) === typeStr);
|
|
427
|
+
if (!exists) {
|
|
428
|
+
unique.push(type_);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
let len = unique.length;
|
|
433
|
+
if (len !== 1) {
|
|
434
|
+
if (len !== 0) {
|
|
435
|
+
return {
|
|
436
|
+
TAG: "Union",
|
|
437
|
+
_0: unique
|
|
438
|
+
};
|
|
439
|
+
} else {
|
|
440
|
+
return "Unknown";
|
|
441
|
+
}
|
|
442
|
+
} else {
|
|
443
|
+
return unique[0];
|
|
444
|
+
}
|
|
445
|
+
case "Intersection" :
|
|
446
|
+
let flattened$1 = irType._0.flatMap(t => {
|
|
447
|
+
let inner = optimizeIR(t);
|
|
448
|
+
if (typeof inner !== "object") {
|
|
449
|
+
return [inner];
|
|
450
|
+
} else if (inner.TAG === "Intersection") {
|
|
451
|
+
return inner._0;
|
|
452
|
+
} else {
|
|
453
|
+
return [inner];
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
let len$1 = flattened$1.length;
|
|
457
|
+
if (len$1 !== 1) {
|
|
458
|
+
if (len$1 !== 0) {
|
|
459
|
+
return {
|
|
460
|
+
TAG: "Intersection",
|
|
461
|
+
_0: flattened$1
|
|
462
|
+
};
|
|
463
|
+
} else {
|
|
464
|
+
return "Unknown";
|
|
465
|
+
}
|
|
466
|
+
} else {
|
|
467
|
+
return flattened$1[0];
|
|
468
|
+
}
|
|
469
|
+
case "Option" :
|
|
470
|
+
return {
|
|
471
|
+
TAG: "Option",
|
|
472
|
+
_0: optimizeIR(irType._0)
|
|
473
|
+
};
|
|
474
|
+
default:
|
|
475
|
+
return irType;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export {
|
|
480
|
+
parseTypeString,
|
|
481
|
+
addWarning,
|
|
482
|
+
parseJsonSchemaWithContext,
|
|
483
|
+
parseJsonSchema,
|
|
484
|
+
parseNamedSchema,
|
|
485
|
+
parseComponentSchemas,
|
|
486
|
+
resolveReference,
|
|
487
|
+
inlineSimpleReferences,
|
|
488
|
+
optimizeIR,
|
|
489
|
+
}
|
|
490
|
+
/* No side effect */
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
|
|
4
|
+
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
|
|
5
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
6
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
7
|
+
import * as OpenapiParser from "@readme/openapi-parser";
|
|
8
|
+
|
|
9
|
+
function jsonToSpec(json) {
|
|
10
|
+
let obj = Stdlib_JSON.Decode.object(json);
|
|
11
|
+
if (obj === undefined) {
|
|
12
|
+
return {
|
|
13
|
+
TAG: "Error",
|
|
14
|
+
_0: "Invalid OpenAPI spec: root is not an object"
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
let pathsDict = Stdlib_Option.getOr(Stdlib_Option.flatMap(obj["paths"], Stdlib_JSON.Decode.object), {});
|
|
18
|
+
let infoObj = Stdlib_Option.getOr(Stdlib_Option.flatMap(obj["info"], Stdlib_JSON.Decode.object), {});
|
|
19
|
+
let openAPISpec_openapi = Stdlib_Option.getOr(Stdlib_Option.flatMap(obj["openapi"], Stdlib_JSON.Decode.string), "3.1.0");
|
|
20
|
+
let openAPISpec_info = {
|
|
21
|
+
title: Stdlib_Option.getOr(Stdlib_Option.flatMap(infoObj["title"], Stdlib_JSON.Decode.string), "Untitled API"),
|
|
22
|
+
version: Stdlib_Option.getOr(Stdlib_Option.flatMap(infoObj["version"], Stdlib_JSON.Decode.string), "0.0.0"),
|
|
23
|
+
description: Stdlib_Option.flatMap(infoObj["description"], Stdlib_JSON.Decode.string)
|
|
24
|
+
};
|
|
25
|
+
let openAPISpec_paths = Object.fromEntries(Object.entries(pathsDict).map(param => [
|
|
26
|
+
param[0],
|
|
27
|
+
param[1]
|
|
28
|
+
]));
|
|
29
|
+
let openAPISpec_components = Stdlib_Option.map(obj["components"], _comp => ({
|
|
30
|
+
schemas: Stdlib_Option.map(Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_JSON.Decode.object(_comp), c => c["schemas"]), Stdlib_JSON.Decode.object), schemas => Object.fromEntries(Object.entries(schemas).map(param => [
|
|
31
|
+
param[0],
|
|
32
|
+
param[1]
|
|
33
|
+
])))
|
|
34
|
+
}));
|
|
35
|
+
let openAPISpec = {
|
|
36
|
+
openapi: openAPISpec_openapi,
|
|
37
|
+
info: openAPISpec_info,
|
|
38
|
+
paths: openAPISpec_paths,
|
|
39
|
+
components: openAPISpec_components
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
TAG: "Ok",
|
|
43
|
+
_0: openAPISpec
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function resolve(source, timeout) {
|
|
48
|
+
try {
|
|
49
|
+
let options = {
|
|
50
|
+
timeoutMs: Stdlib_Option.getOr(timeout, 60000)
|
|
51
|
+
};
|
|
52
|
+
return jsonToSpec(await OpenapiParser.bundle(source, options));
|
|
53
|
+
} catch (raw_err) {
|
|
54
|
+
let err = Primitive_exceptions.internalToException(raw_err);
|
|
55
|
+
if (err.RE_EXN_ID !== "JsExn") {
|
|
56
|
+
return {
|
|
57
|
+
TAG: "Error",
|
|
58
|
+
_0: "Unknown error resolving spec"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
let message = Stdlib_Option.getOr(Stdlib_JsExn.message(err._1), "Unknown error resolving spec");
|
|
62
|
+
return {
|
|
63
|
+
TAG: "Error",
|
|
64
|
+
_0: `Failed to resolve spec: ` + message
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function parseOnly(source, timeout) {
|
|
70
|
+
try {
|
|
71
|
+
let options = {
|
|
72
|
+
timeoutMs: Stdlib_Option.getOr(timeout, 60000)
|
|
73
|
+
};
|
|
74
|
+
return jsonToSpec(await OpenapiParser.parse(source, options));
|
|
75
|
+
} catch (raw_err) {
|
|
76
|
+
let err = Primitive_exceptions.internalToException(raw_err);
|
|
77
|
+
if (err.RE_EXN_ID !== "JsExn") {
|
|
78
|
+
return {
|
|
79
|
+
TAG: "Error",
|
|
80
|
+
_0: "Unknown error parsing spec"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
let message = Stdlib_Option.getOr(Stdlib_JsExn.message(err._1), "Unknown error parsing spec");
|
|
84
|
+
return {
|
|
85
|
+
TAG: "Error",
|
|
86
|
+
_0: `Failed to parse spec: ` + message
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function bundleSpec(source, timeout) {
|
|
92
|
+
try {
|
|
93
|
+
let options = {
|
|
94
|
+
timeoutMs: Stdlib_Option.getOr(timeout, 60000)
|
|
95
|
+
};
|
|
96
|
+
let bundled = await OpenapiParser.bundle(source, options);
|
|
97
|
+
return {
|
|
98
|
+
TAG: "Ok",
|
|
99
|
+
_0: bundled
|
|
100
|
+
};
|
|
101
|
+
} catch (raw_err) {
|
|
102
|
+
let err = Primitive_exceptions.internalToException(raw_err);
|
|
103
|
+
if (err.RE_EXN_ID !== "JsExn") {
|
|
104
|
+
return {
|
|
105
|
+
TAG: "Error",
|
|
106
|
+
_0: "Unknown error bundling spec"
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
let message = Stdlib_Option.getOr(Stdlib_JsExn.message(err._1), "Unknown error bundling spec");
|
|
110
|
+
return {
|
|
111
|
+
TAG: "Error",
|
|
112
|
+
_0: `Failed to bundle spec: ` + message
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function validateSpec(source, timeout) {
|
|
118
|
+
try {
|
|
119
|
+
let options = {
|
|
120
|
+
timeoutMs: Stdlib_Option.getOr(timeout, 60000)
|
|
121
|
+
};
|
|
122
|
+
return jsonToSpec(await OpenapiParser.validate(source, options));
|
|
123
|
+
} catch (raw_err) {
|
|
124
|
+
let err = Primitive_exceptions.internalToException(raw_err);
|
|
125
|
+
if (err.RE_EXN_ID !== "JsExn") {
|
|
126
|
+
return {
|
|
127
|
+
TAG: "Error",
|
|
128
|
+
_0: "Unknown error validating spec"
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
let message = Stdlib_Option.getOr(Stdlib_JsExn.message(err._1), "Unknown error validating spec");
|
|
132
|
+
return {
|
|
133
|
+
TAG: "Error",
|
|
134
|
+
_0: `Failed to validate spec: ` + message
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
jsonToSpec,
|
|
141
|
+
resolve,
|
|
142
|
+
parseOnly,
|
|
143
|
+
bundleSpec,
|
|
144
|
+
validateSpec,
|
|
145
|
+
}
|
|
146
|
+
/* Stdlib_JsExn Not a pure module */
|