@f3liz/rescript-autogen-openapi 0.2.0 → 0.3.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.
@@ -0,0 +1,12 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import Toposort from "toposort";
4
+
5
+ function sortArray(nodes, edges) {
6
+ return Toposort.array(nodes, edges);
7
+ }
8
+
9
+ export {
10
+ sortArray,
11
+ }
12
+ /* toposort Not a pure module */
@@ -167,6 +167,79 @@ function generateDocString(summary, description, param) {
167
167
  }), "");
168
168
  }
169
169
 
170
+ function variantConstructorName(_irType) {
171
+ while (true) {
172
+ let irType = _irType;
173
+ if (typeof irType !== "object") {
174
+ switch (irType) {
175
+ case "Boolean" :
176
+ return "Bool";
177
+ case "Null" :
178
+ return "Null";
179
+ case "Unknown" :
180
+ return "Unknown";
181
+ }
182
+ } else {
183
+ switch (irType.TAG) {
184
+ case "String" :
185
+ return "String";
186
+ case "Number" :
187
+ return "Float";
188
+ case "Integer" :
189
+ return "Int";
190
+ case "Array" :
191
+ return "Array";
192
+ case "Object" :
193
+ return "Object";
194
+ case "Literal" :
195
+ let s = irType._0;
196
+ if (typeof s !== "object") {
197
+ return "Null";
198
+ }
199
+ switch (s.TAG) {
200
+ case "StringLiteral" :
201
+ return JsConvertCase.toPascalCase(s._0);
202
+ case "NumberLiteral" :
203
+ return "Number";
204
+ case "BooleanLiteral" :
205
+ return "Bool";
206
+ }
207
+ case "Union" :
208
+ return "Union";
209
+ case "Intersection" :
210
+ return "Intersection";
211
+ case "Reference" :
212
+ let ref = irType._0;
213
+ return JsConvertCase.toPascalCase(ref.includes("/") ? Stdlib_Option.getOr(ref.split("/")[ref.split("/").length - 1 | 0], "Ref") : ref);
214
+ case "Option" :
215
+ _irType = irType._0;
216
+ continue;
217
+ }
218
+ }
219
+ };
220
+ }
221
+
222
+ function deduplicateNames(names) {
223
+ let counts = {};
224
+ let result = [];
225
+ names.forEach(name => {
226
+ let count = Stdlib_Option.getOr(counts[name], 0);
227
+ counts[name] = count + 1 | 0;
228
+ });
229
+ let seen = {};
230
+ names.forEach(name => {
231
+ let total = Stdlib_Option.getOr(counts[name], 1);
232
+ if (total > 1) {
233
+ let idx = Stdlib_Option.getOr(seen[name], 0) + 1 | 0;
234
+ seen[name] = idx;
235
+ result.push(name + idx.toString());
236
+ return;
237
+ }
238
+ result.push(name);
239
+ });
240
+ return result;
241
+ }
242
+
170
243
  let fetchTypeSignature = "(~url: string, ~method_: string, ~body: option<JSON.t>) => Promise.t<JSON.t>";
171
244
 
172
245
  export {
@@ -182,5 +255,7 @@ export {
182
255
  generateDocComment,
183
256
  generateDocString,
184
257
  fetchTypeSignature,
258
+ variantConstructorName,
259
+ deduplicateNames,
185
260
  }
186
261
  /* js-convert-case Not a pure module */
@@ -230,6 +230,36 @@ function equals(_a, _b) {
230
230
  _b = b.items;
231
231
  _a = a.items;
232
232
  continue;
233
+ case "Object" :
234
+ if (typeof b !== "object") {
235
+ return false;
236
+ }
237
+ if (b.TAG !== "Object") {
238
+ return false;
239
+ }
240
+ let addB = b.additionalProperties;
241
+ let propsB = b.properties;
242
+ let addA = a.additionalProperties;
243
+ let propsA = a.properties;
244
+ if (!(propsA.length === propsB.length && propsA.every((propA, i) => {
245
+ let propB = propsB[i];
246
+ if (propB !== undefined && propA[0] === propB[0] && propA[2] === propB[2]) {
247
+ return equals(propA[1], propB[1]);
248
+ } else {
249
+ return false;
250
+ }
251
+ }))) {
252
+ return false;
253
+ }
254
+ if (addA === undefined) {
255
+ return addB === undefined;
256
+ }
257
+ if (addB === undefined) {
258
+ return false;
259
+ }
260
+ _b = addB;
261
+ _a = addA;
262
+ continue;
233
263
  case "Literal" :
234
264
  if (typeof b !== "object") {
235
265
  return false;
@@ -262,6 +292,48 @@ function equals(_a, _b) {
262
292
  return litA._0 === litB._0;
263
293
  }
264
294
  }
295
+ case "Union" :
296
+ if (typeof b !== "object") {
297
+ return false;
298
+ }
299
+ if (b.TAG !== "Union") {
300
+ return false;
301
+ }
302
+ let typesB = b._0;
303
+ let typesA = a._0;
304
+ if (typesA.length === typesB.length) {
305
+ return typesA.every((tA, i) => {
306
+ let tB = typesB[i];
307
+ if (tB !== undefined) {
308
+ return equals(tA, tB);
309
+ } else {
310
+ return false;
311
+ }
312
+ });
313
+ } else {
314
+ return false;
315
+ }
316
+ case "Intersection" :
317
+ if (typeof b !== "object") {
318
+ return false;
319
+ }
320
+ if (b.TAG !== "Intersection") {
321
+ return false;
322
+ }
323
+ let typesB$1 = b._0;
324
+ let typesA$1 = a._0;
325
+ if (typesA$1.length === typesB$1.length) {
326
+ return typesA$1.every((tA, i) => {
327
+ let tB = typesB$1[i];
328
+ if (tB !== undefined) {
329
+ return equals(tA, tB);
330
+ } else {
331
+ return false;
332
+ }
333
+ });
334
+ } else {
335
+ return false;
336
+ }
265
337
  case "Reference" :
266
338
  if (typeof b !== "object" || b.TAG !== "Reference") {
267
339
  return false;
@@ -278,8 +350,6 @@ function equals(_a, _b) {
278
350
  _b = b._0;
279
351
  _a = a._0;
280
352
  continue;
281
- default:
282
- return false;
283
353
  }
284
354
  }
285
355
  };
@@ -42,6 +42,12 @@ function parseTypeString(rawType) {
42
42
  }
43
43
  }
44
44
 
45
+ function parseTypeAsArray(rawType) {
46
+ if (Array.isArray(rawType)) {
47
+ return rawType.map(parseTypeString);
48
+ }
49
+ }
50
+
45
51
  function addWarning(ctx, warning) {
46
52
  ctx.warnings.push(warning);
47
53
  }
@@ -64,67 +70,188 @@ function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
64
70
  };
65
71
  }
66
72
  let isNullable = Stdlib_Option.getOr(schema.nullable, false);
67
- let normalizedType = Stdlib_Option.map(schema.type, parseTypeString);
68
- let baseType;
73
+ let hasComposition = Stdlib_Option.isSome(schema.anyOf) || Stdlib_Option.isSome(schema.oneOf);
74
+ let typeAsArray = hasComposition ? undefined : Stdlib_Option.flatMap(schema.type, parseTypeAsArray);
75
+ let schema$1 = hasComposition && Stdlib_Option.isSome(Stdlib_Option.flatMap(schema.type, parseTypeAsArray)) ? ({
76
+ type: undefined,
77
+ properties: schema.properties,
78
+ items: schema.items,
79
+ required: schema.required,
80
+ enum: schema.enum,
81
+ $ref: schema.$ref,
82
+ allOf: schema.allOf,
83
+ oneOf: schema.oneOf,
84
+ anyOf: schema.anyOf,
85
+ description: schema.description,
86
+ format: schema.format,
87
+ minLength: schema.minLength,
88
+ maxLength: schema.maxLength,
89
+ minimum: schema.minimum,
90
+ maximum: schema.maximum,
91
+ pattern: schema.pattern,
92
+ nullable: schema.nullable
93
+ }) : schema;
94
+ if (typeAsArray !== undefined) {
95
+ if (typeAsArray.length > 1) {
96
+ let irTypes = typeAsArray.map(t => {
97
+ let subSchema_type = t;
98
+ let subSchema_properties = schema$1.properties;
99
+ let subSchema_items = schema$1.items;
100
+ let subSchema_required = schema$1.required;
101
+ let subSchema_enum = schema$1.enum;
102
+ let subSchema_$ref = schema$1.$ref;
103
+ let subSchema_allOf = schema$1.allOf;
104
+ let subSchema_oneOf = schema$1.oneOf;
105
+ let subSchema_anyOf = schema$1.anyOf;
106
+ let subSchema_description = schema$1.description;
107
+ let subSchema_format = schema$1.format;
108
+ let subSchema_minLength = schema$1.minLength;
109
+ let subSchema_maxLength = schema$1.maxLength;
110
+ let subSchema_minimum = schema$1.minimum;
111
+ let subSchema_maximum = schema$1.maximum;
112
+ let subSchema_pattern = schema$1.pattern;
113
+ let subSchema = {
114
+ type: subSchema_type,
115
+ properties: subSchema_properties,
116
+ items: subSchema_items,
117
+ required: subSchema_required,
118
+ enum: subSchema_enum,
119
+ $ref: subSchema_$ref,
120
+ allOf: subSchema_allOf,
121
+ oneOf: subSchema_oneOf,
122
+ anyOf: subSchema_anyOf,
123
+ description: subSchema_description,
124
+ format: subSchema_format,
125
+ minLength: subSchema_minLength,
126
+ maxLength: subSchema_maxLength,
127
+ minimum: subSchema_minimum,
128
+ maximum: subSchema_maximum,
129
+ pattern: subSchema_pattern,
130
+ nullable: undefined
131
+ };
132
+ return parseJsonSchemaWithContext(ctx, depth + 1 | 0, subSchema);
133
+ });
134
+ let baseType = {
135
+ TAG: "Union",
136
+ _0: irTypes
137
+ };
138
+ if (isNullable) {
139
+ return {
140
+ TAG: "Option",
141
+ _0: baseType
142
+ };
143
+ } else {
144
+ return baseType;
145
+ }
146
+ }
147
+ if (typeAsArray.length === 1) {
148
+ let subSchema_type = typeAsArray[0];
149
+ let subSchema_properties = schema$1.properties;
150
+ let subSchema_items = schema$1.items;
151
+ let subSchema_required = schema$1.required;
152
+ let subSchema_enum = schema$1.enum;
153
+ let subSchema_$ref = schema$1.$ref;
154
+ let subSchema_allOf = schema$1.allOf;
155
+ let subSchema_oneOf = schema$1.oneOf;
156
+ let subSchema_anyOf = schema$1.anyOf;
157
+ let subSchema_description = schema$1.description;
158
+ let subSchema_format = schema$1.format;
159
+ let subSchema_minLength = schema$1.minLength;
160
+ let subSchema_maxLength = schema$1.maxLength;
161
+ let subSchema_minimum = schema$1.minimum;
162
+ let subSchema_maximum = schema$1.maximum;
163
+ let subSchema_pattern = schema$1.pattern;
164
+ let subSchema = {
165
+ type: subSchema_type,
166
+ properties: subSchema_properties,
167
+ items: subSchema_items,
168
+ required: subSchema_required,
169
+ enum: subSchema_enum,
170
+ $ref: subSchema_$ref,
171
+ allOf: subSchema_allOf,
172
+ oneOf: subSchema_oneOf,
173
+ anyOf: subSchema_anyOf,
174
+ description: subSchema_description,
175
+ format: subSchema_format,
176
+ minLength: subSchema_minLength,
177
+ maxLength: subSchema_maxLength,
178
+ minimum: subSchema_minimum,
179
+ maximum: subSchema_maximum,
180
+ pattern: subSchema_pattern,
181
+ nullable: undefined
182
+ };
183
+ let baseType$1 = parseJsonSchemaWithContext(ctx, depth + 1 | 0, subSchema);
184
+ if (isNullable) {
185
+ return {
186
+ TAG: "Option",
187
+ _0: baseType$1
188
+ };
189
+ } else {
190
+ return baseType$1;
191
+ }
192
+ }
193
+ }
194
+ let normalizedType = Stdlib_Option.map(schema$1.type, parseTypeString);
195
+ let baseType$2;
69
196
  if (normalizedType !== undefined) {
70
197
  if (typeof normalizedType !== "object") {
71
198
  switch (normalizedType) {
72
199
  case "String" :
73
- let constraints_minLength = schema.minLength;
74
- let constraints_maxLength = schema.maxLength;
75
- let constraints_pattern = schema.pattern;
200
+ let constraints_minLength = schema$1.minLength;
201
+ let constraints_maxLength = schema$1.maxLength;
202
+ let constraints_pattern = schema$1.pattern;
76
203
  let constraints = {
77
204
  minLength: constraints_minLength,
78
205
  maxLength: constraints_maxLength,
79
206
  pattern: constraints_pattern
80
207
  };
81
- baseType = {
208
+ baseType$2 = {
82
209
  TAG: "String",
83
210
  constraints: constraints
84
211
  };
85
212
  break;
86
213
  case "Number" :
87
- let constraints_minimum = schema.minimum;
88
- let constraints_maximum = schema.maximum;
214
+ let constraints_minimum = schema$1.minimum;
215
+ let constraints_maximum = schema$1.maximum;
89
216
  let constraints$1 = {
90
217
  minimum: constraints_minimum,
91
218
  maximum: constraints_maximum,
92
219
  multipleOf: undefined
93
220
  };
94
- baseType = {
221
+ baseType$2 = {
95
222
  TAG: "Number",
96
223
  constraints: constraints$1
97
224
  };
98
225
  break;
99
226
  case "Integer" :
100
- let constraints_minimum$1 = schema.minimum;
101
- let constraints_maximum$1 = schema.maximum;
227
+ let constraints_minimum$1 = schema$1.minimum;
228
+ let constraints_maximum$1 = schema$1.maximum;
102
229
  let constraints$2 = {
103
230
  minimum: constraints_minimum$1,
104
231
  maximum: constraints_maximum$1,
105
232
  multipleOf: undefined
106
233
  };
107
- baseType = {
234
+ baseType$2 = {
108
235
  TAG: "Integer",
109
236
  constraints: constraints$2
110
237
  };
111
238
  break;
112
239
  case "Boolean" :
113
- baseType = "Boolean";
240
+ baseType$2 = "Boolean";
114
241
  break;
115
242
  case "Object" :
116
- let schemas = schema.allOf;
243
+ let schemas = schema$1.allOf;
117
244
  if (schemas !== undefined) {
118
245
  let types = schemas.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
119
- baseType = {
246
+ baseType$2 = {
120
247
  TAG: "Intersection",
121
248
  _0: types
122
249
  };
123
250
  } else {
124
- let propsDict = schema.properties;
251
+ let propsDict = schema$1.properties;
125
252
  let properties;
126
253
  if (propsDict !== undefined) {
127
- let required = Stdlib_Option.getOr(schema.required, []);
254
+ let required = Stdlib_Option.getOr(schema$1.required, []);
128
255
  properties = Object.entries(propsDict).map(param => {
129
256
  let name = param[0];
130
257
  let isRequired = required.includes(name);
@@ -138,7 +265,7 @@ function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
138
265
  } else {
139
266
  properties = [];
140
267
  }
141
- baseType = {
268
+ baseType$2 = {
142
269
  TAG: "Object",
143
270
  properties: properties,
144
271
  additionalProperties: undefined
@@ -146,16 +273,16 @@ function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
146
273
  }
147
274
  break;
148
275
  case "Null" :
149
- baseType = "Null";
276
+ baseType$2 = "Null";
150
277
  break;
151
278
  case "Unknown" :
152
- baseType = "Unknown";
279
+ baseType$2 = "Unknown";
153
280
  break;
154
281
  }
155
282
  } else {
156
- let itemSchema = schema.items;
283
+ let itemSchema = schema$1.items;
157
284
  let items = itemSchema !== undefined ? parseJsonSchemaWithContext(ctx, depth + 1 | 0, itemSchema) : "Unknown";
158
- baseType = {
285
+ baseType$2 = {
159
286
  TAG: "Array",
160
287
  items: items,
161
288
  constraints: {
@@ -166,11 +293,11 @@ function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
166
293
  };
167
294
  }
168
295
  } 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;
296
+ let match = schema$1.enum;
297
+ let match$1 = schema$1.properties;
298
+ let match$2 = schema$1.allOf;
299
+ let match$3 = schema$1.oneOf;
300
+ let match$4 = schema$1.anyOf;
174
301
  if (match !== undefined) {
175
302
  let literals = match.map(value => {
176
303
  if (value === null) {
@@ -211,59 +338,59 @@ function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
211
338
  return "Unknown";
212
339
  }
213
340
  });
214
- baseType = {
341
+ baseType$2 = {
215
342
  TAG: "Union",
216
343
  _0: literals
217
344
  };
218
345
  } else if (match$1 !== undefined) {
219
- baseType = parseJsonSchemaWithContext(ctx, depth + 1 | 0, {
346
+ baseType$2 = parseJsonSchemaWithContext(ctx, depth + 1 | 0, {
220
347
  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
348
+ properties: schema$1.properties,
349
+ items: schema$1.items,
350
+ required: schema$1.required,
351
+ enum: schema$1.enum,
352
+ $ref: schema$1.$ref,
353
+ allOf: schema$1.allOf,
354
+ oneOf: schema$1.oneOf,
355
+ anyOf: schema$1.anyOf,
356
+ description: schema$1.description,
357
+ format: schema$1.format,
358
+ minLength: schema$1.minLength,
359
+ maxLength: schema$1.maxLength,
360
+ minimum: schema$1.minimum,
361
+ maximum: schema$1.maximum,
362
+ pattern: schema$1.pattern,
363
+ nullable: undefined
237
364
  });
238
365
  } else if (match$2 !== undefined) {
239
366
  let types$1 = match$2.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
240
- baseType = {
367
+ baseType$2 = {
241
368
  TAG: "Intersection",
242
369
  _0: types$1
243
370
  };
244
371
  } else if (match$3 !== undefined) {
245
372
  let types$2 = match$3.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
246
- baseType = {
373
+ baseType$2 = {
247
374
  TAG: "Union",
248
375
  _0: types$2
249
376
  };
250
377
  } else if (match$4 !== undefined) {
251
378
  let types$3 = match$4.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
252
- baseType = {
379
+ baseType$2 = {
253
380
  TAG: "Union",
254
381
  _0: types$3
255
382
  };
256
383
  } else {
257
- baseType = "Unknown";
384
+ baseType$2 = "Unknown";
258
385
  }
259
386
  }
260
387
  if (isNullable) {
261
388
  return {
262
389
  TAG: "Option",
263
- _0: baseType
390
+ _0: baseType$2
264
391
  };
265
392
  } else {
266
- return baseType;
393
+ return baseType$2;
267
394
  }
268
395
  }
269
396
 
@@ -296,6 +423,60 @@ function parseNamedSchema(name, schema) {
296
423
  ];
297
424
  }
298
425
 
426
+ function normalizeReferences(availableNames, irType) {
427
+ if (typeof irType !== "object") {
428
+ return irType;
429
+ }
430
+ switch (irType.TAG) {
431
+ case "Array" :
432
+ return {
433
+ TAG: "Array",
434
+ items: normalizeReferences(availableNames, irType.items),
435
+ constraints: irType.constraints
436
+ };
437
+ case "Object" :
438
+ let newProperties = irType.properties.map(param => [
439
+ param[0],
440
+ normalizeReferences(availableNames, param[1]),
441
+ param[2]
442
+ ]);
443
+ let newAdditional = Stdlib_Option.map(irType.additionalProperties, t => normalizeReferences(availableNames, t));
444
+ return {
445
+ TAG: "Object",
446
+ properties: newProperties,
447
+ additionalProperties: newAdditional
448
+ };
449
+ case "Union" :
450
+ return {
451
+ TAG: "Union",
452
+ _0: irType._0.map(t => normalizeReferences(availableNames, t))
453
+ };
454
+ case "Intersection" :
455
+ return {
456
+ TAG: "Intersection",
457
+ _0: irType._0.map(t => normalizeReferences(availableNames, t))
458
+ };
459
+ case "Reference" :
460
+ let parts = irType._0.split("/");
461
+ let name = Stdlib_Option.getOr(parts[parts.length - 1 | 0], "");
462
+ if (availableNames.includes(name)) {
463
+ return {
464
+ TAG: "Reference",
465
+ _0: name
466
+ };
467
+ } else {
468
+ return irType;
469
+ }
470
+ case "Option" :
471
+ return {
472
+ TAG: "Option",
473
+ _0: normalizeReferences(availableNames, irType._0)
474
+ };
475
+ default:
476
+ return irType;
477
+ }
478
+ }
479
+
299
480
  function parseComponentSchemas(schemas) {
300
481
  let namedSchemas = {};
301
482
  let allWarnings = [];
@@ -305,6 +486,16 @@ function parseComponentSchemas(schemas) {
305
486
  namedSchemas[name] = match[0];
306
487
  allWarnings.push(...match[1]);
307
488
  });
489
+ let availableNames = Object.keys(namedSchemas);
490
+ Object.entries(namedSchemas).forEach(param => {
491
+ let namedSchema = param[1];
492
+ let resolved = normalizeReferences(availableNames, namedSchema.type_);
493
+ namedSchemas[param[0]] = {
494
+ name: namedSchema.name,
495
+ description: namedSchema.description,
496
+ type_: resolved
497
+ };
498
+ });
308
499
  return [
309
500
  {
310
501
  schemas: namedSchemas
@@ -478,10 +669,12 @@ function optimizeIR(irType) {
478
669
 
479
670
  export {
480
671
  parseTypeString,
672
+ parseTypeAsArray,
481
673
  addWarning,
482
674
  parseJsonSchemaWithContext,
483
675
  parseJsonSchema,
484
676
  parseNamedSchema,
677
+ normalizeReferences,
485
678
  parseComponentSchemas,
486
679
  resolveReference,
487
680
  inlineSimpleReferences,