@sebspark/openapi-typegen 4.0.0 → 5.0.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 +27 -65
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,58 +1,21 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
1
|
// src/index.ts
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
generate: () => generate2,
|
|
36
|
-
generateTypescript: () => generateTypescript
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(index_exports);
|
|
39
|
-
var import_promises = require("fs/promises");
|
|
40
|
-
var import_node_path = require("path");
|
|
41
|
-
var import_change_case2 = require("change-case");
|
|
42
|
-
var YAML = __toESM(require("yaml"));
|
|
2
|
+
import { mkdir, readdir, readFile, stat, writeFile } from "fs/promises";
|
|
3
|
+
import { parse, resolve } from "path";
|
|
4
|
+
import { pascalCase as pascalCase2 } from "change-case";
|
|
5
|
+
import * as YAML from "yaml";
|
|
43
6
|
|
|
44
7
|
// src/generator/formatter.ts
|
|
45
|
-
|
|
8
|
+
import { format as pFormat } from "prettier";
|
|
46
9
|
var options = {
|
|
47
10
|
parser: "typescript",
|
|
48
11
|
singleQuote: true,
|
|
49
12
|
semi: false,
|
|
50
13
|
trailingComma: "all"
|
|
51
14
|
};
|
|
52
|
-
var format = async (code) => (
|
|
15
|
+
var format = async (code) => pFormat(code, options);
|
|
53
16
|
|
|
54
17
|
// src/generator/common.ts
|
|
55
|
-
|
|
18
|
+
import { constantCase, pascalCase } from "change-case";
|
|
56
19
|
|
|
57
20
|
// src/generator/document.ts
|
|
58
21
|
var document = ({ title, description }) => {
|
|
@@ -221,12 +184,12 @@ var typeName = (name) => {
|
|
|
221
184
|
}
|
|
222
185
|
const domainStyleTransformed = name.split(".").map((part, index, array) => {
|
|
223
186
|
if (index === array.length - 1) {
|
|
224
|
-
return
|
|
187
|
+
return pascalCase(part);
|
|
225
188
|
}
|
|
226
189
|
return part;
|
|
227
190
|
}).join("_");
|
|
228
191
|
const prefixedIfNumberStart = domainStyleTransformed.match(/^\d/) ? `_${domainStyleTransformed}` : domainStyleTransformed;
|
|
229
|
-
const finalName = prefixedIfNumberStart.includes("_") ? prefixedIfNumberStart :
|
|
192
|
+
const finalName = prefixedIfNumberStart.includes("_") ? prefixedIfNumberStart : pascalCase(prefixedIfNumberStart);
|
|
230
193
|
if (finalName.includes("_")) {
|
|
231
194
|
const lastUnderscoreIndex = finalName.lastIndexOf("_");
|
|
232
195
|
if (lastUnderscoreIndex !== -1 && lastUnderscoreIndex < finalName.length - 1) {
|
|
@@ -280,7 +243,7 @@ var generateArray = (parsed) => {
|
|
|
280
243
|
var generateEnum = (parsed) => {
|
|
281
244
|
if (parsed.name) {
|
|
282
245
|
const values = parsed.values.map(serializeValue).join(", ");
|
|
283
|
-
const valuesName =
|
|
246
|
+
const valuesName = constantCase(`${parsed.name}_VALUES`);
|
|
284
247
|
return [
|
|
285
248
|
`export const ${valuesName} = [${values}] as const`,
|
|
286
249
|
`${preamble(parsed)}typeof ${valuesName}[number]`
|
|
@@ -478,7 +441,7 @@ var generate = (name, doc) => `
|
|
|
478
441
|
RequestOptions,
|
|
479
442
|
Serialized,
|
|
480
443
|
} from '@sebspark/openapi-core'
|
|
481
|
-
import type { Request as ExpressRequest } from 'express'
|
|
444
|
+
import type { Request as ExpressRequest } from 'express-serve-static-core'
|
|
482
445
|
|
|
483
446
|
type Req = Pick<ExpressRequest, 'url' | 'baseUrl' | 'cookies' | 'hostname'>
|
|
484
447
|
|
|
@@ -1051,29 +1014,29 @@ var generate2 = async (input, output) => {
|
|
|
1051
1014
|
await saveDocs(output, generated);
|
|
1052
1015
|
};
|
|
1053
1016
|
var readDocs = async (input) => {
|
|
1054
|
-
const path =
|
|
1055
|
-
const stats = await
|
|
1017
|
+
const path = resolve(input);
|
|
1018
|
+
const stats = await stat(path);
|
|
1056
1019
|
const filePaths = [];
|
|
1057
1020
|
if (stats.isFile()) filePaths.push(path);
|
|
1058
1021
|
if (stats.isDirectory()) {
|
|
1059
|
-
const files = await
|
|
1060
|
-
filePaths.push(...files.map((f) =>
|
|
1022
|
+
const files = await readdir(path);
|
|
1023
|
+
filePaths.push(...files.map((f) => resolve(path, f)));
|
|
1061
1024
|
}
|
|
1062
1025
|
const readFiles = [];
|
|
1063
1026
|
for (const p of filePaths) {
|
|
1064
|
-
const { name, ext } =
|
|
1027
|
+
const { name, ext } = parse(p);
|
|
1065
1028
|
let doc;
|
|
1066
1029
|
switch (ext) {
|
|
1067
1030
|
case ".json": {
|
|
1068
1031
|
console.log(`Reading ${p}`);
|
|
1069
|
-
const txt = await
|
|
1032
|
+
const txt = await readFile(p, "utf8");
|
|
1070
1033
|
doc = JSON.parse(txt);
|
|
1071
1034
|
break;
|
|
1072
1035
|
}
|
|
1073
1036
|
case ".yml":
|
|
1074
1037
|
case ".yaml": {
|
|
1075
1038
|
console.log(`Reading ${p}`);
|
|
1076
|
-
const txt = await
|
|
1039
|
+
const txt = await readFile(p, "utf8");
|
|
1077
1040
|
doc = YAML.parse(txt);
|
|
1078
1041
|
break;
|
|
1079
1042
|
}
|
|
@@ -1100,25 +1063,24 @@ var generateDocs = async (files) => {
|
|
|
1100
1063
|
return generated;
|
|
1101
1064
|
};
|
|
1102
1065
|
var saveDocs = async (output, docs) => {
|
|
1103
|
-
const stats = await
|
|
1104
|
-
const dir = stats.isDirectory() ? output :
|
|
1105
|
-
await
|
|
1066
|
+
const stats = await stat(output);
|
|
1067
|
+
const dir = stats.isDirectory() ? output : parse(output).dir;
|
|
1068
|
+
await mkdir(dir, { recursive: true });
|
|
1106
1069
|
for (const doc of docs) {
|
|
1107
|
-
const path =
|
|
1070
|
+
const path = resolve(dir, `${filename(doc.name)}.ts`);
|
|
1108
1071
|
console.log(`Writing ${path}`);
|
|
1109
|
-
await
|
|
1072
|
+
await writeFile(path, doc.ts, "utf8");
|
|
1110
1073
|
}
|
|
1111
1074
|
};
|
|
1112
1075
|
var classname = (name) => {
|
|
1113
|
-
return (
|
|
1076
|
+
return pascalCase2(name.replace(/\d+/g, ""));
|
|
1114
1077
|
};
|
|
1115
1078
|
var filename = (name) => {
|
|
1116
1079
|
return name.replace(/\./g, "_");
|
|
1117
1080
|
};
|
|
1118
|
-
|
|
1119
|
-
0 && (module.exports = {
|
|
1081
|
+
export {
|
|
1120
1082
|
classname,
|
|
1121
1083
|
filename,
|
|
1122
|
-
generate,
|
|
1084
|
+
generate2 as generate,
|
|
1123
1085
|
generateTypescript
|
|
1124
|
-
}
|
|
1086
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/openapi-typegen",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"bin": "./cli.mjs",
|
|
8
9
|
"files": [
|
|
@@ -10,16 +11,16 @@
|
|
|
10
11
|
"cli.mjs"
|
|
11
12
|
],
|
|
12
13
|
"scripts": {
|
|
13
|
-
"build": "tsup-node src/index.ts src/cli.ts --format
|
|
14
|
+
"build": "tsup-node src/index.ts src/cli.ts --format esm --target node22 --dts",
|
|
14
15
|
"dev": "tsc --watch --noEmit",
|
|
15
16
|
"lint": "biome check .",
|
|
16
17
|
"test": "vitest run --passWithNoTests --coverage",
|
|
17
18
|
"typecheck": "vitest --typecheck.only --passWithNoTests"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
21
|
+
"@sebspark/tsconfig": "*",
|
|
20
22
|
"@types/yargs": "17.0.34",
|
|
21
|
-
"
|
|
22
|
-
"vitest": "4.0.3"
|
|
23
|
+
"vitest": "4.0.5"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
26
|
"@sebspark/openapi-core": "*",
|