api-core-lib 12.0.52 → 12.0.54
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 +81 -68
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -30,7 +30,7 @@ var import_path3 = __toESM(require("path"), 1);
|
|
|
30
30
|
// src/generator/index.ts
|
|
31
31
|
var import_path2 = __toESM(require("path"), 1);
|
|
32
32
|
var import_axios = __toESM(require("axios"), 1);
|
|
33
|
-
var
|
|
33
|
+
var import_chalk2 = __toESM(require("chalk"), 1);
|
|
34
34
|
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
35
35
|
var import_json_schema_ref_parser = __toESM(require("@apidevtools/json-schema-ref-parser"), 1);
|
|
36
36
|
|
|
@@ -49,7 +49,7 @@ function parseSpecToModules(spec) {
|
|
|
49
49
|
}
|
|
50
50
|
const { inputType, outputType } = getInputOutputTypes(endpoint);
|
|
51
51
|
[inputType, outputType].forEach((t) => {
|
|
52
|
-
if (t && !["unknown", "undefined", "any", "QueryOptions"].includes(t)) {
|
|
52
|
+
if (t && !["unknown", "undefined", "any", "QueryOptions", "Promise"].includes(t)) {
|
|
53
53
|
modules[moduleName].types.add(t.replace("[]", ""));
|
|
54
54
|
}
|
|
55
55
|
});
|
|
@@ -70,26 +70,21 @@ function parseSpecToModules(spec) {
|
|
|
70
70
|
return modules;
|
|
71
71
|
}
|
|
72
72
|
function getInputOutputTypes(endpoint) {
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
const responseSchema = successResponse?.content?.["application/json"]?.schema;
|
|
73
|
+
const requestBodySchema = endpoint.requestBody?.content?.["application/json"]?.schema;
|
|
74
|
+
const successResponseSchema = endpoint.responses?.["200"]?.content?.["application/json"]?.schema || endpoint.responses?.["201"]?.content?.["application/json"]?.schema;
|
|
76
75
|
let outputType = "unknown";
|
|
77
|
-
if (
|
|
78
|
-
if (
|
|
79
|
-
outputType =
|
|
80
|
-
} else if (
|
|
81
|
-
outputType = refToTypeName(
|
|
82
|
-
} else if (responseSchema.type === "array" && (responseSchema.items?.title || responseSchema.items?.$ref)) {
|
|
83
|
-
outputType = (responseSchema.items.title || refToTypeName(responseSchema.items.$ref)) + "[]";
|
|
76
|
+
if (successResponseSchema) {
|
|
77
|
+
if (successResponseSchema.$ref) {
|
|
78
|
+
outputType = refToTypeName(successResponseSchema.$ref);
|
|
79
|
+
} else if (successResponseSchema.type === "array" && successResponseSchema.items?.$ref) {
|
|
80
|
+
outputType = `${refToTypeName(successResponseSchema.items.$ref)}[]`;
|
|
84
81
|
}
|
|
85
82
|
}
|
|
86
83
|
let inputType = "undefined";
|
|
87
|
-
if (
|
|
88
|
-
if (
|
|
89
|
-
inputType =
|
|
90
|
-
} else if (
|
|
91
|
-
inputType = refToTypeName(requestBody.$ref);
|
|
92
|
-
} else if (requestBody.type === "object") {
|
|
84
|
+
if (requestBodySchema) {
|
|
85
|
+
if (requestBodySchema.$ref) {
|
|
86
|
+
inputType = refToTypeName(requestBodySchema.$ref);
|
|
87
|
+
} else if (requestBodySchema.type === "object") {
|
|
93
88
|
inputType = "any";
|
|
94
89
|
}
|
|
95
90
|
} else if ((endpoint.parameters || []).some((p) => p.in === "query")) {
|
|
@@ -113,16 +108,25 @@ function refToTypeName(ref) {
|
|
|
113
108
|
// src/generator/file-generator.ts
|
|
114
109
|
var import_fs = __toESM(require("fs"), 1);
|
|
115
110
|
var import_path = __toESM(require("path"), 1);
|
|
111
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
116
112
|
var import_json_schema_to_typescript = require("json-schema-to-typescript");
|
|
117
113
|
async function generateModuleFiles(moduleName, moduleData, spec, outputDir) {
|
|
114
|
+
console.log(import_chalk.default.cyan(`
|
|
115
|
+
Generating module: ${moduleName}`));
|
|
118
116
|
const moduleFolderPath = import_path.default.join(outputDir, moduleName);
|
|
119
117
|
if (!import_fs.default.existsSync(moduleFolderPath)) {
|
|
120
118
|
import_fs.default.mkdirSync(moduleFolderPath, { recursive: true });
|
|
121
119
|
}
|
|
122
|
-
await generateTypesFile(moduleFolderPath, moduleData.types, spec);
|
|
123
|
-
await generateConfigFile(moduleFolderPath, moduleName, moduleData
|
|
120
|
+
await generateTypesFile(moduleFolderPath, moduleName, moduleData.types, spec);
|
|
121
|
+
await generateConfigFile(moduleFolderPath, moduleName, moduleData);
|
|
124
122
|
}
|
|
125
|
-
async function generateTypesFile(moduleFolderPath, typeNames, spec) {
|
|
123
|
+
async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec) {
|
|
124
|
+
const typesCount = typeNames.size;
|
|
125
|
+
if (typesCount === 0) {
|
|
126
|
+
console.log(import_chalk.default.yellow(` - No types found for this module. types.ts will be empty.`));
|
|
127
|
+
} else {
|
|
128
|
+
console.log(import_chalk.default.gray(` - Found ${typesCount} types to generate for types.ts...`));
|
|
129
|
+
}
|
|
126
130
|
let typesContent = `// This file is auto-generated by the API generator. Do not edit.
|
|
127
131
|
|
|
128
132
|
`;
|
|
@@ -130,27 +134,36 @@ async function generateTypesFile(moduleFolderPath, typeNames, spec) {
|
|
|
130
134
|
for (const typeName of typeNames) {
|
|
131
135
|
const schema = allSchemas[typeName];
|
|
132
136
|
if (schema) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
137
|
+
try {
|
|
138
|
+
const tsType = await (0, import_json_schema_to_typescript.compile)(schema, typeName, {
|
|
139
|
+
bannerComment: "",
|
|
140
|
+
additionalProperties: false,
|
|
141
|
+
style: {
|
|
142
|
+
bracketSpacing: true,
|
|
143
|
+
printWidth: 120,
|
|
144
|
+
semi: true,
|
|
145
|
+
singleQuote: true,
|
|
146
|
+
tabWidth: 2,
|
|
147
|
+
trailingComma: "es5",
|
|
148
|
+
useTabs: false
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
typesContent += tsType + "\n";
|
|
152
|
+
} catch (compileError) {
|
|
153
|
+
console.error(import_chalk.default.red(` - Error compiling type "${typeName}" for module "${moduleName}":`));
|
|
154
|
+
console.error(import_chalk.default.red(` ${compileError.message}`));
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
console.log(import_chalk.default.yellow(` - Warning: Schema for type "${typeName}" not found in dereferenced spec, skipping.`));
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
const typesFilePath = import_path.default.join(moduleFolderPath, "types.ts");
|
|
150
161
|
import_fs.default.writeFileSync(typesFilePath, typesContent);
|
|
151
162
|
}
|
|
152
|
-
async function generateConfigFile(moduleFolderPath, moduleName, moduleData
|
|
153
|
-
const typeNamesArray = [...
|
|
163
|
+
async function generateConfigFile(moduleFolderPath, moduleName, moduleData) {
|
|
164
|
+
const typeNamesArray = [...moduleData.types];
|
|
165
|
+
const actionsCount = Object.keys(moduleData.actions).length;
|
|
166
|
+
console.log(import_chalk.default.gray(` - Found ${actionsCount} actions to generate for config.ts...`));
|
|
154
167
|
const typesImportStatement = typeNamesArray.length > 0 ? `import type { ${typeNamesArray.join(", ")} } from './types';` : ``;
|
|
155
168
|
const actionsTypeParts = Object.entries(moduleData.actions).map(
|
|
156
169
|
([actionName, actionData]) => ` ${actionName}: ActionConfigModule<${actionData._inputType}, ${actionData._outputType}>;`
|
|
@@ -182,35 +195,41 @@ export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
|
|
|
182
195
|
|
|
183
196
|
// src/generator/index.ts
|
|
184
197
|
async function runGenerator(options) {
|
|
185
|
-
console.log(
|
|
186
|
-
console.log(
|
|
187
|
-
console.log(
|
|
188
|
-
console.log("\n" +
|
|
198
|
+
console.log(import_chalk2.default.cyan.bold("\u{1F680} Starting API Core Lib Code Generator..."));
|
|
199
|
+
console.log(import_chalk2.default.gray(`Output directory: ${options.output}`));
|
|
200
|
+
console.log(import_chalk2.default.gray(`.env path: ${options.envPath}`));
|
|
201
|
+
console.log("\n" + import_chalk2.default.blue("Step 1: Loading environment variables..."));
|
|
189
202
|
import_dotenv.default.config({ path: options.envPath });
|
|
190
203
|
const specUrl = getSpecUrl();
|
|
191
|
-
console.log(
|
|
204
|
+
console.log(import_chalk2.default.green("\u2713 Environment variables loaded."));
|
|
192
205
|
try {
|
|
193
|
-
console.log("\n" +
|
|
206
|
+
console.log("\n" + import_chalk2.default.blue(`Step 2: Fetching OpenAPI spec from ${specUrl}...`));
|
|
194
207
|
const response = await import_axios.default.get(specUrl, { timeout: 15e3 });
|
|
195
|
-
|
|
196
|
-
if (!
|
|
208
|
+
const originalSpec = response.data;
|
|
209
|
+
if (!originalSpec.openapi || !originalSpec.paths) {
|
|
197
210
|
throw new Error('Invalid OpenAPI specification file. "openapi" or "paths" property is missing.');
|
|
198
211
|
}
|
|
199
|
-
console.log(
|
|
200
|
-
console.log(
|
|
201
|
-
|
|
202
|
-
console.log(
|
|
203
|
-
console.log("\n" +
|
|
204
|
-
const modules = parseSpecToModules(
|
|
212
|
+
console.log(import_chalk2.default.green("\u2713 OpenAPI spec fetched successfully."));
|
|
213
|
+
console.log(import_chalk2.default.blue("Step 2.5: Dereferencing all $refs in the spec..."));
|
|
214
|
+
const dereferencedSpec = await import_json_schema_ref_parser.default.dereference(originalSpec);
|
|
215
|
+
console.log(import_chalk2.default.green("\u2713 Spec dereferenced successfully."));
|
|
216
|
+
console.log("\n" + import_chalk2.default.blue("Step 3: Parsing spec and generating API modules..."));
|
|
217
|
+
const modules = parseSpecToModules(originalSpec);
|
|
218
|
+
const modulesCount = Object.keys(modules).length;
|
|
219
|
+
console.log(import_chalk2.default.gray(`Found ${modulesCount} modules to generate...`));
|
|
220
|
+
if (modulesCount === 0) {
|
|
221
|
+
console.log(import_chalk2.default.yellow("Warning: No modules found based on tags in the OpenAPI spec. No files will be generated."));
|
|
222
|
+
}
|
|
205
223
|
const modulesOutputPath = import_path2.default.join(options.output, "modules");
|
|
206
224
|
for (const moduleName in modules) {
|
|
207
225
|
const moduleData = modules[moduleName];
|
|
208
|
-
await generateModuleFiles(moduleName, moduleData,
|
|
209
|
-
console.log(import_chalk.default.green(`\u2713 Module generated: ${moduleName}/ (config.ts, types.ts)`));
|
|
226
|
+
await generateModuleFiles(moduleName, moduleData, dereferencedSpec, modulesOutputPath);
|
|
210
227
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
228
|
+
if (modulesCount > 0) {
|
|
229
|
+
console.log(import_chalk2.default.green("\u2713 All custom modules generated."));
|
|
230
|
+
}
|
|
231
|
+
console.log(import_chalk2.default.bold.green("\n\u{1F389} API generation complete! All files are located in:"));
|
|
232
|
+
console.log(import_chalk2.default.bold.cyan(options.output));
|
|
214
233
|
} catch (error) {
|
|
215
234
|
handleGenerationError(error);
|
|
216
235
|
}
|
|
@@ -218,23 +237,17 @@ async function runGenerator(options) {
|
|
|
218
237
|
function getSpecUrl() {
|
|
219
238
|
const specUrl = process.env.OPENAPI_SPEC_URL || (process.env.API_URL ? `${process.env.API_URL}/docs-json` : null) || (process.env.NEXT_PUBLIC_API_URL ? `${process.env.NEXT_PUBLIC_API_URL}/docs-json` : null);
|
|
220
239
|
if (!specUrl) {
|
|
221
|
-
console.error(
|
|
222
|
-
console.error(
|
|
240
|
+
console.error(import_chalk2.default.red.bold("\n\u274C Error: API specification URL not found."));
|
|
241
|
+
console.error(import_chalk2.default.red("Please define either OPENAPI_SPEC_URL or API_URL/NEXT_PUBLIC_API_URL in your .env file."));
|
|
223
242
|
process.exit(1);
|
|
224
243
|
}
|
|
225
244
|
return specUrl;
|
|
226
245
|
}
|
|
227
246
|
function handleGenerationError(error) {
|
|
228
|
-
console.error(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
console.error(import_chalk.default.red(`Network Error: ${error.message} (Status: ${error.response.status})`));
|
|
233
|
-
} else if (error.stderr) {
|
|
234
|
-
console.error(import_chalk.default.red(`Command Execution Error: ${error.message}`));
|
|
235
|
-
console.error(import_chalk.default.gray(error.stderr.toString()));
|
|
236
|
-
} else {
|
|
237
|
-
console.error(import_chalk.default.red(error.message));
|
|
247
|
+
console.error(import_chalk2.default.red.bold("\n\u274C An error occurred during generation:"));
|
|
248
|
+
console.error(import_chalk2.default.red(`Error Message: ${error.message}`));
|
|
249
|
+
if (error.stack) {
|
|
250
|
+
console.error(import_chalk2.default.gray(error.stack));
|
|
238
251
|
}
|
|
239
252
|
process.exit(1);
|
|
240
253
|
}
|