api-core-lib 12.0.53 → 12.0.55
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 +61 -52
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -30,11 +30,12 @@ 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_chalk3 = __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
|
|
|
37
37
|
// src/generator/spec-parser.ts
|
|
38
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
38
39
|
function parseSpecToModules(spec) {
|
|
39
40
|
const modules = {};
|
|
40
41
|
for (const apiPath in spec.paths) {
|
|
@@ -47,7 +48,7 @@ function parseSpecToModules(spec) {
|
|
|
47
48
|
const commonPath = apiPath.substring(0, apiPath.lastIndexOf("/"));
|
|
48
49
|
modules[moduleName] = { baseEndpoint: commonPath || "/", actions: {}, types: /* @__PURE__ */ new Set() };
|
|
49
50
|
}
|
|
50
|
-
const { inputType, outputType } = getInputOutputTypes(endpoint);
|
|
51
|
+
const { inputType, outputType } = getInputOutputTypes(endpoint, apiPath);
|
|
51
52
|
[inputType, outputType].forEach((t) => {
|
|
52
53
|
if (t && !["unknown", "undefined", "any", "QueryOptions", "Promise"].includes(t)) {
|
|
53
54
|
modules[moduleName].types.add(t.replace("[]", ""));
|
|
@@ -69,28 +70,36 @@ function parseSpecToModules(spec) {
|
|
|
69
70
|
}
|
|
70
71
|
return modules;
|
|
71
72
|
}
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
function getInputOutputTypes(endpoint, apiPath) {
|
|
74
|
+
const requestBodySchema = endpoint.requestBody?.content?.["application/json"]?.schema;
|
|
75
|
+
const successResponseSchema = endpoint.responses?.["200"]?.content?.["application/json"]?.schema || endpoint.responses?.["201"]?.content?.["application/json"]?.schema;
|
|
76
|
+
const operationId = endpoint.operationId || `(unknown operation at ${apiPath})`;
|
|
77
|
+
console.log(import_chalk.default.magenta(`
|
|
78
|
+
--- DIAGNOSING Operation: ${operationId} ---`));
|
|
79
|
+
if (successResponseSchema) {
|
|
80
|
+
console.log(import_chalk.default.blue(" Response Schema Structure:"));
|
|
81
|
+
console.log(JSON.stringify(successResponseSchema, null, 2));
|
|
82
|
+
} else {
|
|
83
|
+
console.log(import_chalk.default.gray(" - No success response schema found."));
|
|
76
84
|
}
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
if (requestBodySchema) {
|
|
86
|
+
console.log(import_chalk.default.blue(" Request Body Schema Structure:"));
|
|
87
|
+
console.log(JSON.stringify(requestBodySchema, null, 2));
|
|
80
88
|
}
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
console.log(import_chalk.default.magenta("---------------------------------------------------\n"));
|
|
90
|
+
let outputType = "unknown";
|
|
91
|
+
if (successResponseSchema) {
|
|
92
|
+
if (successResponseSchema.$ref) {
|
|
93
|
+
outputType = refToTypeName(successResponseSchema.$ref);
|
|
94
|
+
} else if (successResponseSchema.type === "array" && successResponseSchema.items?.$ref) {
|
|
95
|
+
outputType = `${refToTypeName(successResponseSchema.items.$ref)}[]`;
|
|
96
|
+
}
|
|
83
97
|
}
|
|
84
|
-
return "unknown";
|
|
85
|
-
}
|
|
86
|
-
function getInputOutputTypes(endpoint) {
|
|
87
|
-
const requestBodySchema = endpoint.requestBody?.content?.["application/json"]?.schema;
|
|
88
|
-
const successResponseSchema = endpoint.responses?.["200"]?.content?.["application/json"]?.schema || endpoint.responses?.["201"]?.content?.["application/json"]?.schema;
|
|
89
|
-
let outputType = extractTypeNameFromSchema(successResponseSchema);
|
|
90
98
|
let inputType = "undefined";
|
|
91
99
|
if (requestBodySchema) {
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
if (requestBodySchema.$ref) {
|
|
101
|
+
inputType = refToTypeName(requestBodySchema.$ref);
|
|
102
|
+
} else if (requestBodySchema.type === "object") {
|
|
94
103
|
inputType = "any";
|
|
95
104
|
}
|
|
96
105
|
} else if ((endpoint.parameters || []).some((p) => p.in === "query")) {
|
|
@@ -114,10 +123,10 @@ function refToTypeName(ref) {
|
|
|
114
123
|
// src/generator/file-generator.ts
|
|
115
124
|
var import_fs = __toESM(require("fs"), 1);
|
|
116
125
|
var import_path = __toESM(require("path"), 1);
|
|
117
|
-
var
|
|
126
|
+
var import_chalk2 = __toESM(require("chalk"), 1);
|
|
118
127
|
var import_json_schema_to_typescript = require("json-schema-to-typescript");
|
|
119
128
|
async function generateModuleFiles(moduleName, moduleData, spec, outputDir) {
|
|
120
|
-
console.log(
|
|
129
|
+
console.log(import_chalk2.default.cyan(`
|
|
121
130
|
Generating module: ${moduleName}`));
|
|
122
131
|
const moduleFolderPath = import_path.default.join(outputDir, moduleName);
|
|
123
132
|
if (!import_fs.default.existsSync(moduleFolderPath)) {
|
|
@@ -129,9 +138,9 @@ Generating module: ${moduleName}`));
|
|
|
129
138
|
async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec) {
|
|
130
139
|
const typesCount = typeNames.size;
|
|
131
140
|
if (typesCount === 0) {
|
|
132
|
-
console.log(
|
|
141
|
+
console.log(import_chalk2.default.yellow(` - No types found for this module. types.ts will be empty.`));
|
|
133
142
|
} else {
|
|
134
|
-
console.log(
|
|
143
|
+
console.log(import_chalk2.default.gray(` - Found ${typesCount} types to generate for types.ts...`));
|
|
135
144
|
}
|
|
136
145
|
let typesContent = `// This file is auto-generated by the API generator. Do not edit.
|
|
137
146
|
|
|
@@ -156,11 +165,11 @@ async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec)
|
|
|
156
165
|
});
|
|
157
166
|
typesContent += tsType + "\n";
|
|
158
167
|
} catch (compileError) {
|
|
159
|
-
console.error(
|
|
160
|
-
console.error(
|
|
168
|
+
console.error(import_chalk2.default.red(` - Error compiling type "${typeName}" for module "${moduleName}":`));
|
|
169
|
+
console.error(import_chalk2.default.red(` ${compileError.message}`));
|
|
161
170
|
}
|
|
162
171
|
} else {
|
|
163
|
-
console.log(
|
|
172
|
+
console.log(import_chalk2.default.yellow(` - Warning: Schema for type "${typeName}" not found in dereferenced spec, skipping.`));
|
|
164
173
|
}
|
|
165
174
|
}
|
|
166
175
|
const typesFilePath = import_path.default.join(moduleFolderPath, "types.ts");
|
|
@@ -169,7 +178,7 @@ async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec)
|
|
|
169
178
|
async function generateConfigFile(moduleFolderPath, moduleName, moduleData) {
|
|
170
179
|
const typeNamesArray = [...moduleData.types];
|
|
171
180
|
const actionsCount = Object.keys(moduleData.actions).length;
|
|
172
|
-
console.log(
|
|
181
|
+
console.log(import_chalk2.default.gray(` - Found ${actionsCount} actions to generate for config.ts...`));
|
|
173
182
|
const typesImportStatement = typeNamesArray.length > 0 ? `import type { ${typeNamesArray.join(", ")} } from './types';` : ``;
|
|
174
183
|
const actionsTypeParts = Object.entries(moduleData.actions).map(
|
|
175
184
|
([actionName, actionData]) => ` ${actionName}: ActionConfigModule<${actionData._inputType}, ${actionData._outputType}>;`
|
|
@@ -201,41 +210,41 @@ export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
|
|
|
201
210
|
|
|
202
211
|
// src/generator/index.ts
|
|
203
212
|
async function runGenerator(options) {
|
|
204
|
-
console.log(
|
|
205
|
-
console.log(
|
|
206
|
-
console.log(
|
|
207
|
-
console.log("\n" +
|
|
213
|
+
console.log(import_chalk3.default.cyan.bold("\u{1F680} Starting API Core Lib Code Generator..."));
|
|
214
|
+
console.log(import_chalk3.default.gray(`Output directory: ${options.output}`));
|
|
215
|
+
console.log(import_chalk3.default.gray(`.env path: ${options.envPath}`));
|
|
216
|
+
console.log("\n" + import_chalk3.default.blue("Step 1: Loading environment variables..."));
|
|
208
217
|
import_dotenv.default.config({ path: options.envPath });
|
|
209
218
|
const specUrl = getSpecUrl();
|
|
210
|
-
console.log(
|
|
219
|
+
console.log(import_chalk3.default.green("\u2713 Environment variables loaded."));
|
|
211
220
|
try {
|
|
212
|
-
console.log("\n" +
|
|
221
|
+
console.log("\n" + import_chalk3.default.blue(`Step 2: Fetching OpenAPI spec from ${specUrl}...`));
|
|
213
222
|
const response = await import_axios.default.get(specUrl, { timeout: 15e3 });
|
|
214
|
-
|
|
215
|
-
if (!
|
|
223
|
+
const originalSpec = response.data;
|
|
224
|
+
if (!originalSpec.openapi || !originalSpec.paths) {
|
|
216
225
|
throw new Error('Invalid OpenAPI specification file. "openapi" or "paths" property is missing.');
|
|
217
226
|
}
|
|
218
|
-
console.log(
|
|
219
|
-
console.log(
|
|
220
|
-
|
|
221
|
-
console.log(
|
|
222
|
-
console.log("\n" +
|
|
223
|
-
const modules = parseSpecToModules(
|
|
227
|
+
console.log(import_chalk3.default.green("\u2713 OpenAPI spec fetched successfully."));
|
|
228
|
+
console.log(import_chalk3.default.blue("Step 2.5: Dereferencing all $refs in the spec..."));
|
|
229
|
+
const dereferencedSpec = await import_json_schema_ref_parser.default.dereference(originalSpec);
|
|
230
|
+
console.log(import_chalk3.default.green("\u2713 Spec dereferenced successfully."));
|
|
231
|
+
console.log("\n" + import_chalk3.default.blue("Step 3: Parsing spec and generating API modules..."));
|
|
232
|
+
const modules = parseSpecToModules(originalSpec);
|
|
224
233
|
const modulesCount = Object.keys(modules).length;
|
|
225
|
-
console.log(
|
|
234
|
+
console.log(import_chalk3.default.gray(`Found ${modulesCount} modules to generate...`));
|
|
226
235
|
if (modulesCount === 0) {
|
|
227
|
-
console.log(
|
|
236
|
+
console.log(import_chalk3.default.yellow("Warning: No modules found based on tags in the OpenAPI spec. No files will be generated."));
|
|
228
237
|
}
|
|
229
238
|
const modulesOutputPath = import_path2.default.join(options.output, "modules");
|
|
230
239
|
for (const moduleName in modules) {
|
|
231
240
|
const moduleData = modules[moduleName];
|
|
232
|
-
await generateModuleFiles(moduleName, moduleData,
|
|
241
|
+
await generateModuleFiles(moduleName, moduleData, dereferencedSpec, modulesOutputPath);
|
|
233
242
|
}
|
|
234
243
|
if (modulesCount > 0) {
|
|
235
|
-
console.log(
|
|
244
|
+
console.log(import_chalk3.default.green("\u2713 All custom modules generated."));
|
|
236
245
|
}
|
|
237
|
-
console.log(
|
|
238
|
-
console.log(
|
|
246
|
+
console.log(import_chalk3.default.bold.green("\n\u{1F389} API generation complete! All files are located in:"));
|
|
247
|
+
console.log(import_chalk3.default.bold.cyan(options.output));
|
|
239
248
|
} catch (error) {
|
|
240
249
|
handleGenerationError(error);
|
|
241
250
|
}
|
|
@@ -243,17 +252,17 @@ async function runGenerator(options) {
|
|
|
243
252
|
function getSpecUrl() {
|
|
244
253
|
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);
|
|
245
254
|
if (!specUrl) {
|
|
246
|
-
console.error(
|
|
247
|
-
console.error(
|
|
255
|
+
console.error(import_chalk3.default.red.bold("\n\u274C Error: API specification URL not found."));
|
|
256
|
+
console.error(import_chalk3.default.red("Please define either OPENAPI_SPEC_URL or API_URL/NEXT_PUBLIC_API_URL in your .env file."));
|
|
248
257
|
process.exit(1);
|
|
249
258
|
}
|
|
250
259
|
return specUrl;
|
|
251
260
|
}
|
|
252
261
|
function handleGenerationError(error) {
|
|
253
|
-
console.error(
|
|
254
|
-
console.error(
|
|
262
|
+
console.error(import_chalk3.default.red.bold("\n\u274C An error occurred during generation:"));
|
|
263
|
+
console.error(import_chalk3.default.red(`Error Message: ${error.message}`));
|
|
255
264
|
if (error.stack) {
|
|
256
|
-
console.error(
|
|
265
|
+
console.error(import_chalk3.default.gray(error.stack));
|
|
257
266
|
}
|
|
258
267
|
process.exit(1);
|
|
259
268
|
}
|