api-core-lib 12.0.54 → 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.
Files changed (2) hide show
  1. package/dist/cli.cjs +46 -31
  2. 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 import_chalk2 = __toESM(require("chalk"), 1);
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,9 +70,23 @@ function parseSpecToModules(spec) {
69
70
  }
70
71
  return modules;
71
72
  }
72
- function getInputOutputTypes(endpoint) {
73
+ function getInputOutputTypes(endpoint, apiPath) {
73
74
  const requestBodySchema = endpoint.requestBody?.content?.["application/json"]?.schema;
74
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."));
84
+ }
85
+ if (requestBodySchema) {
86
+ console.log(import_chalk.default.blue(" Request Body Schema Structure:"));
87
+ console.log(JSON.stringify(requestBodySchema, null, 2));
88
+ }
89
+ console.log(import_chalk.default.magenta("---------------------------------------------------\n"));
75
90
  let outputType = "unknown";
76
91
  if (successResponseSchema) {
77
92
  if (successResponseSchema.$ref) {
@@ -108,10 +123,10 @@ function refToTypeName(ref) {
108
123
  // src/generator/file-generator.ts
109
124
  var import_fs = __toESM(require("fs"), 1);
110
125
  var import_path = __toESM(require("path"), 1);
111
- var import_chalk = __toESM(require("chalk"), 1);
126
+ var import_chalk2 = __toESM(require("chalk"), 1);
112
127
  var import_json_schema_to_typescript = require("json-schema-to-typescript");
113
128
  async function generateModuleFiles(moduleName, moduleData, spec, outputDir) {
114
- console.log(import_chalk.default.cyan(`
129
+ console.log(import_chalk2.default.cyan(`
115
130
  Generating module: ${moduleName}`));
116
131
  const moduleFolderPath = import_path.default.join(outputDir, moduleName);
117
132
  if (!import_fs.default.existsSync(moduleFolderPath)) {
@@ -123,9 +138,9 @@ Generating module: ${moduleName}`));
123
138
  async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec) {
124
139
  const typesCount = typeNames.size;
125
140
  if (typesCount === 0) {
126
- console.log(import_chalk.default.yellow(` - No types found for this module. types.ts will be empty.`));
141
+ console.log(import_chalk2.default.yellow(` - No types found for this module. types.ts will be empty.`));
127
142
  } else {
128
- console.log(import_chalk.default.gray(` - Found ${typesCount} types to generate for types.ts...`));
143
+ console.log(import_chalk2.default.gray(` - Found ${typesCount} types to generate for types.ts...`));
129
144
  }
130
145
  let typesContent = `// This file is auto-generated by the API generator. Do not edit.
131
146
 
@@ -150,11 +165,11 @@ async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec)
150
165
  });
151
166
  typesContent += tsType + "\n";
152
167
  } 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}`));
168
+ console.error(import_chalk2.default.red(` - Error compiling type "${typeName}" for module "${moduleName}":`));
169
+ console.error(import_chalk2.default.red(` ${compileError.message}`));
155
170
  }
156
171
  } else {
157
- console.log(import_chalk.default.yellow(` - Warning: Schema for type "${typeName}" not found in dereferenced spec, skipping.`));
172
+ console.log(import_chalk2.default.yellow(` - Warning: Schema for type "${typeName}" not found in dereferenced spec, skipping.`));
158
173
  }
159
174
  }
160
175
  const typesFilePath = import_path.default.join(moduleFolderPath, "types.ts");
@@ -163,7 +178,7 @@ async function generateTypesFile(moduleFolderPath, moduleName, typeNames, spec)
163
178
  async function generateConfigFile(moduleFolderPath, moduleName, moduleData) {
164
179
  const typeNamesArray = [...moduleData.types];
165
180
  const actionsCount = Object.keys(moduleData.actions).length;
166
- console.log(import_chalk.default.gray(` - Found ${actionsCount} actions to generate for config.ts...`));
181
+ console.log(import_chalk2.default.gray(` - Found ${actionsCount} actions to generate for config.ts...`));
167
182
  const typesImportStatement = typeNamesArray.length > 0 ? `import type { ${typeNamesArray.join(", ")} } from './types';` : ``;
168
183
  const actionsTypeParts = Object.entries(moduleData.actions).map(
169
184
  ([actionName, actionData]) => ` ${actionName}: ActionConfigModule<${actionData._inputType}, ${actionData._outputType}>;`
@@ -195,30 +210,30 @@ export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
195
210
 
196
211
  // src/generator/index.ts
197
212
  async function runGenerator(options) {
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..."));
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..."));
202
217
  import_dotenv.default.config({ path: options.envPath });
203
218
  const specUrl = getSpecUrl();
204
- console.log(import_chalk2.default.green("\u2713 Environment variables loaded."));
219
+ console.log(import_chalk3.default.green("\u2713 Environment variables loaded."));
205
220
  try {
206
- console.log("\n" + import_chalk2.default.blue(`Step 2: Fetching OpenAPI spec from ${specUrl}...`));
221
+ console.log("\n" + import_chalk3.default.blue(`Step 2: Fetching OpenAPI spec from ${specUrl}...`));
207
222
  const response = await import_axios.default.get(specUrl, { timeout: 15e3 });
208
223
  const originalSpec = response.data;
209
224
  if (!originalSpec.openapi || !originalSpec.paths) {
210
225
  throw new Error('Invalid OpenAPI specification file. "openapi" or "paths" property is missing.');
211
226
  }
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..."));
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..."));
214
229
  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..."));
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..."));
217
232
  const modules = parseSpecToModules(originalSpec);
218
233
  const modulesCount = Object.keys(modules).length;
219
- console.log(import_chalk2.default.gray(`Found ${modulesCount} modules to generate...`));
234
+ console.log(import_chalk3.default.gray(`Found ${modulesCount} modules to generate...`));
220
235
  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."));
236
+ console.log(import_chalk3.default.yellow("Warning: No modules found based on tags in the OpenAPI spec. No files will be generated."));
222
237
  }
223
238
  const modulesOutputPath = import_path2.default.join(options.output, "modules");
224
239
  for (const moduleName in modules) {
@@ -226,10 +241,10 @@ async function runGenerator(options) {
226
241
  await generateModuleFiles(moduleName, moduleData, dereferencedSpec, modulesOutputPath);
227
242
  }
228
243
  if (modulesCount > 0) {
229
- console.log(import_chalk2.default.green("\u2713 All custom modules generated."));
244
+ console.log(import_chalk3.default.green("\u2713 All custom modules generated."));
230
245
  }
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));
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));
233
248
  } catch (error) {
234
249
  handleGenerationError(error);
235
250
  }
@@ -237,17 +252,17 @@ async function runGenerator(options) {
237
252
  function getSpecUrl() {
238
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);
239
254
  if (!specUrl) {
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."));
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."));
242
257
  process.exit(1);
243
258
  }
244
259
  return specUrl;
245
260
  }
246
261
  function handleGenerationError(error) {
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}`));
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}`));
249
264
  if (error.stack) {
250
- console.error(import_chalk2.default.gray(error.stack));
265
+ console.error(import_chalk3.default.gray(error.stack));
251
266
  }
252
267
  process.exit(1);
253
268
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "12.0.54",
3
+ "version": "12.0.55",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {