@sebspark/openapi-typegen 1.2.1 → 1.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.
Files changed (2) hide show
  1. package/dist/index.js +58 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -100,7 +100,7 @@ var requestArgs = (args, name, title) => {
100
100
  if (!args)
101
101
  return [];
102
102
  const tokens = [];
103
- const type = args.extends.map((e) => e.type).join(AND) || "Object";
103
+ const type = (args.allOf || []).map((e) => e.type).join(AND) || "Object";
104
104
  tokens.push(
105
105
  param(
106
106
  buildPath("args", name),
@@ -178,7 +178,7 @@ var generateType = (parsed) => {
178
178
  type = generateCustom(parsed);
179
179
  }
180
180
  }
181
- return type.replace(/ & \{\s*\}/g, "");
181
+ return type.replace(/ [&|] \{\s*\}/g, "");
182
182
  };
183
183
  var generateProperty = (property) => {
184
184
  const types = property.type.map(generateType);
@@ -196,7 +196,37 @@ var propertyName = (name) => {
196
196
  return name;
197
197
  return `'${name}'`;
198
198
  };
199
- var extensions = (type) => type.extends.map(generateType).concat("").join(AND);
199
+ var extensions = (type) => (type.allOf || []).map(generateType).concat("").join(AND) + parseOptional(type.oneOf, type.discriminator);
200
+ var parseOptional = (optional, discriminator) => {
201
+ const tokens = [];
202
+ const map = reverseDiscriminator(discriminator);
203
+ for (const type of optional || []) {
204
+ if (type.type === "object")
205
+ tokens.push(generateType(type));
206
+ else {
207
+ const custom = type;
208
+ if (!map[custom.type])
209
+ tokens.push(generateType(custom));
210
+ else {
211
+ tokens.push(
212
+ `(${generateType(custom)} & { ${discriminator == null ? void 0 : discriminator.propertyName}: '${map[custom.type]}' })`
213
+ );
214
+ }
215
+ }
216
+ }
217
+ if (tokens.length)
218
+ tokens.push("");
219
+ return tokens.join(OR);
220
+ };
221
+ var reverseDiscriminator = (discriminator) => {
222
+ const reverse = {};
223
+ if (!discriminator)
224
+ return reverse;
225
+ for (const [val, { type }] of Object.entries(discriminator.mapping)) {
226
+ reverse[type] = val;
227
+ }
228
+ return reverse;
229
+ };
200
230
  var generatePrimitive = (parsed) => `${preamble(parsed)}${parsed.type}`;
201
231
  var generateCustom = (parsed) => `${preamble(parsed)}${typeName(parsed.type)}`;
202
232
  var generateObject = (parsed) => {
@@ -416,7 +446,6 @@ var generateComponents = (components) => {
416
446
  tokens.push(
417
447
  generateType({
418
448
  type: "object",
419
- extends: [],
420
449
  name: param2.name,
421
450
  properties: [
422
451
  {
@@ -489,11 +518,11 @@ var parseSchema = (name, schemaOrRef) => {
489
518
  }
490
519
  };
491
520
  var parseObjectSchema = (name, schema) => {
521
+ var _a;
492
522
  const type = {
493
523
  name,
494
524
  type: "object",
495
525
  properties: [],
496
- extends: [],
497
526
  ...parseDocumentation(schema)
498
527
  };
499
528
  if (schema.properties) {
@@ -502,7 +531,23 @@ var parseObjectSchema = (name, schema) => {
502
531
  );
503
532
  }
504
533
  if (schema.allOf) {
505
- type.extends = schema.allOf.flatMap(parsePropertyType);
534
+ type.allOf = schema.allOf.flatMap(parsePropertyType);
535
+ }
536
+ if (schema.oneOf) {
537
+ type.oneOf = schema.oneOf.flatMap(parsePropertyType);
538
+ }
539
+ if (schema.anyOf) {
540
+ type.oneOf = schema.anyOf.flatMap(parsePropertyType);
541
+ }
542
+ if ((_a = schema.discriminator) == null ? void 0 : _a.mapping) {
543
+ const mapping = {};
544
+ for (const [prop, ref] of Object.entries(schema.discriminator.mapping)) {
545
+ mapping[prop] = { type: parseRef(ref) };
546
+ }
547
+ type.discriminator = {
548
+ propertyName: schema.discriminator.propertyName,
549
+ mapping
550
+ };
506
551
  }
507
552
  return type;
508
553
  };
@@ -617,7 +662,6 @@ var parseArgs = (path, components) => {
617
662
  };
618
663
  var createArgs = (initializer = {}) => ({
619
664
  type: "object",
620
- extends: [],
621
665
  properties: [],
622
666
  optional: true,
623
667
  ...initializer
@@ -633,7 +677,9 @@ var parseParameters2 = (parameters = [], components = {}) => {
633
677
  const param2 = findRef(components, ref);
634
678
  const arg = args[param2.in] || createArgs({ ...parseDocumentation(param2) });
635
679
  arg.optional = arg.optional && !param2.required;
636
- arg.extends.push({ type: parseRef(ref) });
680
+ if (!arg.allOf)
681
+ arg.allOf = [];
682
+ arg.allOf.push({ type: parseRef(ref) });
637
683
  args[param2.in] = arg;
638
684
  break;
639
685
  }
@@ -667,6 +713,7 @@ var parseParameters2 = (parameters = [], components = {}) => {
667
713
  return args;
668
714
  };
669
715
  var parseRequestBody = (requestBody, components = {}) => {
716
+ var _a, _b;
670
717
  const args = {};
671
718
  if (!requestBody)
672
719
  return args;
@@ -675,7 +722,7 @@ var parseRequestBody = (requestBody, components = {}) => {
675
722
  const refBody = findRef(components, ref);
676
723
  args.body = createArgs({
677
724
  optional: !refBody.required,
678
- extends: [{ type: parseRef(ref) }]
725
+ allOf: [{ type: parseRef(ref) }]
679
726
  });
680
727
  } else {
681
728
  const body = requestBody;
@@ -692,13 +739,13 @@ var parseRequestBody = (requestBody, components = {}) => {
692
739
  } else if (parsed.type) {
693
740
  args.body = createArgs({
694
741
  optional: !body.required,
695
- extends: [parsed],
742
+ allOf: [parsed],
696
743
  ...parseDocumentation(body)
697
744
  });
698
745
  }
699
746
  }
700
747
  }
701
- if (bodyArgs.extends.length || bodyArgs.properties.length) {
748
+ if (((_a = bodyArgs.allOf) == null ? void 0 : _a.length) || ((_b = bodyArgs.oneOf) == null ? void 0 : _b.length) || bodyArgs.properties.length) {
702
749
  args.body = bodyArgs;
703
750
  }
704
751
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/openapi-typegen",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",