osury 0.7.1 → 0.8.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.7.1",
5
+ "version": "0.8.0",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "osury": "./bin/osury.mjs"
@@ -281,11 +281,12 @@ function generateInlineRecord(refName, schemasDict) {
281
281
  }
282
282
  }
283
283
 
284
- function generateInlineVariantBody(types, schemasDict) {
284
+ function generateInlineVariantBody(types, schemasDict, tagsDict) {
285
285
  return types.map(t => {
286
286
  if (typeof t === "object" && t._tag === "Ref") {
287
287
  let name = t._0;
288
- let tag = ucFirst(name);
288
+ let tagValue = tagsDict[name];
289
+ let tag = tagValue !== undefined ? ucFirst(tagValue) : ucFirst(name);
289
290
  let inlineRecord = generateInlineRecord(name, schemasDict);
290
291
  return tag + `(` + inlineRecord + `)`;
291
292
  }
@@ -571,13 +572,13 @@ function generateVariantBody(types) {
571
572
  }).join(" | ");
572
573
  }
573
574
 
574
- function generateTypeDefWithSkipSet(namedSchema, _skipSet, schemasDict) {
575
+ function generateTypeDefWithSkipSet(namedSchema, _skipSet, schemasDict, tagsDict) {
575
576
  let typeName = lcFirst(namedSchema.name);
576
577
  let types = namedSchema.schema;
577
578
  if (typeof types === "object" && types._tag === "Union") {
578
579
  let types$1 = types._0;
579
580
  if (isRefOnlyUnion(types$1)) {
580
- let variantBody = generateInlineVariantBody(types$1, schemasDict);
581
+ let variantBody = generateInlineVariantBody(types$1, schemasDict, tagsDict);
581
582
  return `@genType
582
583
  @tag("_tag")
583
584
  @schema
@@ -623,7 +624,8 @@ type ` + typeName + ` = ` + typeBody;
623
624
  function generateModule(schemas) {
624
625
  let extractedUnions = schemas.flatMap(s => extractUnions(s.name, s.schema).map(extracted => ({
625
626
  name: extracted.name,
626
- schema: extracted.schema
627
+ schema: extracted.schema,
628
+ discriminatorTag: undefined
627
629
  })));
628
630
  let seen = {};
629
631
  let uniqueUnions = extractedUnions.filter(u => {
@@ -636,16 +638,23 @@ function generateModule(schemas) {
636
638
  });
637
639
  let modifiedSchemas = schemas.map(s => ({
638
640
  name: s.name,
639
- schema: replaceUnions(s.name, s.schema)
641
+ schema: replaceUnions(s.name, s.schema),
642
+ discriminatorTag: s.discriminatorTag
640
643
  }));
641
644
  let allSchemas = uniqueUnions.concat(modifiedSchemas);
642
645
  let schemasDict = {};
646
+ let tagsDict = {};
643
647
  allSchemas.forEach(s => {
644
648
  schemasDict[s.name] = s.schema;
649
+ let tag = s.discriminatorTag;
650
+ if (tag !== undefined) {
651
+ tagsDict[s.name] = tag;
652
+ return;
653
+ }
645
654
  });
646
655
  let sorted = topologicalSort(allSchemas);
647
656
  let skipSet = {};
648
- let typeDefs = sorted.map(s => generateTypeDefWithSkipSet(s, skipSet, schemasDict)).join("\n\n");
657
+ let typeDefs = sorted.map(s => generateTypeDefWithSkipSet(s, skipSet, schemasDict, tagsDict)).join("\n\n");
649
658
  return "module S = Sury\n\n" + typeDefs;
650
659
  }
651
660
 
@@ -91,7 +91,8 @@ function parsePathResponses(pathsJson) {
91
91
  TAG: "Ok",
92
92
  _0: {
93
93
  name: name,
94
- schema: schemaType._0
94
+ schema: schemaType._0,
95
+ discriminatorTag: undefined
95
96
  }
96
97
  };
97
98
  } else {
@@ -129,6 +130,30 @@ function parsePathResponses(pathsJson) {
129
130
  };
130
131
  }
131
132
 
133
+ function extractDiscriminatorTag(schemaJson) {
134
+ if (typeof schemaJson !== "object" || schemaJson === null || Array.isArray(schemaJson)) {
135
+ return;
136
+ }
137
+ let match = schemaJson["properties"];
138
+ if (match === undefined) {
139
+ return;
140
+ }
141
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
142
+ return;
143
+ }
144
+ let match$1 = match["_tag"];
145
+ if (match$1 === undefined) {
146
+ return;
147
+ }
148
+ if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
149
+ return;
150
+ }
151
+ let match$2 = match$1["const"];
152
+ if (typeof match$2 === "string") {
153
+ return match$2;
154
+ }
155
+ }
156
+
132
157
  function parseComponentSchemas(componentsJson) {
133
158
  if (typeof componentsJson === "object" && componentsJson !== null && !Array.isArray(componentsJson)) {
134
159
  let match = componentsJson["schemas"];
@@ -142,13 +167,16 @@ function parseComponentSchemas(componentsJson) {
142
167
  if (typeof match === "object" && match !== null && !Array.isArray(match)) {
143
168
  let entries = Object.entries(match);
144
169
  let results = entries.map(param => {
145
- let schemaType = Schema.parse(param[1]);
170
+ let schemaJson = param[1];
171
+ let discriminatorTag = extractDiscriminatorTag(schemaJson);
172
+ let schemaType = Schema.parse(schemaJson);
146
173
  if (schemaType.TAG === "Ok") {
147
174
  return {
148
175
  TAG: "Ok",
149
176
  _0: {
150
177
  name: param[0],
151
- schema: schemaType._0
178
+ schema: schemaType._0,
179
+ discriminatorTag: discriminatorTag
152
180
  }
153
181
  };
154
182
  } else {
@@ -252,6 +280,7 @@ export {
252
280
  pathToName,
253
281
  ucFirst,
254
282
  parsePathResponses,
283
+ extractDiscriminatorTag,
255
284
  parseComponentSchemas,
256
285
  parseDocument,
257
286
  }