@sebspark/openapi-typegen 1.4.1 → 1.5.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.d.ts +3 -1
- package/dist/index.js +39 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ import { OpenApiDocument } from '@sebspark/openapi-core';
|
|
|
2
2
|
|
|
3
3
|
declare const generateTypescript: (name: string, doc: OpenApiDocument) => Promise<string>;
|
|
4
4
|
declare const generate: (input: string, output?: string) => Promise<string | undefined>;
|
|
5
|
+
declare const classname: (name: string) => string;
|
|
6
|
+
declare const filename: (name: string) => string;
|
|
5
7
|
|
|
6
|
-
export { generate, generateTypescript };
|
|
8
|
+
export { classname, filename, generate, generateTypescript };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
classname: () => classname,
|
|
34
|
+
filename: () => filename,
|
|
33
35
|
generate: () => generate2,
|
|
34
36
|
generateTypescript: () => generateTypescript
|
|
35
37
|
});
|
|
@@ -187,9 +189,33 @@ var generateProperty = (property) => {
|
|
|
187
189
|
var preamble = (type) => type.name ? `${document(type)}export type ${typeName(type.name)} = ` : "";
|
|
188
190
|
var rxProperVariable = /^[a-zA-Z_<>$][a-zA-Z0-9_<>$]*$/;
|
|
189
191
|
var typeName = (name) => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
192
|
+
const namingConventionRegex = /^([a-z_]\w*)(<([a-z_]\w*(,\s*)?)+>)?$/;
|
|
193
|
+
const hasCapitalLetterRegex = /[A-Z]/;
|
|
194
|
+
if (namingConventionRegex.test(name) && hasCapitalLetterRegex.test(name)) {
|
|
195
|
+
return name;
|
|
196
|
+
}
|
|
197
|
+
if (name.includes("<")) {
|
|
198
|
+
return name.replace(
|
|
199
|
+
/<([^>]+)>/,
|
|
200
|
+
(_match, genericContent) => `<${typeName(genericContent)}>`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
const domainStyleTransformed = name.split(".").map((part, index, array) => {
|
|
204
|
+
if (index === array.length - 1) {
|
|
205
|
+
return (0, import_change_case.pascalCase)(part);
|
|
206
|
+
}
|
|
207
|
+
return part;
|
|
208
|
+
}).join("_");
|
|
209
|
+
const prefixedIfNumberStart = domainStyleTransformed.match(/^\d/) ? `_${domainStyleTransformed}` : domainStyleTransformed;
|
|
210
|
+
const finalName = prefixedIfNumberStart.includes("_") ? prefixedIfNumberStart : (0, import_change_case.pascalCase)(prefixedIfNumberStart);
|
|
211
|
+
if (finalName.includes("_")) {
|
|
212
|
+
const lastUnderscoreIndex = finalName.lastIndexOf("_");
|
|
213
|
+
if (lastUnderscoreIndex !== -1 && lastUnderscoreIndex < finalName.length - 1) {
|
|
214
|
+
return finalName.substring(0, lastUnderscoreIndex + 1) + finalName.charAt(lastUnderscoreIndex + 1).toUpperCase() + finalName.slice(lastUnderscoreIndex + 2);
|
|
215
|
+
}
|
|
216
|
+
return finalName;
|
|
217
|
+
}
|
|
218
|
+
return finalName.charAt(0).toUpperCase() + finalName.slice(1);
|
|
193
219
|
};
|
|
194
220
|
var propertyName = (name) => {
|
|
195
221
|
if (rxProperVariable.test(name.replace(/\./g, "_")))
|
|
@@ -895,7 +921,7 @@ var generateDocs = async (files) => {
|
|
|
895
921
|
const generated = [];
|
|
896
922
|
for (const doc of files) {
|
|
897
923
|
console.log(`Generating ${doc.name}`);
|
|
898
|
-
const ts = await generateTypescript(doc.name, doc.doc);
|
|
924
|
+
const ts = await generateTypescript(classname(doc.name), doc.doc);
|
|
899
925
|
generated.push({
|
|
900
926
|
...doc,
|
|
901
927
|
ts
|
|
@@ -908,13 +934,21 @@ var saveDocs = async (output, docs) => {
|
|
|
908
934
|
const dir = stats.isDirectory() ? output : (0, import_path.parse)(output).dir;
|
|
909
935
|
await (0, import_promises.mkdir)(dir, { recursive: true });
|
|
910
936
|
for (const doc of docs) {
|
|
911
|
-
const path = (0, import_path.resolve)(dir, `${doc.name}.ts`);
|
|
937
|
+
const path = (0, import_path.resolve)(dir, `${filename(doc.name)}.ts`);
|
|
912
938
|
console.log(`Writing ${path}`);
|
|
913
939
|
await (0, import_promises.writeFile)(path, doc.ts, "utf8");
|
|
914
940
|
}
|
|
915
941
|
};
|
|
942
|
+
var classname = (name) => {
|
|
943
|
+
return (0, import_change_case2.pascalCase)(name.replace(/\d+/g, ""));
|
|
944
|
+
};
|
|
945
|
+
var filename = (name) => {
|
|
946
|
+
return name.replace(/\./g, "_");
|
|
947
|
+
};
|
|
916
948
|
// Annotate the CommonJS export names for ESM import in node:
|
|
917
949
|
0 && (module.exports = {
|
|
950
|
+
classname,
|
|
951
|
+
filename,
|
|
918
952
|
generate,
|
|
919
953
|
generateTypescript
|
|
920
954
|
});
|