api-core-lib 12.0.37 → 12.0.38
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/cli.cjs +60 -32
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -36,10 +36,14 @@ var import_dotenv = __toESM(require("dotenv"), 1);
|
|
|
36
36
|
var import_child_process = require("child_process");
|
|
37
37
|
|
|
38
38
|
// src/generator/module-parser.ts
|
|
39
|
+
function sanitizeActionName(operationId) {
|
|
40
|
+
if (!operationId) return `unnamedAction_${Date.now()}`;
|
|
41
|
+
const name = operationId.split("_").slice(1).join("_");
|
|
42
|
+
return name.charAt(0).toLowerCase() + name.slice(1).replace(/_v\d+$/, "");
|
|
43
|
+
}
|
|
39
44
|
function refToTypeName(ref) {
|
|
40
45
|
if (!ref) return "unknown";
|
|
41
|
-
|
|
42
|
-
return `components["${parts.join('"]["')}"]`;
|
|
46
|
+
return ref.split("/").pop() || "unknown";
|
|
43
47
|
}
|
|
44
48
|
function parseSpecToModules(spec) {
|
|
45
49
|
const modules = {};
|
|
@@ -50,23 +54,37 @@ function parseSpecToModules(spec) {
|
|
|
50
54
|
const tagName = endpoint.tags[0];
|
|
51
55
|
const moduleName = tagName.replace(/[^a-zA-Z0-9]/g, "").replace(/Central|Tenant/g, "") + "Api";
|
|
52
56
|
if (!modules[moduleName]) {
|
|
53
|
-
|
|
57
|
+
const commonPath = apiPath.substring(0, apiPath.lastIndexOf("/"));
|
|
58
|
+
modules[moduleName] = { baseEndpoint: commonPath || "/", actions: {} };
|
|
54
59
|
}
|
|
55
|
-
const
|
|
60
|
+
const requestBody = endpoint.requestBody?.content?.["application/json"]?.schema;
|
|
56
61
|
const successResponse = endpoint.responses["200"] || endpoint.responses["201"];
|
|
57
|
-
const
|
|
58
|
-
|
|
62
|
+
const responseSchema = successResponse?.content?.["application/json"]?.schema;
|
|
63
|
+
let outputType = "unknown";
|
|
64
|
+
if (responseSchema) {
|
|
65
|
+
if (responseSchema.$ref) {
|
|
66
|
+
outputType = refToTypeName(responseSchema.$ref);
|
|
67
|
+
} else if (responseSchema.type === "array" && responseSchema.items?.$ref) {
|
|
68
|
+
outputType = `${refToTypeName(responseSchema.items.$ref)}[]`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let inputType = "undefined";
|
|
72
|
+
if (requestBody) {
|
|
73
|
+
inputType = refToTypeName(requestBody.$ref);
|
|
74
|
+
} else if ((endpoint.parameters || []).some((p) => p.in === "query")) {
|
|
75
|
+
inputType = "QueryOptions";
|
|
76
|
+
}
|
|
77
|
+
const actionName = sanitizeActionName(endpoint.operationId);
|
|
78
|
+
const relativePath = apiPath.replace(modules[moduleName].baseEndpoint, "") || "/";
|
|
59
79
|
modules[moduleName].actions[actionName] = {
|
|
60
80
|
method: method.toUpperCase(),
|
|
61
|
-
path:
|
|
81
|
+
path: relativePath,
|
|
62
82
|
description: endpoint.summary || "No description available.",
|
|
63
83
|
hasQuery: (endpoint.parameters || []).some((p) => p.in === "query"),
|
|
64
84
|
autoFetch: method.toUpperCase() === "GET" && !apiPath.includes("{"),
|
|
65
85
|
invalidates: [],
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
_inputType: refToTypeName(requestBodyRef),
|
|
69
|
-
_outputType: refToTypeName(responseRef)
|
|
86
|
+
_inputType: inputType,
|
|
87
|
+
_outputType: outputType
|
|
70
88
|
};
|
|
71
89
|
}
|
|
72
90
|
}
|
|
@@ -99,20 +117,25 @@ async function runGenerator(options) {
|
|
|
99
117
|
console.log(import_chalk.default.green(`\u2713 OpenAPI spec saved temporarily to ${tempSpecPath}`));
|
|
100
118
|
console.log("\n" + import_chalk.default.blue("Step 3: Generating organized TypeScript types..."));
|
|
101
119
|
const typesOutputPath = import_path.default.join(options.output, "types");
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
120
|
+
const ignoreFilePath = import_path.default.join(typesOutputPath, ".openapi-generator-ignore");
|
|
121
|
+
const ignoreContent = `
|
|
122
|
+
# Ignore all documentation files
|
|
123
|
+
docs/
|
|
124
|
+
# Ignore git and helper files
|
|
125
|
+
.openapi-generator-ignore
|
|
126
|
+
.npmignore
|
|
127
|
+
git_push.sh
|
|
128
|
+
# Ignore the main generator metadata
|
|
129
|
+
.openapi-generator/
|
|
130
|
+
# We only want the generated models and apis
|
|
131
|
+
`;
|
|
132
|
+
if (!import_fs.default.existsSync(typesOutputPath)) {
|
|
133
|
+
import_fs.default.mkdirSync(typesOutputPath, { recursive: true });
|
|
134
|
+
}
|
|
135
|
+
import_fs.default.writeFileSync(ignoreFilePath, ignoreContent);
|
|
136
|
+
console.log(import_chalk.default.gray("\u2713 Created .openapi-generator-ignore to ensure clean output."));
|
|
137
|
+
console.log(import_chalk.default.gray(`Executing: ${ignoreContent}`));
|
|
138
|
+
(0, import_child_process.execSync)(ignoreContent, { stdio: "inherit" });
|
|
116
139
|
console.log(import_chalk.default.green(`\u2713 Types generated successfully at ${typesOutputPath}`));
|
|
117
140
|
console.log("\n" + import_chalk.default.blue("Step 4: Generating custom API modules..."));
|
|
118
141
|
const modules = parseSpecToModules(spec);
|
|
@@ -120,14 +143,19 @@ async function runGenerator(options) {
|
|
|
120
143
|
if (!import_fs.default.existsSync(modulesOutputPath)) {
|
|
121
144
|
import_fs.default.mkdirSync(modulesOutputPath, { recursive: true });
|
|
122
145
|
}
|
|
146
|
+
const allModelTypes = /* @__PURE__ */ new Set();
|
|
147
|
+
Object.values(modules).forEach((mod) => {
|
|
148
|
+
Object.values(mod.actions).forEach((action) => {
|
|
149
|
+
if (action._inputType && !["unknown", "undefined", "QueryOptions"].includes(action._inputType)) allModelTypes.add(action._inputType.replace("[]", ""));
|
|
150
|
+
if (action._outputType && !["unknown"].includes(action._outputType)) allModelTypes.add(action._outputType.replace("[]", ""));
|
|
151
|
+
});
|
|
152
|
+
});
|
|
123
153
|
for (const moduleName in modules) {
|
|
124
154
|
const moduleData = modules[moduleName];
|
|
125
155
|
const fileName = `${moduleName}.module.ts`;
|
|
126
156
|
const filePath = import_path.default.join(modulesOutputPath, fileName);
|
|
127
157
|
const actionsTypeParts = Object.entries(moduleData.actions).map(([actionName, actionData]) => {
|
|
128
|
-
|
|
129
|
-
const outputType = actionData._outputType;
|
|
130
|
-
return ` ${actionName}: ActionConfigModule<${inputType}, ${outputType}>;`;
|
|
158
|
+
return ` ${actionName}: ActionConfigModule<${actionData._inputType}, ${actionData._outputType}>;`;
|
|
131
159
|
});
|
|
132
160
|
const actionsTypeDefinition = `{
|
|
133
161
|
${actionsTypeParts.join("\n")}
|
|
@@ -146,14 +174,14 @@ ${actionsValueParts.join(",\n")}
|
|
|
146
174
|
*/
|
|
147
175
|
|
|
148
176
|
import type { ApiModuleConfig, ActionConfigModule, QueryOptions } from 'api-core-lib';
|
|
149
|
-
|
|
177
|
+
// [\u0625\u0635\u0644\u0627\u062D] \u0627\u0633\u062A\u064A\u0631\u0627\u062F \u0627\u0644\u0623\u0646\u0648\u0627\u0639 \u0645\u0646 \u0627\u0644\u0645\u0644\u0641 \u0627\u0644\u0631\u0626\u064A\u0633\u064A \u0627\u0644\u0645\u0646\u0638\u0645
|
|
178
|
+
import type { ${[...allModelTypes].join(", ")} } from '../types';
|
|
150
179
|
|
|
151
180
|
/**
|
|
152
181
|
* Defines the configuration for the ${moduleName} API module.
|
|
153
|
-
* This is a strongly-typed configuration object that ensures type safety.
|
|
154
182
|
*/
|
|
155
|
-
export const ${moduleName}Module:
|
|
156
|
-
baseEndpoint: '
|
|
183
|
+
export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
|
|
184
|
+
baseEndpoint: '${moduleData.baseEndpoint}',
|
|
157
185
|
actions: ${actionsValueDefinition},
|
|
158
186
|
};
|
|
159
187
|
`;
|