@sebspark/openapi-typegen 1.3.0 → 1.4.1
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 +19 -42
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -187,46 +187,16 @@ var generateProperty = (property) => {
|
|
|
187
187
|
var preamble = (type) => type.name ? `${document(type)}export type ${typeName(type.name)} = ` : "";
|
|
188
188
|
var rxProperVariable = /^[a-zA-Z_<>$][a-zA-Z0-9_<>$]*$/;
|
|
189
189
|
var typeName = (name) => {
|
|
190
|
-
if (rxProperVariable.test(name))
|
|
191
|
-
return name;
|
|
190
|
+
if (rxProperVariable.test(name.replace(/\./g, "_")))
|
|
191
|
+
return name.replace(/\./g, "_");
|
|
192
192
|
return (0, import_change_case.pascalCase)(name);
|
|
193
193
|
};
|
|
194
194
|
var propertyName = (name) => {
|
|
195
|
-
if (rxProperVariable.test(name))
|
|
196
|
-
return name;
|
|
197
|
-
return `'${name}'`;
|
|
198
|
-
};
|
|
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;
|
|
195
|
+
if (rxProperVariable.test(name.replace(/\./g, "_")))
|
|
196
|
+
return name.replace(/\./g, "_");
|
|
197
|
+
return `'${name.replace(/\./g, "_")}'`;
|
|
229
198
|
};
|
|
199
|
+
var extensions = (type) => (type.allOf || []).map(generateType).concat("").join(AND) + (type.oneOf || []).map(generateType).concat("").join(OR);
|
|
230
200
|
var generatePrimitive = (parsed) => `${preamble(parsed)}${parsed.type}`;
|
|
231
201
|
var generateCustom = (parsed) => `${preamble(parsed)}${typeName(parsed.type)}`;
|
|
232
202
|
var generateObject = (parsed) => {
|
|
@@ -234,6 +204,18 @@ var generateObject = (parsed) => {
|
|
|
234
204
|
lines.push(`${preamble(parsed)}${extensions(parsed)}{`);
|
|
235
205
|
lines.push(...parsed.properties.map(generateProperty));
|
|
236
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("}");
|
|
237
219
|
return lines.join("\n");
|
|
238
220
|
};
|
|
239
221
|
var generateArray = (parsed) => {
|
|
@@ -904,7 +886,7 @@ var readDocs = async (input) => {
|
|
|
904
886
|
}
|
|
905
887
|
readFiles.push({
|
|
906
888
|
doc,
|
|
907
|
-
name
|
|
889
|
+
name
|
|
908
890
|
});
|
|
909
891
|
}
|
|
910
892
|
return readFiles;
|
|
@@ -931,11 +913,6 @@ var saveDocs = async (output, docs) => {
|
|
|
931
913
|
await (0, import_promises.writeFile)(path, doc.ts, "utf8");
|
|
932
914
|
}
|
|
933
915
|
};
|
|
934
|
-
var formatName = (name) => {
|
|
935
|
-
if (name[0].toUpperCase() === name[0])
|
|
936
|
-
return name;
|
|
937
|
-
return (0, import_change_case2.pascalCase)(name);
|
|
938
|
-
};
|
|
939
916
|
// Annotate the CommonJS export names for ESM import in node:
|
|
940
917
|
0 && (module.exports = {
|
|
941
918
|
generate,
|