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.
Files changed (2) hide show
  1. package/dist/cli.cjs +60 -32
  2. 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
- const parts = ref.replace(/^#\/components\//, "").split("/");
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
- modules[moduleName] = { actions: {} };
57
+ const commonPath = apiPath.substring(0, apiPath.lastIndexOf("/"));
58
+ modules[moduleName] = { baseEndpoint: commonPath || "/", actions: {} };
54
59
  }
55
- const requestBodyRef = endpoint.requestBody?.content["application/json"]?.schema?.$ref;
60
+ const requestBody = endpoint.requestBody?.content?.["application/json"]?.schema;
56
61
  const successResponse = endpoint.responses["200"] || endpoint.responses["201"];
57
- const responseRef = successResponse?.content?.["application/json"]?.schema?.$ref;
58
- const actionName = endpoint.operationId || `${method}${apiPath.replace(/[\/{}]+/g, "_")}`;
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: apiPath,
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 generatorCommand = [
103
- "npx",
104
- "@openapitools/openapi-generator-cli",
105
- "generate",
106
- "-i",
107
- `"${tempSpecPath}"`,
108
- "-g",
109
- "typescript-axios",
110
- "-o",
111
- `"${typesOutputPath}"`,
112
- "--additional-properties=supportsES6=true,useSingleRequestParameter=true,withInterfaces=true,modelPropertyNaming=original"
113
- ].join(" ");
114
- console.log(import_chalk.default.gray(`Executing: ${generatorCommand}`));
115
- (0, import_child_process.execSync)(generatorCommand, { stdio: "inherit" });
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
- const inputType = actionData._inputType !== "unknown" ? actionData._inputType : actionData.hasQuery ? "QueryOptions" : "undefined";
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
- import type { components } from '../types/api-types.generated';
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: ApiModuleCodeConfig<${actionsTypeDefinition}> = {
156
- baseEndpoint: '/api/v1', // \u064A\u0645\u0643\u0646\u0643 \u062C\u0639\u0644\u0647 \u062F\u064A\u0646\u0627\u0645\u064A\u0643\u064A\u064B\u0627
183
+ export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
184
+ baseEndpoint: '${moduleData.baseEndpoint}',
157
185
  actions: ${actionsValueDefinition},
158
186
  };
159
187
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "12.0.37",
3
+ "version": "12.0.38",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {