osury 0.26.0 → 0.27.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "osury",
3
3
  "type": "module",
4
4
  "description": "Generate ReScript types with Sury schemas from OpenAPI specifications",
5
- "version": "0.26.0",
5
+ "version": "0.27.0",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "osury": "bin/osury.mjs"
@@ -375,6 +375,147 @@ function parseComponentSchemas(componentsJson) {
375
375
  };
376
376
  }
377
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
+ properties[match$1] = match$2;
415
+ let match$3 = param["required"];
416
+ let isRequired = match$3 === true;
417
+ let pathRequired = location === "path";
418
+ if (isRequired || pathRequired) {
419
+ required.push(match$1);
420
+ return;
421
+ }
422
+ });
423
+ if (Object.entries(properties).length === 0) {
424
+ return;
425
+ }
426
+ let obj = {};
427
+ obj["type"] = "object";
428
+ obj["properties"] = properties;
429
+ obj["required"] = required;
430
+ return obj;
431
+ }
432
+
433
+ function parsePathParameters(pathsJson) {
434
+ if (typeof pathsJson !== "object" || pathsJson === null || Array.isArray(pathsJson)) {
435
+ return {
436
+ TAG: "Ok",
437
+ _0: []
438
+ };
439
+ }
440
+ let results = Object.entries(pathsJson).flatMap(param => {
441
+ let methodsJson = param[1];
442
+ let path = param[0];
443
+ if (typeof methodsJson === "object" && methodsJson !== null && !Array.isArray(methodsJson)) {
444
+ return Core__Array.filterMap(Object.entries(methodsJson), param => {
445
+ let opJson = param[1];
446
+ let method = param[0];
447
+ let httpMethods = [
448
+ "get",
449
+ "post",
450
+ "put",
451
+ "patch",
452
+ "delete"
453
+ ];
454
+ if (!httpMethods.includes(method)) {
455
+ return;
456
+ }
457
+ if (typeof opJson !== "object" || opJson === null || Array.isArray(opJson)) {
458
+ return;
459
+ }
460
+ let match = opJson["parameters"];
461
+ if (match === undefined) {
462
+ return;
463
+ }
464
+ if (!Array.isArray(match)) {
465
+ return;
466
+ }
467
+ let objJson = buildParamsObjectJson(match);
468
+ if (objJson === undefined) {
469
+ return;
470
+ }
471
+ let name = ucFirst(method) + pathToName(path) + "Params";
472
+ let schemaType = Schema.parse(objJson);
473
+ if (schemaType.TAG === "Ok") {
474
+ return {
475
+ TAG: "Ok",
476
+ _0: {
477
+ name: name,
478
+ schema: schemaType._0,
479
+ discriminatorTag: undefined,
480
+ discriminatorPropertyName: undefined,
481
+ fieldDiscriminators: undefined
482
+ }
483
+ };
484
+ } else {
485
+ return {
486
+ TAG: "Error",
487
+ _0: schemaType._0
488
+ };
489
+ }
490
+ });
491
+ } else {
492
+ return [];
493
+ }
494
+ });
495
+ let errors = Core__Array.filterMap(results, r => {
496
+ if (r.TAG === "Ok") {
497
+ return;
498
+ } else {
499
+ return r._0;
500
+ }
501
+ }).flat();
502
+ if (errors.length > 0) {
503
+ return {
504
+ TAG: "Error",
505
+ _0: errors
506
+ };
507
+ }
508
+ let schemas = Core__Array.filterMap(results, r => {
509
+ if (r.TAG === "Ok") {
510
+ return r._0;
511
+ }
512
+ });
513
+ return {
514
+ TAG: "Ok",
515
+ _0: schemas
516
+ };
517
+ }
518
+
378
519
  function parseDocument(json) {
379
520
  if (typeof json === "object" && json !== null && !Array.isArray(json)) {
380
521
  let componentsJson = json["components"];
@@ -387,29 +528,38 @@ function parseDocument(json) {
387
528
  TAG: "Ok",
388
529
  _0: []
389
530
  });
390
- if (componentSchemas.TAG === "Ok") {
391
- if (pathSchemas.TAG === "Ok") {
531
+ let pathsJson$1 = json["paths"];
532
+ let paramSchemas = pathsJson$1 !== undefined ? parsePathParameters(pathsJson$1) : ({
533
+ TAG: "Ok",
534
+ _0: []
535
+ });
536
+ let exit = 0;
537
+ if (componentSchemas.TAG === "Ok" && pathSchemas.TAG === "Ok") {
538
+ if (paramSchemas.TAG === "Ok") {
392
539
  return {
393
540
  TAG: "Ok",
394
- _0: componentSchemas._0.concat(pathSchemas._0)
395
- };
396
- } else {
397
- return {
398
- TAG: "Error",
399
- _0: pathSchemas._0
541
+ _0: componentSchemas._0.concat(pathSchemas._0).concat(paramSchemas._0)
400
542
  };
401
543
  }
402
- }
403
- let e = componentSchemas._0;
404
- if (pathSchemas.TAG === "Ok") {
405
- return {
406
- TAG: "Error",
407
- _0: e
408
- };
544
+ exit = 2;
409
545
  } else {
546
+ exit = 2;
547
+ }
548
+ if (exit === 2) {
549
+ let errs = Core__Array.filterMap([
550
+ componentSchemas,
551
+ pathSchemas,
552
+ paramSchemas
553
+ ], r => {
554
+ if (r.TAG === "Ok") {
555
+ return;
556
+ } else {
557
+ return r._0;
558
+ }
559
+ }).flat();
410
560
  return {
411
561
  TAG: "Error",
412
- _0: e.concat(pathSchemas._0)
562
+ _0: errs
413
563
  };
414
564
  }
415
565
  }
@@ -431,6 +581,8 @@ export {
431
581
  extractDiscriminatorPropertyName,
432
582
  extractDiscriminatorTag,
433
583
  parseComponentSchemas,
584
+ buildParamsObjectJson,
585
+ parsePathParameters,
434
586
  parseDocument,
435
587
  }
436
588
  /* No side effect */