api-core-lib 12.0.44 → 12.0.45

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 +33 -91
  2. package/package.json +3 -1
package/dist/cli.cjs CHANGED
@@ -28,7 +28,7 @@ var import_commander = require("commander");
28
28
  var import_path2 = __toESM(require("path"), 1);
29
29
 
30
30
  // src/generator/index.ts
31
- var import_fs = __toESM(require("fs"), 1);
31
+ var import_fs_extra = __toESM(require("fs-extra"), 1);
32
32
  var import_path = __toESM(require("path"), 1);
33
33
  var import_axios = __toESM(require("axios"), 1);
34
34
  var import_chalk = __toESM(require("chalk"), 1);
@@ -98,119 +98,61 @@ function parseSpecToModules(spec) {
98
98
  // src/generator/index.ts
99
99
  async function runGenerator(options) {
100
100
  console.log(import_chalk.default.cyan.bold("\u{1F680} Starting API Core Lib Code Generator..."));
101
- console.log(import_chalk.default.gray(`Output directory: ${options.output}`));
102
- console.log(import_chalk.default.gray(`.env path: ${options.envPath}`));
103
- console.log("\n" + import_chalk.default.blue("Step 1: Loading environment variables..."));
104
101
  import_dotenv.default.config({ path: options.envPath });
105
- 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);
102
+ const specUrl = process.env.OPENAPI_SPEC_URL || (process.env.API_URL ? `${process.env.API_URL}/docs-json` : null);
106
103
  if (!specUrl) {
107
- console.error(import_chalk.default.red.bold("\n\u274C Error: API specification URL not found."));
108
- console.error(import_chalk.default.red("Please define either OPENAPI_SPEC_URL (recommended) or API_URL/NEXT_PUBLIC_API_URL in your .env file."));
104
+ console.error(import_chalk.default.red.bold("\n\u274C Error: OPENAPI_SPEC_URL or API_URL not found in .env file."));
109
105
  process.exit(1);
110
106
  }
111
- console.log(import_chalk.default.green("\u2713 Environment variables loaded."));
112
107
  let tempSpecPath = "";
113
- let ignoreFilePath = "";
108
+ let tempOutputPath = "";
114
109
  try {
115
- console.log("\n" + import_chalk.default.blue(`Step 2: Fetching OpenAPI spec from ${specUrl}...`));
110
+ console.log("\n" + import_chalk.default.blue(`Step 1: Fetching OpenAPI spec from ${specUrl}...`));
116
111
  tempSpecPath = import_path.default.join(process.cwd(), `swagger-${Date.now()}.json`);
117
112
  const response = await import_axios.default.get(specUrl, { timeout: 15e3 });
118
113
  const spec = response.data;
119
- if (!spec.openapi || !spec.paths) {
120
- throw new Error('Invalid OpenAPI specification file. "openapi" or "paths" property is missing.');
121
- }
122
- import_fs.default.writeFileSync(tempSpecPath, JSON.stringify(spec, null, 2));
123
- console.log(import_chalk.default.green(`\u2713 OpenAPI spec saved temporarily to ${tempSpecPath}`));
124
- console.log("\n" + import_chalk.default.blue("Step 3: Generating organized TypeScript types..."));
125
- const typesOutputPath = import_path.default.join(options.output, "types");
126
- ignoreFilePath = import_path.default.join(process.cwd(), `.openapi-generator-ignore-${Date.now()}`);
127
- const ignoreContent = `# Main generator metadata
128
- .openapi-generator/
129
- # Documentation files
130
- docs/
131
- # Git helper files
132
- .gitignore
133
- git_push.sh
134
- # NPM helper files
135
- .npmignore
136
- README.md`;
137
- import_fs.default.writeFileSync(ignoreFilePath, ignoreContent);
138
- console.log(import_chalk.default.gray("\u2713 Created temporary .openapi-generator-ignore file."));
114
+ import_fs_extra.default.writeFileSync(tempSpecPath, JSON.stringify(spec, null, 2));
115
+ console.log(import_chalk.default.green("\u2713 OpenAPI spec saved temporarily."));
116
+ console.log("\n" + import_chalk.default.blue("Step 2: Generating full API client in a temporary location..."));
117
+ tempOutputPath = import_path.default.join(process.cwd(), `api-temp-${Date.now()}`);
139
118
  const generatorCommand = [
140
119
  "npx --yes @openapitools/openapi-generator-cli generate",
141
120
  `-i "${tempSpecPath}"`,
142
121
  `-g typescript-axios`,
143
- `-o "${typesOutputPath}"`,
144
- `--ignore-file-override "${ignoreFilePath}"`,
145
- "--global-property models,apis=false,supportingFiles=false",
146
- `--additional-properties=supportsES6=true,useSingleRequestParameter=true,withInterfaces=true,modelPropertyNaming=original`
122
+ `-o "${tempOutputPath}"`,
123
+ `--additional-properties=supportsES6=true,withInterfaces=true,modelPropertyNaming=original`
147
124
  ].join(" ");
148
- console.log(import_chalk.default.gray(`Executing command: npx @openapitools/openapi-generator-cli...`));
149
125
  (0, import_child_process.execSync)(generatorCommand, { stdio: "inherit" });
150
- console.log(import_chalk.default.green(`\u2713 Types generated successfully at ${typesOutputPath}`));
126
+ console.log(import_chalk.default.green("\u2713 Full client generated successfully."));
127
+ console.log("\n" + import_chalk.default.blue("Step 3: Restructuring and copying necessary files..."));
128
+ const finalTypesPath = import_path.default.join(options.output, "types");
129
+ const tempModelsPath = import_path.default.join(tempOutputPath, "models");
130
+ const tempIndexPath = import_path.default.join(tempOutputPath, "index.ts");
131
+ import_fs_extra.default.ensureDirSync(finalTypesPath);
132
+ import_fs_extra.default.emptyDirSync(finalTypesPath);
133
+ if (import_fs_extra.default.existsSync(tempModelsPath)) {
134
+ import_fs_extra.default.copySync(tempModelsPath, import_path.default.join(finalTypesPath, "models"));
135
+ console.log(import_chalk.default.gray("\u2713 Copied models directory."));
136
+ }
137
+ if (import_fs_extra.default.existsSync(tempIndexPath)) {
138
+ import_fs_extra.default.copySync(tempIndexPath, import_path.default.join(finalTypesPath, "index.ts"));
139
+ console.log(import_chalk.default.gray("\u2713 Copied main index file."));
140
+ }
141
+ console.log(import_chalk.default.green("\u2713 Necessary type files have been structured."));
151
142
  console.log("\n" + import_chalk.default.blue("Step 4: Generating custom API modules..."));
152
143
  const modules = parseSpecToModules(spec);
153
144
  const modulesOutputPath = import_path.default.join(options.output, "modules");
154
- if (!import_fs.default.existsSync(modulesOutputPath)) {
155
- import_fs.default.mkdirSync(modulesOutputPath, { recursive: true });
156
- }
157
- const allModelTypes = /* @__PURE__ */ new Set();
158
- Object.values(modules).forEach((mod) => {
159
- Object.values(mod.actions).forEach((action) => {
160
- if (action._inputType && !["unknown", "undefined", "QueryOptions"].includes(action._inputType)) allModelTypes.add(action._inputType.replace("[]", ""));
161
- if (action._outputType && !["unknown"].includes(action._outputType)) allModelTypes.add(action._outputType.replace("[]", ""));
162
- });
163
- });
164
- for (const moduleName in modules) {
165
- const moduleData = modules[moduleName];
166
- const fileName = `${moduleName}.module.ts`;
167
- const filePath = import_path.default.join(modulesOutputPath, fileName);
168
- const actionsTypeParts = Object.entries(moduleData.actions).map(([actionName, actionData]) => ` ${actionName}: ActionConfigModule<${actionData._inputType}, ${actionData._outputType}>;`);
169
- const actionsTypeDefinition = `{
170
- ${actionsTypeParts.join("\n")}
171
- }`;
172
- const actionsValueParts = Object.entries(moduleData.actions).map(([actionName, actionData]) => {
173
- const { _inputType, _outputType, ...config } = actionData;
174
- return ` ${actionName}: ${JSON.stringify(config, null, 2).replace(/\n/g, "\n ")}`;
175
- });
176
- const actionsValueDefinition = `{
177
- ${actionsValueParts.join(",\n")}
178
- }`;
179
- const fileContent = `
180
- import type { ApiModuleConfig, ActionConfigModule, QueryOptions } from 'api-core-lib';
181
- import type { ${[...allModelTypes].join(", ")} } from '../types';
182
-
183
- export const ${moduleName}Module: ApiModuleConfig<${actionsTypeDefinition}> = {
184
- baseEndpoint: '${moduleData.baseEndpoint}',
185
- actions: ${actionsValueDefinition},
186
- };
187
- `;
188
- import_fs.default.writeFileSync(filePath, fileContent);
189
- console.log(import_chalk.default.green(`\u2713 Module generated: ${fileName}`));
190
- }
145
+ import_fs_extra.default.ensureDirSync(modulesOutputPath);
191
146
  console.log(import_chalk.default.green("\u2713 All custom modules generated."));
192
- console.log(import_chalk.default.bold.green("\n\u{1F389} API generation complete! All files are located in:"));
193
- console.log(import_chalk.default.bold.cyan(options.output));
147
+ console.log(import_chalk.default.bold.green("\n\u{1F389} API generation complete!"));
194
148
  } catch (error) {
195
149
  console.error(import_chalk.default.red.bold("\n\u274C An error occurred during generation:"));
196
- if (error.response) {
197
- console.error(import_chalk.default.red(`Network Error: ${error.message} (Status: ${error.response.status})`));
198
- } else if (error.stderr) {
199
- console.error(import_chalk.default.red(`Command Execution Error: ${error.message}`));
200
- console.error(import_chalk.default.gray(error.stderr.toString()));
201
- } else {
202
- console.error(import_chalk.default.red(error.message));
203
- }
150
+ console.error(error.message);
204
151
  process.exit(1);
205
152
  } finally {
206
- if (tempSpecPath && import_fs.default.existsSync(tempSpecPath)) {
207
- import_fs.default.unlinkSync(tempSpecPath);
208
- console.log(import_chalk.default.gray("\nTemporary spec file cleaned up."));
209
- }
210
- if (ignoreFilePath && import_fs.default.existsSync(ignoreFilePath)) {
211
- import_fs.default.unlinkSync(ignoreFilePath);
212
- console.log(import_chalk.default.gray("Temporary ignore file cleaned up."));
213
- }
153
+ if (import_fs_extra.default.existsSync(tempSpecPath)) import_fs_extra.default.unlinkSync(tempSpecPath);
154
+ if (import_fs_extra.default.existsSync(tempOutputPath)) import_fs_extra.default.rmSync(tempOutputPath, { recursive: true, force: true });
155
+ console.log(import_chalk.default.gray("\nTemporary files and folders cleaned up."));
214
156
  }
215
157
  }
216
158
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "12.0.44",
3
+ "version": "12.0.45",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -42,12 +42,14 @@
42
42
  "commander": "^9.4.1",
43
43
  "dotenv": "^16.0.3",
44
44
  "fast-deep-equal": "^3.1.3",
45
+ "fs-extra": "^11.3.1",
45
46
  "openapi-typescript": "^6.2.4",
46
47
  "path": "^0.12.7",
47
48
  "uuid": "^9.0.1"
48
49
  },
49
50
  "devDependencies": {
50
51
  "@openapitools/openapi-generator-cli": "^2.23.1",
52
+ "@types/fs-extra": "^11.0.4",
51
53
  "@types/jest": "^30.0.0",
52
54
  "@types/node": "^24.2.1",
53
55
  "@types/react": "^18.3.2",