api-core-lib 12.0.54 → 12.0.56

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 +61 -4
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -193,6 +193,61 @@ export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
193
193
  import_fs.default.writeFileSync(configFilePath, configContent);
194
194
  }
195
195
 
196
+ // src/generator/spec-preprocessor.ts
197
+ function toPascalCase(str) {
198
+ return str.replace(/_v\d+$/, "").split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
199
+ }
200
+ function normalizeSchema(schema, schemas, baseName, suffix) {
201
+ if (!schema) return schema;
202
+ if (schema.$ref) return schema;
203
+ if (schema.type === "object" && schema.properties) {
204
+ const typeName = `${toPascalCase(baseName)}${suffix}Dto`;
205
+ if (!schemas[typeName]) {
206
+ schemas[typeName] = schema;
207
+ }
208
+ return { $ref: `#/components/schemas/${typeName}` };
209
+ }
210
+ if (schema.type === "array" && schema.items) {
211
+ schema.items = normalizeSchema(schema.items, schemas, baseName, suffix.replace("[]", ""));
212
+ return schema;
213
+ }
214
+ return schema;
215
+ }
216
+ function preprocessSpec(spec) {
217
+ if (!spec.components) spec.components = {};
218
+ if (!spec.components.schemas) spec.components.schemas = {};
219
+ for (const apiPath in spec.paths) {
220
+ for (const method in spec.paths[apiPath]) {
221
+ const endpoint = spec.paths[apiPath][method];
222
+ const operationId = endpoint.operationId;
223
+ if (!operationId) continue;
224
+ const requestBodySchema = endpoint.requestBody?.content?.["application/json"]?.schema;
225
+ if (requestBodySchema) {
226
+ endpoint.requestBody.content["application/json"].schema = normalizeSchema(
227
+ requestBodySchema,
228
+ spec.components.schemas,
229
+ operationId,
230
+ "Request"
231
+ );
232
+ }
233
+ if (endpoint.responses) {
234
+ for (const statusCode in endpoint.responses) {
235
+ const responseSchema = endpoint.responses[statusCode]?.content?.["application/json"]?.schema;
236
+ if (responseSchema) {
237
+ endpoint.responses[statusCode].content["application/json"].schema = normalizeSchema(
238
+ responseSchema,
239
+ spec.components.schemas,
240
+ operationId,
241
+ "Response"
242
+ );
243
+ }
244
+ }
245
+ }
246
+ }
247
+ }
248
+ return spec;
249
+ }
250
+
196
251
  // src/generator/index.ts
197
252
  async function runGenerator(options) {
198
253
  console.log(import_chalk2.default.cyan.bold("\u{1F680} Starting API Core Lib Code Generator..."));
@@ -210,15 +265,18 @@ async function runGenerator(options) {
210
265
  throw new Error('Invalid OpenAPI specification file. "openapi" or "paths" property is missing.');
211
266
  }
212
267
  console.log(import_chalk2.default.green("\u2713 OpenAPI spec fetched successfully."));
268
+ console.log(import_chalk2.default.blue("Step 2.2: Preprocessing spec to normalize schemas..."));
269
+ const normalizedSpec = preprocessSpec(originalSpec);
270
+ console.log(import_chalk2.default.green("\u2713 Spec has been normalized successfully."));
213
271
  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);
272
+ const dereferencedSpec = await import_json_schema_ref_parser.default.dereference(normalizedSpec);
215
273
  console.log(import_chalk2.default.green("\u2713 Spec dereferenced successfully."));
216
274
  console.log("\n" + import_chalk2.default.blue("Step 3: Parsing spec and generating API modules..."));
217
- const modules = parseSpecToModules(originalSpec);
275
+ const modules = parseSpecToModules(normalizedSpec);
218
276
  const modulesCount = Object.keys(modules).length;
219
277
  console.log(import_chalk2.default.gray(`Found ${modulesCount} modules to generate...`));
220
278
  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."));
279
+ console.log(import_chalk2.default.yellow("Warning: No modules found."));
222
280
  }
223
281
  const modulesOutputPath = import_path2.default.join(options.output, "modules");
224
282
  for (const moduleName in modules) {
@@ -238,7 +296,6 @@ function getSpecUrl() {
238
296
  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
297
  if (!specUrl) {
240
298
  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."));
242
299
  process.exit(1);
243
300
  }
244
301
  return specUrl;
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.56",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {