@sebspark/openapi-typegen 1.4.1 → 1.5.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.d.ts +3 -1
- package/dist/index.js +50 -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
|
});
|
|
@@ -186,10 +188,45 @@ var generateProperty = (property) => {
|
|
|
186
188
|
};
|
|
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_<>$]*$/;
|
|
191
|
+
var isValidName = (name) => {
|
|
192
|
+
const namingConventionRegex = /^([A-Z_]\w*)([a-z_]\w*)(<([a-z_]\w*(,\s*)?)+>)?$/;
|
|
193
|
+
const hasCapitalLetterRegex = /[A-Z]/;
|
|
194
|
+
if (!namingConventionRegex.test(name))
|
|
195
|
+
return false;
|
|
196
|
+
if (!hasCapitalLetterRegex.test(name)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
if (name[0] !== name[0].toUpperCase() && !name.includes("_")) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
};
|
|
189
204
|
var typeName = (name) => {
|
|
190
|
-
if (
|
|
191
|
-
return name
|
|
192
|
-
|
|
205
|
+
if (isValidName(name)) {
|
|
206
|
+
return name;
|
|
207
|
+
}
|
|
208
|
+
if (name.includes("<")) {
|
|
209
|
+
return name.replace(
|
|
210
|
+
/<([^>]+)>/,
|
|
211
|
+
(_match, genericContent) => `<${typeName(genericContent)}>`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const domainStyleTransformed = name.split(".").map((part, index, array) => {
|
|
215
|
+
if (index === array.length - 1) {
|
|
216
|
+
return (0, import_change_case.pascalCase)(part);
|
|
217
|
+
}
|
|
218
|
+
return part;
|
|
219
|
+
}).join("_");
|
|
220
|
+
const prefixedIfNumberStart = domainStyleTransformed.match(/^\d/) ? `_${domainStyleTransformed}` : domainStyleTransformed;
|
|
221
|
+
const finalName = prefixedIfNumberStart.includes("_") ? prefixedIfNumberStart : (0, import_change_case.pascalCase)(prefixedIfNumberStart);
|
|
222
|
+
if (finalName.includes("_")) {
|
|
223
|
+
const lastUnderscoreIndex = finalName.lastIndexOf("_");
|
|
224
|
+
if (lastUnderscoreIndex !== -1 && lastUnderscoreIndex < finalName.length - 1) {
|
|
225
|
+
return finalName.substring(0, lastUnderscoreIndex + 1) + finalName.charAt(lastUnderscoreIndex + 1).toUpperCase() + finalName.slice(lastUnderscoreIndex + 2);
|
|
226
|
+
}
|
|
227
|
+
return finalName;
|
|
228
|
+
}
|
|
229
|
+
return finalName.charAt(0).toUpperCase() + finalName.slice(1);
|
|
193
230
|
};
|
|
194
231
|
var propertyName = (name) => {
|
|
195
232
|
if (rxProperVariable.test(name.replace(/\./g, "_")))
|
|
@@ -895,7 +932,7 @@ var generateDocs = async (files) => {
|
|
|
895
932
|
const generated = [];
|
|
896
933
|
for (const doc of files) {
|
|
897
934
|
console.log(`Generating ${doc.name}`);
|
|
898
|
-
const ts = await generateTypescript(doc.name, doc.doc);
|
|
935
|
+
const ts = await generateTypescript(classname(doc.name), doc.doc);
|
|
899
936
|
generated.push({
|
|
900
937
|
...doc,
|
|
901
938
|
ts
|
|
@@ -908,13 +945,21 @@ var saveDocs = async (output, docs) => {
|
|
|
908
945
|
const dir = stats.isDirectory() ? output : (0, import_path.parse)(output).dir;
|
|
909
946
|
await (0, import_promises.mkdir)(dir, { recursive: true });
|
|
910
947
|
for (const doc of docs) {
|
|
911
|
-
const path = (0, import_path.resolve)(dir, `${doc.name}.ts`);
|
|
948
|
+
const path = (0, import_path.resolve)(dir, `${filename(doc.name)}.ts`);
|
|
912
949
|
console.log(`Writing ${path}`);
|
|
913
950
|
await (0, import_promises.writeFile)(path, doc.ts, "utf8");
|
|
914
951
|
}
|
|
915
952
|
};
|
|
953
|
+
var classname = (name) => {
|
|
954
|
+
return (0, import_change_case2.pascalCase)(name.replace(/\d+/g, ""));
|
|
955
|
+
};
|
|
956
|
+
var filename = (name) => {
|
|
957
|
+
return name.replace(/\./g, "_");
|
|
958
|
+
};
|
|
916
959
|
// Annotate the CommonJS export names for ESM import in node:
|
|
917
960
|
0 && (module.exports = {
|
|
961
|
+
classname,
|
|
962
|
+
filename,
|
|
918
963
|
generate,
|
|
919
964
|
generateTypescript
|
|
920
965
|
});
|