@sebspark/openapi-typegen 1.2.1 → 1.4.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/dist/index.js +40 -11
- 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.
|
|
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(/
|
|
181
|
+
return type.replace(/ [&|] \{\s*\}/g, "");
|
|
182
182
|
};
|
|
183
183
|
var generateProperty = (property) => {
|
|
184
184
|
const types = property.type.map(generateType);
|
|
@@ -196,7 +196,7 @@ var propertyName = (name) => {
|
|
|
196
196
|
return name;
|
|
197
197
|
return `'${name}'`;
|
|
198
198
|
};
|
|
199
|
-
var extensions = (type) => type.
|
|
199
|
+
var extensions = (type) => (type.allOf || []).map(generateType).concat("").join(AND) + (type.oneOf || []).map(generateType).concat("").join(OR);
|
|
200
200
|
var generatePrimitive = (parsed) => `${preamble(parsed)}${parsed.type}`;
|
|
201
201
|
var generateCustom = (parsed) => `${preamble(parsed)}${typeName(parsed.type)}`;
|
|
202
202
|
var generateObject = (parsed) => {
|
|
@@ -204,6 +204,18 @@ var generateObject = (parsed) => {
|
|
|
204
204
|
lines.push(`${preamble(parsed)}${extensions(parsed)}{`);
|
|
205
205
|
lines.push(...parsed.properties.map(generateProperty));
|
|
206
206
|
lines.push("}");
|
|
207
|
+
if (parsed.discriminator && parsed.name) {
|
|
208
|
+
lines.push(generateDiscriminator(parsed.discriminator, parsed.name));
|
|
209
|
+
}
|
|
210
|
+
return lines.join("\n");
|
|
211
|
+
};
|
|
212
|
+
var generateDiscriminator = (discriminator, name) => {
|
|
213
|
+
const lines = [""];
|
|
214
|
+
lines.push(`export type ${name}Discriminator = {`);
|
|
215
|
+
for (const [key, type] of Object.entries(discriminator.mapping)) {
|
|
216
|
+
lines.push(`${key}: ${type.type}`);
|
|
217
|
+
}
|
|
218
|
+
lines.push("}");
|
|
207
219
|
return lines.join("\n");
|
|
208
220
|
};
|
|
209
221
|
var generateArray = (parsed) => {
|
|
@@ -416,7 +428,6 @@ var generateComponents = (components) => {
|
|
|
416
428
|
tokens.push(
|
|
417
429
|
generateType({
|
|
418
430
|
type: "object",
|
|
419
|
-
extends: [],
|
|
420
431
|
name: param2.name,
|
|
421
432
|
properties: [
|
|
422
433
|
{
|
|
@@ -489,11 +500,11 @@ var parseSchema = (name, schemaOrRef) => {
|
|
|
489
500
|
}
|
|
490
501
|
};
|
|
491
502
|
var parseObjectSchema = (name, schema) => {
|
|
503
|
+
var _a;
|
|
492
504
|
const type = {
|
|
493
505
|
name,
|
|
494
506
|
type: "object",
|
|
495
507
|
properties: [],
|
|
496
|
-
extends: [],
|
|
497
508
|
...parseDocumentation(schema)
|
|
498
509
|
};
|
|
499
510
|
if (schema.properties) {
|
|
@@ -502,7 +513,23 @@ var parseObjectSchema = (name, schema) => {
|
|
|
502
513
|
);
|
|
503
514
|
}
|
|
504
515
|
if (schema.allOf) {
|
|
505
|
-
type.
|
|
516
|
+
type.allOf = schema.allOf.flatMap(parsePropertyType);
|
|
517
|
+
}
|
|
518
|
+
if (schema.oneOf) {
|
|
519
|
+
type.oneOf = schema.oneOf.flatMap(parsePropertyType);
|
|
520
|
+
}
|
|
521
|
+
if (schema.anyOf) {
|
|
522
|
+
type.oneOf = schema.anyOf.flatMap(parsePropertyType);
|
|
523
|
+
}
|
|
524
|
+
if ((_a = schema.discriminator) == null ? void 0 : _a.mapping) {
|
|
525
|
+
const mapping = {};
|
|
526
|
+
for (const [prop, ref] of Object.entries(schema.discriminator.mapping)) {
|
|
527
|
+
mapping[prop] = { type: parseRef(ref) };
|
|
528
|
+
}
|
|
529
|
+
type.discriminator = {
|
|
530
|
+
propertyName: schema.discriminator.propertyName,
|
|
531
|
+
mapping
|
|
532
|
+
};
|
|
506
533
|
}
|
|
507
534
|
return type;
|
|
508
535
|
};
|
|
@@ -617,7 +644,6 @@ var parseArgs = (path, components) => {
|
|
|
617
644
|
};
|
|
618
645
|
var createArgs = (initializer = {}) => ({
|
|
619
646
|
type: "object",
|
|
620
|
-
extends: [],
|
|
621
647
|
properties: [],
|
|
622
648
|
optional: true,
|
|
623
649
|
...initializer
|
|
@@ -633,7 +659,9 @@ var parseParameters2 = (parameters = [], components = {}) => {
|
|
|
633
659
|
const param2 = findRef(components, ref);
|
|
634
660
|
const arg = args[param2.in] || createArgs({ ...parseDocumentation(param2) });
|
|
635
661
|
arg.optional = arg.optional && !param2.required;
|
|
636
|
-
arg.
|
|
662
|
+
if (!arg.allOf)
|
|
663
|
+
arg.allOf = [];
|
|
664
|
+
arg.allOf.push({ type: parseRef(ref) });
|
|
637
665
|
args[param2.in] = arg;
|
|
638
666
|
break;
|
|
639
667
|
}
|
|
@@ -667,6 +695,7 @@ var parseParameters2 = (parameters = [], components = {}) => {
|
|
|
667
695
|
return args;
|
|
668
696
|
};
|
|
669
697
|
var parseRequestBody = (requestBody, components = {}) => {
|
|
698
|
+
var _a, _b;
|
|
670
699
|
const args = {};
|
|
671
700
|
if (!requestBody)
|
|
672
701
|
return args;
|
|
@@ -675,7 +704,7 @@ var parseRequestBody = (requestBody, components = {}) => {
|
|
|
675
704
|
const refBody = findRef(components, ref);
|
|
676
705
|
args.body = createArgs({
|
|
677
706
|
optional: !refBody.required,
|
|
678
|
-
|
|
707
|
+
allOf: [{ type: parseRef(ref) }]
|
|
679
708
|
});
|
|
680
709
|
} else {
|
|
681
710
|
const body = requestBody;
|
|
@@ -692,13 +721,13 @@ var parseRequestBody = (requestBody, components = {}) => {
|
|
|
692
721
|
} else if (parsed.type) {
|
|
693
722
|
args.body = createArgs({
|
|
694
723
|
optional: !body.required,
|
|
695
|
-
|
|
724
|
+
allOf: [parsed],
|
|
696
725
|
...parseDocumentation(body)
|
|
697
726
|
});
|
|
698
727
|
}
|
|
699
728
|
}
|
|
700
729
|
}
|
|
701
|
-
if (bodyArgs.
|
|
730
|
+
if (((_a = bodyArgs.allOf) == null ? void 0 : _a.length) || ((_b = bodyArgs.oneOf) == null ? void 0 : _b.length) || bodyArgs.properties.length) {
|
|
702
731
|
args.body = bodyArgs;
|
|
703
732
|
}
|
|
704
733
|
}
|