osury 1.1.0 → 1.2.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.
@@ -1,726 +0,0 @@
1
- // Generated by ReScript, PLEASE EDIT WITH CARE
2
-
3
- import * as Errors from "./Errors.res.mjs";
4
- import * as Schema from "./Schema.res.mjs";
5
- import * as Core__Array from "@rescript/core/src/Core__Array.res.mjs";
6
- import * as Core__Option from "@rescript/core/src/Core__Option.res.mjs";
7
-
8
- function pathToName(path) {
9
- return path.split("/").filter(s => {
10
- if (s !== "") {
11
- return !s.startsWith("{");
12
- } else {
13
- return false;
14
- }
15
- }).map(segment => segment.split("-").map(part => {
16
- let first = part.charAt(0).toUpperCase();
17
- let rest = part.slice(1);
18
- return first + rest;
19
- }).join("")).join("");
20
- }
21
-
22
- function ucFirst(s) {
23
- let first = s.charAt(0).toUpperCase();
24
- let rest = s.slice(1);
25
- return first + rest;
26
- }
27
-
28
- function parsePathResponses(pathsJson) {
29
- if (typeof pathsJson !== "object" || pathsJson === null || Array.isArray(pathsJson)) {
30
- return {
31
- TAG: "Ok",
32
- _0: []
33
- };
34
- }
35
- let results = Object.entries(pathsJson).flatMap(param => {
36
- let methodsJson = param[1];
37
- let path = param[0];
38
- if (typeof methodsJson === "object" && methodsJson !== null && !Array.isArray(methodsJson)) {
39
- return Core__Array.filterMap(Object.entries(methodsJson), param => {
40
- let opJson = param[1];
41
- let method = param[0];
42
- let httpMethods = [
43
- "get",
44
- "post",
45
- "put",
46
- "patch",
47
- "delete"
48
- ];
49
- if (!httpMethods.includes(method)) {
50
- return;
51
- }
52
- if (typeof opJson !== "object" || opJson === null || Array.isArray(opJson)) {
53
- return;
54
- }
55
- let match = opJson["responses"];
56
- if (match === undefined) {
57
- return;
58
- }
59
- if (typeof match !== "object" || match === null || Array.isArray(match)) {
60
- return;
61
- }
62
- let r = match["200"];
63
- let responseJson = r !== undefined ? r : match["201"];
64
- if (responseJson === undefined) {
65
- return;
66
- }
67
- if (typeof responseJson !== "object" || responseJson === null || Array.isArray(responseJson)) {
68
- return;
69
- }
70
- let match$1 = responseJson["content"];
71
- if (match$1 === undefined) {
72
- return;
73
- }
74
- if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
75
- return;
76
- }
77
- let match$2 = match$1["application/json"];
78
- if (match$2 === undefined) {
79
- return;
80
- }
81
- if (typeof match$2 !== "object" || match$2 === null || Array.isArray(match$2)) {
82
- return;
83
- }
84
- let schemaJson = match$2["schema"];
85
- if (schemaJson === undefined) {
86
- return;
87
- }
88
- let name = ucFirst(method) + pathToName(path) + "Response";
89
- let schemaType = Schema.parse(schemaJson);
90
- if (schemaType.TAG === "Ok") {
91
- return {
92
- TAG: "Ok",
93
- _0: {
94
- name: name,
95
- schema: schemaType._0,
96
- discriminatorTag: undefined,
97
- discriminatorPropertyName: undefined,
98
- fieldDiscriminators: undefined
99
- }
100
- };
101
- } else {
102
- return {
103
- TAG: "Error",
104
- _0: schemaType._0
105
- };
106
- }
107
- });
108
- } else {
109
- return [];
110
- }
111
- });
112
- let errors = Core__Array.filterMap(results, r => {
113
- if (r.TAG === "Ok") {
114
- return;
115
- } else {
116
- return r._0;
117
- }
118
- }).flat();
119
- if (errors.length > 0) {
120
- return {
121
- TAG: "Error",
122
- _0: errors
123
- };
124
- }
125
- let schemas = Core__Array.filterMap(results, r => {
126
- if (r.TAG === "Ok") {
127
- return r._0;
128
- }
129
- });
130
- return {
131
- TAG: "Ok",
132
- _0: schemas
133
- };
134
- }
135
-
136
- function extractDiscriminatorFromPair(items, discDict) {
137
- let match = discDict["propertyName"];
138
- if (match === undefined) {
139
- return;
140
- }
141
- if (typeof match !== "string") {
142
- return;
143
- }
144
- let memberNames = Core__Array.filterMap(items, item => {
145
- if (typeof item !== "object" || item === null || Array.isArray(item)) {
146
- return;
147
- }
148
- let match = item["$ref"];
149
- if (match === undefined) {
150
- return;
151
- }
152
- if (typeof match !== "string") {
153
- return;
154
- }
155
- let parts = match.split("/");
156
- return parts[parts.length - 1 | 0];
157
- });
158
- if (memberNames.length < 2) {
159
- return;
160
- }
161
- let lcNames = memberNames.map(n => {
162
- let first = n.charAt(0).toLowerCase();
163
- let rest = n.slice(1);
164
- return first + rest;
165
- });
166
- let firstName = Core__Option.getOr(lcNames[0], "unknown");
167
- let restNames = lcNames.slice(1);
168
- let unionName = firstName + restNames.map(n => "Or" + ucFirst(n)).join("");
169
- return [
170
- unionName,
171
- match
172
- ];
173
- }
174
-
175
- function extractFieldDiscriminators(schemaJson) {
176
- let result = {};
177
- if (typeof schemaJson === "object" && schemaJson !== null && !Array.isArray(schemaJson)) {
178
- let match = schemaJson["properties"];
179
- if (typeof match === "object" && match !== null && !Array.isArray(match)) {
180
- Object.entries(match).forEach(param => {
181
- let propJson = param[1];
182
- if (typeof propJson !== "object" || propJson === null || Array.isArray(propJson)) {
183
- return;
184
- }
185
- let match = propJson["anyOf"];
186
- let directItems;
187
- let exit = 0;
188
- if (Array.isArray(match)) {
189
- directItems = match;
190
- } else {
191
- exit = 1;
192
- }
193
- if (exit === 1) {
194
- let match$1 = propJson["oneOf"];
195
- directItems = Array.isArray(match$1) ? match$1 : undefined;
196
- }
197
- let match$2 = propJson["discriminator"];
198
- let exit$1 = 0;
199
- if (directItems !== undefined && match$2 !== undefined) {
200
- if (typeof match$2 === "object" && match$2 !== null && !Array.isArray(match$2)) {
201
- let match$3 = extractDiscriminatorFromPair(directItems, match$2);
202
- if (match$3 !== undefined) {
203
- result[match$3[0]] = match$3[1];
204
- return;
205
- } else {
206
- return;
207
- }
208
- }
209
- exit$1 = 1;
210
- } else {
211
- exit$1 = 1;
212
- }
213
- if (exit$1 === 1) {
214
- let match$4 = propJson["items"];
215
- if (match$4 === undefined) {
216
- return;
217
- }
218
- if (typeof match$4 !== "object" || match$4 === null || Array.isArray(match$4)) {
219
- return;
220
- }
221
- let match$5 = match$4["anyOf"];
222
- let nestedItems;
223
- let exit$2 = 0;
224
- if (Array.isArray(match$5)) {
225
- nestedItems = match$5;
226
- } else {
227
- exit$2 = 2;
228
- }
229
- if (exit$2 === 2) {
230
- let match$6 = match$4["oneOf"];
231
- nestedItems = Array.isArray(match$6) ? match$6 : undefined;
232
- }
233
- let match$7 = match$4["discriminator"];
234
- if (nestedItems === undefined) {
235
- return;
236
- }
237
- if (match$7 === undefined) {
238
- return;
239
- }
240
- if (typeof match$7 !== "object" || match$7 === null || Array.isArray(match$7)) {
241
- return;
242
- }
243
- let match$8 = extractDiscriminatorFromPair(nestedItems, match$7);
244
- if (match$8 !== undefined) {
245
- result[match$8[0]] = match$8[1];
246
- return;
247
- } else {
248
- return;
249
- }
250
- }
251
- });
252
- }
253
- }
254
- return result;
255
- }
256
-
257
- function extractDiscriminatorPropertyName(schemaJson) {
258
- if (typeof schemaJson !== "object" || schemaJson === null || Array.isArray(schemaJson)) {
259
- return;
260
- }
261
- let match = schemaJson["discriminator"];
262
- if (match === undefined) {
263
- return;
264
- }
265
- if (typeof match !== "object" || match === null || Array.isArray(match)) {
266
- return;
267
- }
268
- let match$1 = match["propertyName"];
269
- if (typeof match$1 === "string") {
270
- return match$1;
271
- }
272
- }
273
-
274
- function extractDiscriminatorTag(schemaJson) {
275
- if (typeof schemaJson !== "object" || schemaJson === null || Array.isArray(schemaJson)) {
276
- return;
277
- }
278
- let match = schemaJson["properties"];
279
- if (match === undefined) {
280
- return;
281
- }
282
- if (typeof match !== "object" || match === null || Array.isArray(match)) {
283
- return;
284
- }
285
- let match$1 = match["_tag"];
286
- if (match$1 === undefined) {
287
- return;
288
- }
289
- if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
290
- return;
291
- }
292
- let match$2 = match$1["const"];
293
- if (typeof match$2 === "string") {
294
- return match$2;
295
- }
296
- }
297
-
298
- function parseComponentSchemas(componentsJson) {
299
- if (typeof componentsJson === "object" && componentsJson !== null && !Array.isArray(componentsJson)) {
300
- let match = componentsJson["schemas"];
301
- if (match === undefined) {
302
- return {
303
- TAG: "Ok",
304
- _0: []
305
- };
306
- }
307
- let exit = 0;
308
- if (typeof match === "object" && match !== null && !Array.isArray(match)) {
309
- let entries = Object.entries(match);
310
- let results = entries.map(param => {
311
- let schemaJson = param[1];
312
- let discriminatorTag = extractDiscriminatorTag(schemaJson);
313
- let discriminatorPropertyName = extractDiscriminatorPropertyName(schemaJson);
314
- let fieldDiscs = extractFieldDiscriminators(schemaJson);
315
- let fieldDiscriminators = Object.entries(fieldDiscs).length > 0 ? fieldDiscs : undefined;
316
- let schemaType = Schema.parse(schemaJson);
317
- if (schemaType.TAG === "Ok") {
318
- return {
319
- TAG: "Ok",
320
- _0: {
321
- name: param[0],
322
- schema: schemaType._0,
323
- discriminatorTag: discriminatorTag,
324
- discriminatorPropertyName: discriminatorPropertyName,
325
- fieldDiscriminators: fieldDiscriminators
326
- }
327
- };
328
- } else {
329
- return {
330
- TAG: "Error",
331
- _0: schemaType._0
332
- };
333
- }
334
- });
335
- let errors = Core__Array.filterMap(results, r => {
336
- if (r.TAG === "Ok") {
337
- return;
338
- } else {
339
- return r._0;
340
- }
341
- }).flat();
342
- if (errors.length > 0) {
343
- return {
344
- TAG: "Error",
345
- _0: errors
346
- };
347
- }
348
- let schemas = Core__Array.filterMap(results, r => {
349
- if (r.TAG === "Ok") {
350
- return r._0;
351
- }
352
- });
353
- return {
354
- TAG: "Ok",
355
- _0: schemas
356
- };
357
- }
358
- exit = 2;
359
- if (exit === 2) {
360
- return {
361
- TAG: "Error",
362
- _0: [Errors.makeError({
363
- TAG: "InvalidJson",
364
- _0: "schemas must be an object"
365
- }, undefined, undefined, undefined)]
366
- };
367
- }
368
- }
369
- return {
370
- TAG: "Error",
371
- _0: [Errors.makeError({
372
- TAG: "InvalidJson",
373
- _0: "components must be an object"
374
- }, undefined, undefined, undefined)]
375
- };
376
- }
377
-
378
- function buildParamsObjectJson(params) {
379
- let properties = {};
380
- let required = [];
381
- params.forEach(param => {
382
- if (typeof param !== "object" || param === null || Array.isArray(param)) {
383
- return;
384
- }
385
- let match = param["in"];
386
- let location = typeof match === "string" ? match : undefined;
387
- let isQueryOrPath;
388
- if (location !== undefined) {
389
- switch (location) {
390
- case "path" :
391
- case "query" :
392
- isQueryOrPath = true;
393
- break;
394
- default:
395
- isQueryOrPath = false;
396
- }
397
- } else {
398
- isQueryOrPath = false;
399
- }
400
- if (!isQueryOrPath) {
401
- return;
402
- }
403
- let match$1 = param["name"];
404
- let match$2 = param["schema"];
405
- if (match$1 === undefined) {
406
- return;
407
- }
408
- if (typeof match$1 !== "string") {
409
- return;
410
- }
411
- if (match$2 === undefined) {
412
- return;
413
- }
414
- let cleanSchema;
415
- cleanSchema = typeof match$2 === "object" && match$2 !== null && !Array.isArray(match$2) ? Object.fromEntries(Object.entries(match$2).filter(param => param[0] !== "default")) : match$2;
416
- properties[match$1] = cleanSchema;
417
- let match$3 = param["required"];
418
- let isRequired = match$3 === true;
419
- let pathRequired = location === "path";
420
- if (isRequired || pathRequired) {
421
- required.push(match$1);
422
- return;
423
- }
424
- });
425
- if (Object.entries(properties).length === 0) {
426
- return;
427
- }
428
- let obj = {};
429
- obj["type"] = "object";
430
- obj["properties"] = properties;
431
- obj["required"] = required;
432
- return obj;
433
- }
434
-
435
- function parsePathParameters(pathsJson) {
436
- if (typeof pathsJson !== "object" || pathsJson === null || Array.isArray(pathsJson)) {
437
- return {
438
- TAG: "Ok",
439
- _0: []
440
- };
441
- }
442
- let results = Object.entries(pathsJson).flatMap(param => {
443
- let methodsJson = param[1];
444
- let path = param[0];
445
- if (typeof methodsJson === "object" && methodsJson !== null && !Array.isArray(methodsJson)) {
446
- return Core__Array.filterMap(Object.entries(methodsJson), param => {
447
- let opJson = param[1];
448
- let method = param[0];
449
- let httpMethods = [
450
- "get",
451
- "post",
452
- "put",
453
- "patch",
454
- "delete"
455
- ];
456
- if (!httpMethods.includes(method)) {
457
- return;
458
- }
459
- if (typeof opJson !== "object" || opJson === null || Array.isArray(opJson)) {
460
- return;
461
- }
462
- let match = opJson["parameters"];
463
- if (match === undefined) {
464
- return;
465
- }
466
- if (!Array.isArray(match)) {
467
- return;
468
- }
469
- let objJson = buildParamsObjectJson(match);
470
- if (objJson === undefined) {
471
- return;
472
- }
473
- let name = ucFirst(method) + pathToName(path) + "Params";
474
- let schemaType = Schema.parse(objJson);
475
- if (schemaType.TAG === "Ok") {
476
- return {
477
- TAG: "Ok",
478
- _0: {
479
- name: name,
480
- schema: schemaType._0,
481
- discriminatorTag: undefined,
482
- discriminatorPropertyName: undefined,
483
- fieldDiscriminators: undefined
484
- }
485
- };
486
- } else {
487
- return {
488
- TAG: "Error",
489
- _0: schemaType._0
490
- };
491
- }
492
- });
493
- } else {
494
- return [];
495
- }
496
- });
497
- let errors = Core__Array.filterMap(results, r => {
498
- if (r.TAG === "Ok") {
499
- return;
500
- } else {
501
- return r._0;
502
- }
503
- }).flat();
504
- if (errors.length > 0) {
505
- return {
506
- TAG: "Error",
507
- _0: errors
508
- };
509
- }
510
- let schemas = Core__Array.filterMap(results, r => {
511
- if (r.TAG === "Ok") {
512
- return r._0;
513
- }
514
- });
515
- return {
516
- TAG: "Ok",
517
- _0: schemas
518
- };
519
- }
520
-
521
- function extractAllDiscriminatorMappings(json) {
522
- let result = {};
523
- let walk = j => {
524
- if (Array.isArray(j)) {
525
- j.forEach(walk);
526
- return;
527
- }
528
- switch (typeof j) {
529
- case "object" :
530
- let match = j["oneOf"];
531
- let match$1 = j["discriminator"];
532
- if (Array.isArray(match) && typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) {
533
- let match$2 = match$1["mapping"];
534
- if (typeof match$2 === "object" && match$2 !== null && !Array.isArray(match$2)) {
535
- Object.entries(match$2).forEach(param => {
536
- let refValue = param[1];
537
- if (typeof refValue !== "string") {
538
- return;
539
- }
540
- let parts = refValue.split("/");
541
- let schemaName = parts[parts.length - 1 | 0];
542
- if (schemaName !== undefined) {
543
- result[schemaName] = param[0];
544
- return;
545
- }
546
- });
547
- }
548
- }
549
- Object.entries(j).forEach(param => walk(param[1]));
550
- return;
551
- default:
552
- return;
553
- }
554
- };
555
- walk(json);
556
- return result;
557
- }
558
-
559
- function rewriteVariantTagsInType(schema, tagByRef) {
560
- if (typeof schema !== "object") {
561
- return schema;
562
- }
563
- switch (schema._tag) {
564
- case "Optional" :
565
- return {
566
- _tag: "Optional",
567
- _0: rewriteVariantTagsInType(schema._0, tagByRef)
568
- };
569
- case "Nullable" :
570
- return {
571
- _tag: "Nullable",
572
- _0: rewriteVariantTagsInType(schema._0, tagByRef)
573
- };
574
- case "Object" :
575
- return {
576
- _tag: "Object",
577
- _0: schema._0.map(f => ({
578
- name: f.name,
579
- type: rewriteVariantTagsInType(f.type, tagByRef),
580
- required: f.required
581
- }))
582
- };
583
- case "Array" :
584
- return {
585
- _tag: "Array",
586
- _0: rewriteVariantTagsInType(schema._0, tagByRef)
587
- };
588
- case "PolyVariant" :
589
- let newCases = schema._0.map(c => {
590
- let refName = c.payload;
591
- if (typeof refName !== "object") {
592
- return {
593
- _tag: c._tag,
594
- payload: rewriteVariantTagsInType(refName, tagByRef)
595
- };
596
- }
597
- if (refName._tag !== "Ref") {
598
- return {
599
- _tag: c._tag,
600
- payload: rewriteVariantTagsInType(refName, tagByRef)
601
- };
602
- }
603
- let actualTag = tagByRef[refName._0];
604
- if (actualTag !== undefined && actualTag !== c._tag) {
605
- return {
606
- _tag: actualTag,
607
- payload: c.payload
608
- };
609
- } else {
610
- return c;
611
- }
612
- });
613
- return {
614
- _tag: "PolyVariant",
615
- _0: newCases
616
- };
617
- case "Dict" :
618
- return {
619
- _tag: "Dict",
620
- _0: rewriteVariantTagsInType(schema._0, tagByRef)
621
- };
622
- case "Union" :
623
- return {
624
- _tag: "Union",
625
- _0: schema._0.map(t => rewriteVariantTagsInType(t, tagByRef))
626
- };
627
- default:
628
- return schema;
629
- }
630
- }
631
-
632
- function resolveRefTagsInPolyVariants(schemas, mappingByRef) {
633
- let tagByRef = {};
634
- schemas.forEach(s => {
635
- let tag = s.discriminatorTag;
636
- if (tag !== undefined) {
637
- tagByRef[s.name] = tag;
638
- return;
639
- }
640
- });
641
- Object.entries(mappingByRef).forEach(param => {
642
- tagByRef[param[0]] = param[1];
643
- });
644
- return schemas.map(s => ({
645
- name: s.name,
646
- schema: rewriteVariantTagsInType(s.schema, tagByRef),
647
- discriminatorTag: s.discriminatorTag,
648
- discriminatorPropertyName: s.discriminatorPropertyName,
649
- fieldDiscriminators: s.fieldDiscriminators
650
- }));
651
- }
652
-
653
- function parseDocument(json) {
654
- if (typeof json === "object" && json !== null && !Array.isArray(json)) {
655
- let componentsJson = json["components"];
656
- let componentSchemas = componentsJson !== undefined ? parseComponentSchemas(componentsJson) : ({
657
- TAG: "Ok",
658
- _0: []
659
- });
660
- let pathsJson = json["paths"];
661
- let pathSchemas = pathsJson !== undefined ? parsePathResponses(pathsJson) : ({
662
- TAG: "Ok",
663
- _0: []
664
- });
665
- let pathsJson$1 = json["paths"];
666
- let paramSchemas = pathsJson$1 !== undefined ? parsePathParameters(pathsJson$1) : ({
667
- TAG: "Ok",
668
- _0: []
669
- });
670
- let mappingByRef = extractAllDiscriminatorMappings(json);
671
- let exit = 0;
672
- if (componentSchemas.TAG === "Ok" && pathSchemas.TAG === "Ok") {
673
- if (paramSchemas.TAG === "Ok") {
674
- return {
675
- TAG: "Ok",
676
- _0: resolveRefTagsInPolyVariants(componentSchemas._0.concat(pathSchemas._0).concat(paramSchemas._0), mappingByRef)
677
- };
678
- }
679
- exit = 2;
680
- } else {
681
- exit = 2;
682
- }
683
- if (exit === 2) {
684
- let errs = Core__Array.filterMap([
685
- componentSchemas,
686
- pathSchemas,
687
- paramSchemas
688
- ], r => {
689
- if (r.TAG === "Ok") {
690
- return;
691
- } else {
692
- return r._0;
693
- }
694
- }).flat();
695
- return {
696
- TAG: "Error",
697
- _0: errs
698
- };
699
- }
700
- }
701
- return {
702
- TAG: "Error",
703
- _0: [Errors.makeError({
704
- TAG: "InvalidJson",
705
- _0: "document must be an object"
706
- }, undefined, undefined, undefined)]
707
- };
708
- }
709
-
710
- export {
711
- pathToName,
712
- ucFirst,
713
- parsePathResponses,
714
- extractDiscriminatorFromPair,
715
- extractFieldDiscriminators,
716
- extractDiscriminatorPropertyName,
717
- extractDiscriminatorTag,
718
- parseComponentSchemas,
719
- buildParamsObjectJson,
720
- parsePathParameters,
721
- extractAllDiscriminatorMappings,
722
- rewriteVariantTagsInType,
723
- resolveRefTagsInPolyVariants,
724
- parseDocument,
725
- }
726
- /* No side effect */