@vc-shell/api-client-generator 1.1.7 → 1.1.9
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/CHANGELOG.md +13 -0
- package/README.md +198 -83
- package/dist/api-client-generator.js +280 -150
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
## [1.1.9](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.8...v1.1.9) (2025-05-15)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## [1.1.8](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.7...v1.1.8) (2025-05-15)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **generate-api-client:** enhance API client generation with new features and improvements ([fcf85db](https://github.com/VirtoCommerce/vc-shell/commit/fcf85db675390abc218b4cd1fa3f204be9a50da3))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
1
14
|
## [1.1.7](https://github.com/VirtoCommerce/vc-shell/compare/v1.1.6...v1.1.7) (2025-05-12)
|
|
2
15
|
|
|
3
16
|
|
package/README.md
CHANGED
|
@@ -1,83 +1,198 @@
|
|
|
1
|
-
# Platform Manager REST Client
|
|
2
|
-
|
|
3
|
-
# Generate API client
|
|
4
|
-
|
|
5
|
-
This guide describes the process of generating an API client to access the VC Platform API from your custom application.
|
|
6
|
-
|
|
7
|
-
!!! note
|
|
8
|
-
Platform Manager REST Client offers generated REST API methods that make it easy to interact with the existing VirtoCommerce Platform API.
|
|
9
|
-
|
|
10
|
-
## Prerequisites
|
|
11
|
-
|
|
12
|
-
* .NET Core 6.0, particularly if you are using MacOS or Linux.
|
|
13
|
-
|
|
14
|
-
## Generate TypeScript API clients
|
|
15
|
-
|
|
16
|
-
To enable TypeScript API client generation in your project:
|
|
17
|
-
|
|
18
|
-
1. Add dependencies to your project:
|
|
19
|
-
|
|
20
|
-
Using command
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
yarn add @vc-shell/api-client-generator cross-env
|
|
24
|
-
```
|
|
25
|
-
<br>
|
|
26
|
-
|
|
27
|
-
`cross-env` runs scripts that set and use environment variables across platforms.
|
|
28
|
-
|
|
29
|
-
Manually
|
|
30
|
-
|
|
31
|
-
Add the dependencies to your project's **package.json**:
|
|
32
|
-
|
|
33
|
-
```json title="vc-app/package.json" linenums="1"
|
|
34
|
-
{
|
|
35
|
-
...
|
|
36
|
-
"devDependencies": {
|
|
37
|
-
...
|
|
38
|
-
"@vc-shell/api-client-generator": "latest",
|
|
39
|
-
"cross-env": "latest",
|
|
40
|
-
...
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
2. Configure client generation in your project. Inside your project's **package.json** file, add a `"generate-api-client"` command to the list of scripts:
|
|
46
|
-
|
|
47
|
-
```title="vc-app-extend/package.json" linenums="1"
|
|
48
|
-
{
|
|
49
|
-
"scripts": {
|
|
50
|
-
...
|
|
51
|
-
"generate-api-client": cross-env api-client-generator --APP_PLATFORM_MODULES='[MarketplaceVendor,Catalog,Orders]' --APP_API_CLIENT_DIRECTORY=./src/api_client/
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
The options are listed in the table below:
|
|
57
|
-
|
|
58
|
-
| Options | Description | Example |
|
|
59
|
-
|----------------------------- |---------------------------------------------------------------- |------------------------------------------------------------ |
|
|
60
|
-
| `--APP_PLATFORM_MODULES` | Platform modules to generate API client.<br>{==string[]==} <br> Customize the `--APP_PLATFORM_MODULES` list<br>to match your project's requirements. | `--APP_PLATFORM_MODULES='[MarketplaceVendor,Orders,Catalog]'` |
|
|
61
|
-
| `--APP_API_CLIENT_DIRECTORY` | Output directory for generated API clients. <br>{==string==} | `--APP_API_CLIENT_DIRECTORY=./src/api_client/` |
|
|
62
|
-
| `--APP_PLATFORM_URL` | Platform URL to obtain client API configs. <br>{==string==} | `--APP_PLATFORM_URL=https://vcmp-dev.govirto.com/` |
|
|
63
|
-
| `--APP_PACKAGE_NAME` | Package name for generated API clients. <br>{==string==} | `--APP_PACKAGE_NAME
|
|
64
|
-
| `--APP_PACKAGE_VERSION` | Package version for generated API clients. <br>{==string==} | `--APP_PACKAGE_VERSION=1.
|
|
65
|
-
| `--APP_OUT_DIR` | Output directory for generated API clients. <br>{==string==} | `--APP_OUT_DIR=./src/api_client/` |
|
|
66
|
-
| `--
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
|
|
1
|
+
# Platform Manager REST Client
|
|
2
|
+
|
|
3
|
+
# Generate API client
|
|
4
|
+
|
|
5
|
+
This guide describes the process of generating an API client to access the VC Platform API from your custom application.
|
|
6
|
+
|
|
7
|
+
!!! note
|
|
8
|
+
Platform Manager REST Client offers generated REST API methods that make it easy to interact with the existing VirtoCommerce Platform API.
|
|
9
|
+
|
|
10
|
+
## Prerequisites
|
|
11
|
+
|
|
12
|
+
* .NET Core 6.0, particularly if you are using MacOS or Linux.
|
|
13
|
+
|
|
14
|
+
## Generate TypeScript API clients
|
|
15
|
+
|
|
16
|
+
To enable TypeScript API client generation in your project:
|
|
17
|
+
|
|
18
|
+
1. Add dependencies to your project:
|
|
19
|
+
|
|
20
|
+
Using command
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
yarn add @vc-shell/api-client-generator cross-env
|
|
24
|
+
```
|
|
25
|
+
<br>
|
|
26
|
+
|
|
27
|
+
`cross-env` runs scripts that set and use environment variables across platforms.
|
|
28
|
+
|
|
29
|
+
Manually
|
|
30
|
+
|
|
31
|
+
Add the dependencies to your project's **package.json**:
|
|
32
|
+
|
|
33
|
+
```json title="vc-app/package.json" linenums="1"
|
|
34
|
+
{
|
|
35
|
+
...
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
...
|
|
38
|
+
"@vc-shell/api-client-generator": "latest",
|
|
39
|
+
"cross-env": "latest",
|
|
40
|
+
...
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
2. Configure client generation in your project. Inside your project's **package.json** file, add a `"generate-api-client"` command to the list of scripts:
|
|
46
|
+
|
|
47
|
+
```title="vc-app-extend/package.json" linenums="1"
|
|
48
|
+
{
|
|
49
|
+
"scripts": {
|
|
50
|
+
...
|
|
51
|
+
"generate-api-client": cross-env api-client-generator --APP_PLATFORM_MODULES='[Virtocommerce.MarketplaceVendor,Virtocommerce.Catalog,Virtocommerce.Orders]' --APP_API_CLIENT_DIRECTORY=./src/api_client/
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The options are listed in the table below:
|
|
57
|
+
|
|
58
|
+
| Options | Description | Example |
|
|
59
|
+
|----------------------------- |---------------------------------------------------------------- |------------------------------------------------------------ |
|
|
60
|
+
| `--APP_PLATFORM_MODULES` | Platform modules to generate API client.<br>{==string[]==} <br> Customize the `--APP_PLATFORM_MODULES` list<br>to match your project's requirements. | `--APP_PLATFORM_MODULES='[Virtocommerce.MarketplaceVendor,Virtocommerce.Orders,Virtocommerce.Catalog]'` |
|
|
61
|
+
| `--APP_API_CLIENT_DIRECTORY` | Output directory for generated API clients. <br>{==string==} | `--APP_API_CLIENT_DIRECTORY=./src/api_client/` |
|
|
62
|
+
| `--APP_PLATFORM_URL` | Platform URL to obtain client API configs. <br>{==string==} | `--APP_PLATFORM_URL=https://vcmp-dev.govirto.com/` |
|
|
63
|
+
| `--APP_PACKAGE_NAME` | Package name for generated API clients. <br>{==string==} | `--APP_PACKAGE_NAME=@api-client` |
|
|
64
|
+
| `--APP_PACKAGE_VERSION` | Package version for generated API clients. <br>{==string==} | `--APP_PACKAGE_VERSION=1.1.0` |
|
|
65
|
+
| `--APP_OUT_DIR` | Output directory for generated API clients. <br>{==string==} | `--APP_OUT_DIR=./src/api_client/` |
|
|
66
|
+
| `--APP_BUILD_DIR` | Directory where TypeScript files will be compiled. <br>{==string==} | `--APP_BUILD_DIR=lib` (default is "dist") |
|
|
67
|
+
| `--SKIP_BUILD` | Skip build step. <br>{==boolean==} | `--SKIP_BUILD=true` |
|
|
68
|
+
| `--VERBOSE` | Enable verbose logging. <br>{==boolean==} | `--VERBOSE=true` |
|
|
69
|
+
|
|
70
|
+
3. Configure Platform URL to ensure your project can access the platform's API configurations. Add the platform URL to your project's **.env** file:
|
|
71
|
+
|
|
72
|
+
```title="vc-app-extend/.env"
|
|
73
|
+
APP_PLATFORM_URL=https://vcmp-dev.govirto.com/
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
!!! note
|
|
77
|
+
Alternatively, you can specify the Platform URL as a command option in the previous step when running the `"generate-api-client"` command.
|
|
78
|
+
|
|
79
|
+
4. Generate the API clients using the following command:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
yarn generate-api-client
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This command generates the required API clients for your custom application. Now you can effortlessly access the VC Platform API from your custom application using the generated API client.
|
|
86
|
+
|
|
87
|
+
## Features
|
|
88
|
+
|
|
89
|
+
### Smart Configuration Merging
|
|
90
|
+
|
|
91
|
+
The generator now intelligently merges configuration files to preserve your custom settings. When you update an existing API client:
|
|
92
|
+
|
|
93
|
+
- Custom package.json fields like name, version, description, keywords, author and license are preserved
|
|
94
|
+
- Custom tsconfig.json settings are maintained
|
|
95
|
+
- Exports are intelligently updated to preserve existing paths
|
|
96
|
+
|
|
97
|
+
### Metadata Tracking
|
|
98
|
+
|
|
99
|
+
API client now includes metadata to track the generation:
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"apiClientGenerator": {
|
|
104
|
+
"version": "1.1.0",
|
|
105
|
+
"generatedAt": "2025-04-01T12:30:45.123Z"
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Multiple API Exports
|
|
111
|
+
|
|
112
|
+
The generator handles multiple API clients effectively:
|
|
113
|
+
|
|
114
|
+
- Creates standardized exports with both short names (`./{moduleName}`) and full names (`./virtocommerce.{moduleName}`)
|
|
115
|
+
- Prevents duplicate module exports by intelligently merging with existing ones
|
|
116
|
+
- Automatically removes `module` and `types` fields that conflict with multiple exports
|
|
117
|
+
- Works properly with incremental generation (can add new APIs without breaking existing ones)
|
|
118
|
+
|
|
119
|
+
### Smart Root Export Handling
|
|
120
|
+
|
|
121
|
+
The generator now intelligently handles root exports based on the number of API modules:
|
|
122
|
+
|
|
123
|
+
- **Single API Module**: When only one API module is generated, it automatically becomes the root export (`.`) and `module`/`types` fields are maintained
|
|
124
|
+
- **Multiple API Modules**: When multiple API modules are generated, only subpath exports are used (e.g., `./moduleA`, `./moduleB`)
|
|
125
|
+
- **Preserving Preferences**: If a root export already exists, it will be preserved as long as it points to one of the generated modules
|
|
126
|
+
|
|
127
|
+
This enables both simple usage for single-API packages and proper subpath exporting for multi-API packages:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// Single API package (using root export)
|
|
131
|
+
import { MyClient } from '@myapp/api';
|
|
132
|
+
|
|
133
|
+
// Multi-API package (using subpath exports)
|
|
134
|
+
import { ClientA } from '@myapp/api/moduleA';
|
|
135
|
+
import { ClientB } from '@myapp/api/moduleB';
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Directory Creation
|
|
139
|
+
|
|
140
|
+
Target directories are automatically created if they don't exist.
|
|
141
|
+
|
|
142
|
+
### Verbose Logging
|
|
143
|
+
|
|
144
|
+
Enable detailed logging to troubleshoot generation issues:
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
yarn generate-api-client --VERBOSE=true
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Custom Build Directory
|
|
151
|
+
|
|
152
|
+
You can specify a custom build directory where TypeScript files will be compiled:
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
yarn generate-api-client --APP_BUILD_DIR=lib
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
By default, the build directory is "dist".
|
|
159
|
+
|
|
160
|
+
### Error Handling Improvements
|
|
161
|
+
|
|
162
|
+
Better error handling and reporting make it easier to diagnose issues during client generation.
|
|
163
|
+
|
|
164
|
+
## Recent Improvements (v1.1.8)
|
|
165
|
+
|
|
166
|
+
### Fixed Issues
|
|
167
|
+
|
|
168
|
+
- **Configuration Preservation**: Fixed issue where package.json and tsconfig.json user settings were being overwritten
|
|
169
|
+
- **Export Path Management**: Improved export path handling to prevent incorrect paths with duplicated prefixes
|
|
170
|
+
- **Path Normalization**: Added better path normalization to handle edge cases correctly
|
|
171
|
+
- **Export Deduplication**: Implemented smarter export merging to prevent duplicate exports with different path formats
|
|
172
|
+
- **Build Directory Support**: Added proper support for custom build directories via APP_BUILD_DIR parameter
|
|
173
|
+
- **Declaration Files Location**: Fixed issue with TypeScript declaration files location to match the build directory
|
|
174
|
+
- **Error Handling**: Improved JSON parsing error handling with better fallback strategies
|
|
175
|
+
- **Root Export Handling**: Fixed issue with root exports being ignored or improperly handled
|
|
176
|
+
|
|
177
|
+
### New Features
|
|
178
|
+
|
|
179
|
+
- **Intelligent Config Merging**: Complete rewrite of configuration merging logic to better preserve user settings
|
|
180
|
+
- **Export Path Standardization**: New system for standardizing export paths to improve consistency
|
|
181
|
+
- **Path Validation**: Added strict validation of export paths to prevent invalid configurations
|
|
182
|
+
- **Improved Logging**: Added more detailed logging for better debugging
|
|
183
|
+
- **Extended User Fields Preservation**: Expanded the list of user-defined fields that are preserved during updates
|
|
184
|
+
- **Better Directory Management**: Improved directory creation and validation process
|
|
185
|
+
- **Smart Single/Multi API Detection**: Automatically detects single API scenarios and creates appropriate root exports
|
|
186
|
+
- **Module/Types Field Handling**: Preserves module and types fields for single API packages while removing them for multi-API packages
|
|
187
|
+
|
|
188
|
+
## Troubleshooting
|
|
189
|
+
|
|
190
|
+
If you encounter issues during API client generation:
|
|
191
|
+
|
|
192
|
+
1. Enable verbose logging with `--VERBOSE=true`
|
|
193
|
+
2. Ensure target directories have proper permissions
|
|
194
|
+
3. Check your connectivity to the platform URL
|
|
195
|
+
4. Verify that the specified platform modules exist
|
|
196
|
+
5. If you encounter JSON parsing errors in tsconfig.json or package.json, try using the `--VERBOSE=true` flag to see detailed error messages
|
|
197
|
+
6. Check for duplicate exports in your package.json which might cause conflicts
|
|
198
|
+
7. If you manually modified exports in package.json, ensure they follow the correct format
|
|
@@ -1,186 +1,316 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { sync as
|
|
3
|
-
import { resolveConfig as
|
|
4
|
-
import { existsSync as
|
|
5
|
-
import
|
|
6
|
-
import { fileURLToPath as
|
|
7
|
-
import { cwd as
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import e from "chalk";
|
|
2
|
+
import { sync as D } from "cross-spawn";
|
|
3
|
+
import { resolveConfig as j } from "vite";
|
|
4
|
+
import { existsSync as w, mkdirSync as I, writeFileSync as R, readFileSync as O } from "node:fs";
|
|
5
|
+
import h, { dirname as B, resolve as E, join as A, relative as C } from "node:path";
|
|
6
|
+
import { fileURLToPath as b } from "node:url";
|
|
7
|
+
import { cwd as x } from "node:process";
|
|
8
|
+
import L from "mri";
|
|
9
|
+
const S = b(import.meta.url), N = B(S);
|
|
10
|
+
class k {
|
|
10
11
|
workingDirectory;
|
|
11
12
|
generatorDirectory;
|
|
12
13
|
assetsDirectory;
|
|
13
14
|
apiClientDirectory;
|
|
14
15
|
nswagPaths;
|
|
15
|
-
constructor(
|
|
16
|
-
this.workingDirectory =
|
|
17
|
-
configuration:
|
|
16
|
+
constructor(r) {
|
|
17
|
+
this.workingDirectory = x(), this.generatorDirectory = E(N, ".."), this.assetsDirectory = A(this.generatorDirectory, "public", "assets"), this.apiClientDirectory = E(this.workingDirectory, r), this.nswagPaths = {
|
|
18
|
+
configuration: A(C(this.workingDirectory, this.assetsDirectory), "config.nswag"),
|
|
18
19
|
authApiBase: "authApiBase.ts",
|
|
19
20
|
templates: "templates"
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
|
-
resolveApiClientPaths(
|
|
23
|
-
const
|
|
23
|
+
resolveApiClientPaths(r) {
|
|
24
|
+
const p = `${r.toLowerCase()}.ts`;
|
|
24
25
|
return {
|
|
25
|
-
fileName:
|
|
26
|
-
nswag:
|
|
27
|
-
console:
|
|
26
|
+
fileName: p,
|
|
27
|
+
nswag: A(C(this.assetsDirectory, this.apiClientDirectory), p),
|
|
28
|
+
console: A(C(this.workingDirectory, this.apiClientDirectory), p)
|
|
28
29
|
};
|
|
29
30
|
}
|
|
30
31
|
}
|
|
32
|
+
function T(t, r, p = []) {
|
|
33
|
+
const n = { ...t };
|
|
34
|
+
for (const a of p)
|
|
35
|
+
a in r && (n[a] = r[a]);
|
|
36
|
+
for (const [a, i] of Object.entries(r)) {
|
|
37
|
+
const l = a;
|
|
38
|
+
a in n && typeof n[l] == "object" && !Array.isArray(n[l]) && typeof i == "object" && !Array.isArray(i) && i !== null ? n[l] = { ...n[l], ...i } : p.includes(a) || (n[l] = i);
|
|
39
|
+
}
|
|
40
|
+
return n;
|
|
41
|
+
}
|
|
42
|
+
function M(t, r, p, n) {
|
|
43
|
+
const a = {
|
|
44
|
+
extends: "@vc-shell/ts-config/tsconfig.json",
|
|
45
|
+
compilerOptions: {
|
|
46
|
+
baseUrl: ".",
|
|
47
|
+
declarationDir: h.join(p, "types"),
|
|
48
|
+
outDir: n,
|
|
49
|
+
rootDir: "./"
|
|
50
|
+
},
|
|
51
|
+
files: [],
|
|
52
|
+
include: ["*.ts"]
|
|
53
|
+
};
|
|
54
|
+
let i = { ...a };
|
|
55
|
+
if (w(t))
|
|
56
|
+
try {
|
|
57
|
+
const l = O(t, "utf-8");
|
|
58
|
+
try {
|
|
59
|
+
const o = JSON.parse(l);
|
|
60
|
+
i = T(a, o), o.compilerOptions && typeof o.compilerOptions == "object" && (i.compilerOptions = {
|
|
61
|
+
...a.compilerOptions,
|
|
62
|
+
...o.compilerOptions
|
|
63
|
+
}, i.compilerOptions.outDir = n, i.compilerOptions.declarationDir = h.join(n, "types"));
|
|
64
|
+
const s = Array.isArray(o.files) ? o.files : [];
|
|
65
|
+
i.files = Array.from(/* @__PURE__ */ new Set([...r, ...s])), console.log("api-client-generator %s Updated existing tsconfig.json", e.greenBright("success"));
|
|
66
|
+
} catch (o) {
|
|
67
|
+
console.error(
|
|
68
|
+
"api-client-generator %s Failed to parse existing tsconfig.json, creating new one",
|
|
69
|
+
e.yellow("warning"),
|
|
70
|
+
o
|
|
71
|
+
), i = { ...a }, i.files = r;
|
|
72
|
+
}
|
|
73
|
+
} catch (l) {
|
|
74
|
+
console.error("api-client-generator %s Failed to read existing tsconfig.json", e.red("error"), l);
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
i.files = r, console.log("api-client-generator %s Created new tsconfig.json", e.greenBright("success"));
|
|
78
|
+
return i;
|
|
79
|
+
}
|
|
80
|
+
function _(t) {
|
|
81
|
+
return t = t.replace(/\.(js|ts)$/i, ""), t = t.replace(/\/\.\//g, "/"), t !== "." && !t.startsWith("./") && (t = "./" + t), t = t.replace(/\/+/g, "/"), t = t.replace(/\.\.\//g, ""), t;
|
|
82
|
+
}
|
|
31
83
|
function y(t) {
|
|
32
|
-
|
|
84
|
+
const r = _(t);
|
|
85
|
+
return r === "." ? "" : r.replace(/^\.\//, "").toLowerCase();
|
|
33
86
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
87
|
+
function v(t) {
|
|
88
|
+
const r = y(t);
|
|
89
|
+
return r ? [`./${r}`] : [];
|
|
37
90
|
}
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
91
|
+
function F(t, r, p, n) {
|
|
92
|
+
const a = {}, i = /* @__PURE__ */ new Set(), l = [];
|
|
93
|
+
for (const o of t) {
|
|
94
|
+
const s = y(o);
|
|
95
|
+
if (!s) continue;
|
|
96
|
+
l.includes(s) || l.push(s);
|
|
97
|
+
const c = `./${r}/${s}.js`, g = `./${r}/types/${s}.d.ts`, f = {
|
|
98
|
+
import: c,
|
|
99
|
+
types: g
|
|
100
|
+
}, m = v(s);
|
|
101
|
+
for (const P of m)
|
|
102
|
+
a[P] = f, i.add(s), n && console.log(
|
|
103
|
+
"api-client-generator %s Created export: %s -> %s, %s",
|
|
104
|
+
e.blue("debug"),
|
|
105
|
+
e.whiteBright(P),
|
|
106
|
+
e.whiteBright(c),
|
|
107
|
+
e.whiteBright(g)
|
|
108
|
+
);
|
|
55
109
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (a === 0)
|
|
65
|
-
o._.push(c);
|
|
66
|
-
else if (c.substring(a, a + 3) === "no-") {
|
|
67
|
-
if (s = c.substring(a + 3), h && !~d.indexOf(s))
|
|
68
|
-
return e.unknown(c);
|
|
69
|
-
o[s] = !1;
|
|
70
|
-
} else {
|
|
71
|
-
for (l = a + 1; l < c.length && c.charCodeAt(l) !== 61; l++)
|
|
72
|
-
;
|
|
73
|
-
for (s = c.substring(a, l), u = c.substring(++l) || r + 1 === f || ("" + t[r + 1]).charCodeAt(0) === 45 || t[++r], n = a === 2 ? [s] : s, l = 0; l < n.length; l++) {
|
|
74
|
-
if (s = n[l], h && !~d.indexOf(s)) return e.unknown("-".repeat(a) + s);
|
|
75
|
-
$(o, s, l + 1 < n.length || u, e);
|
|
110
|
+
if (p)
|
|
111
|
+
for (const [o, s] of Object.entries(p)) {
|
|
112
|
+
if (o === ".") {
|
|
113
|
+
if (typeof s == "object" && s !== null) {
|
|
114
|
+
const f = s;
|
|
115
|
+
f.import && f.types && n && console.log("api-client-generator %s Found existing root export", e.blue("debug"));
|
|
116
|
+
}
|
|
117
|
+
continue;
|
|
76
118
|
}
|
|
119
|
+
const c = y(o);
|
|
120
|
+
if (!c || i.has(c)) continue;
|
|
121
|
+
const g = _(o);
|
|
122
|
+
if (typeof s == "object" && s !== null) {
|
|
123
|
+
const f = s;
|
|
124
|
+
f.import && f.types && (a[g] = {
|
|
125
|
+
import: _(f.import) + ".js",
|
|
126
|
+
types: _(f.types) + ".d.ts"
|
|
127
|
+
}, i.add(c), l.includes(c) || l.push(c), n && console.log(
|
|
128
|
+
"api-client-generator %s Preserved custom export: %s",
|
|
129
|
+
e.blue("debug"),
|
|
130
|
+
e.whiteBright(g)
|
|
131
|
+
));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (l.length === 1) {
|
|
135
|
+
const o = l[0], s = `./${r}/${o}.js`, c = `./${r}/types/${o}.d.ts`;
|
|
136
|
+
a["."] = {
|
|
137
|
+
import: s,
|
|
138
|
+
types: c
|
|
139
|
+
}, n && console.log(
|
|
140
|
+
"api-client-generator %s Created root export for single module: %s",
|
|
141
|
+
e.blue("debug"),
|
|
142
|
+
e.whiteBright(o)
|
|
143
|
+
);
|
|
144
|
+
} else if (p && "." in p) {
|
|
145
|
+
const o = p["."];
|
|
146
|
+
if (o && typeof o == "object" && o.import) {
|
|
147
|
+
const s = y(o.import);
|
|
148
|
+
if (l.includes(s)) {
|
|
149
|
+
const c = `./${r}/${s}.js`, g = `./${r}/types/${s}.d.ts`;
|
|
150
|
+
a["."] = {
|
|
151
|
+
import: c,
|
|
152
|
+
types: g
|
|
153
|
+
}, n && console.log(
|
|
154
|
+
"api-client-generator %s Preserved existing root export pointing to module: %s",
|
|
155
|
+
e.blue("debug"),
|
|
156
|
+
e.whiteBright(s)
|
|
157
|
+
);
|
|
158
|
+
} else n && console.log(
|
|
159
|
+
"api-client-generator %s Removed root export that pointed to non-existing module",
|
|
160
|
+
e.yellow("warning")
|
|
161
|
+
);
|
|
77
162
|
}
|
|
78
163
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
164
|
+
return a;
|
|
165
|
+
}
|
|
166
|
+
function $(t, r, p) {
|
|
167
|
+
const n = p.VERBOSE ?? !1, a = p.APP_BUILD_DIR ?? "dist", i = [];
|
|
168
|
+
a && i.push(a), i.push("package.json");
|
|
169
|
+
const l = {
|
|
170
|
+
name: p.APP_PACKAGE_NAME || "api-client",
|
|
171
|
+
version: p.APP_PACKAGE_VERSION || "1.0.0",
|
|
172
|
+
files: i,
|
|
173
|
+
exports: {},
|
|
174
|
+
// Add metadata to track generated content
|
|
175
|
+
apiClientGenerator: {
|
|
176
|
+
version: process.env.npm_package_version || "unknown",
|
|
177
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
let o = { ...l }, s, c, g;
|
|
181
|
+
if (w(t))
|
|
182
|
+
try {
|
|
183
|
+
const u = O(t, "utf-8");
|
|
184
|
+
try {
|
|
185
|
+
const d = JSON.parse(u);
|
|
186
|
+
typeof d.module == "string" && (c = d.module), typeof d.types == "string" && (g = d.types), o = T(l, d, [
|
|
187
|
+
"name",
|
|
188
|
+
"version",
|
|
189
|
+
"description",
|
|
190
|
+
"keywords",
|
|
191
|
+
"author",
|
|
192
|
+
"license",
|
|
193
|
+
"private",
|
|
194
|
+
"scripts",
|
|
195
|
+
"devDependencies",
|
|
196
|
+
"dependencies"
|
|
197
|
+
]), d.files && Array.isArray(d.files) && (o.files = Array.from(/* @__PURE__ */ new Set([...i, ...d.files]))), s = d.exports, console.log("api-client-generator %s Updated existing package.json", e.greenBright("success"));
|
|
198
|
+
} catch (d) {
|
|
199
|
+
console.error(
|
|
200
|
+
"api-client-generator %s Failed to parse existing package.json, creating new one",
|
|
201
|
+
e.yellow("warning"),
|
|
202
|
+
d
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
} catch (u) {
|
|
206
|
+
console.error("api-client-generator %s Failed to read existing package.json", e.red("error"), u);
|
|
207
|
+
}
|
|
208
|
+
else
|
|
209
|
+
console.log("api-client-generator %s Creating new package.json", e.greenBright("success"));
|
|
210
|
+
o.exports = F(r, a, s, n);
|
|
211
|
+
const f = r.map(y).filter(Boolean);
|
|
212
|
+
if (Array.from(new Set(f)).length === 1 && o.exports && "." in o.exports) {
|
|
213
|
+
const u = o.exports["."];
|
|
214
|
+
u && u.import && (o.module = u.import, n && console.log(
|
|
215
|
+
"api-client-generator %s Set module field to: %s",
|
|
216
|
+
e.blue("debug"),
|
|
217
|
+
e.whiteBright(u.import)
|
|
218
|
+
)), u && u.types && (o.types = u.types, n && console.log(
|
|
219
|
+
"api-client-generator %s Set types field to: %s",
|
|
220
|
+
e.blue("debug"),
|
|
221
|
+
e.whiteBright(u.types)
|
|
222
|
+
));
|
|
223
|
+
} else
|
|
224
|
+
"module" in o && (delete o.module, n && console.log(
|
|
225
|
+
"api-client-generator %s Removed 'module' field from package.json as it conflicts with subpath exports",
|
|
226
|
+
e.blue("debug")
|
|
227
|
+
)), "types" in o && (delete o.types, n && console.log(
|
|
228
|
+
"api-client-generator %s Removed 'types' field from package.json as it conflicts with subpath exports",
|
|
229
|
+
e.blue("debug")
|
|
230
|
+
));
|
|
86
231
|
return o;
|
|
87
232
|
}
|
|
88
|
-
async function
|
|
89
|
-
await
|
|
90
|
-
const t =
|
|
91
|
-
if (!
|
|
233
|
+
async function U() {
|
|
234
|
+
await j({}, "build");
|
|
235
|
+
const t = L(process.argv.slice(2)), r = process.env.APP_PLATFORM_URL ?? t.APP_PLATFORM_URL, p = t.VERBOSE ?? !1;
|
|
236
|
+
if (!r)
|
|
92
237
|
return console.log(
|
|
93
|
-
|
|
238
|
+
e.red("error"),
|
|
94
239
|
"api-client-generator APP_PLATFORM_URL is required in .env config or as api-client-generator argument"
|
|
95
240
|
);
|
|
96
241
|
if (!t.APP_PLATFORM_MODULES)
|
|
97
|
-
return console.log(
|
|
242
|
+
return console.log(e.red("error"), "api-client-generator modules command is required");
|
|
98
243
|
if (!t.APP_API_CLIENT_DIRECTORY)
|
|
99
|
-
return console.log(
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
244
|
+
return console.log(e.red("error"), "api-client-generator outDir command is required");
|
|
245
|
+
const n = t.APP_OUT_DIR ?? "./", a = t.APP_BUILD_DIR ?? "dist", i = new k(t.APP_API_CLIENT_DIRECTORY);
|
|
246
|
+
if (!w(t.APP_API_CLIENT_DIRECTORY))
|
|
247
|
+
try {
|
|
248
|
+
I(t.APP_API_CLIENT_DIRECTORY, { recursive: !0 }), console.log(
|
|
249
|
+
"api-client-generator %s Created directory %s",
|
|
250
|
+
e.greenBright("success"),
|
|
251
|
+
e.whiteBright(t.APP_API_CLIENT_DIRECTORY)
|
|
252
|
+
);
|
|
253
|
+
} catch (c) {
|
|
254
|
+
console.error(
|
|
255
|
+
"api-client-generator %s Failed to create directory %s",
|
|
256
|
+
e.red("error"),
|
|
257
|
+
e.whiteBright(t.APP_API_CLIENT_DIRECTORY),
|
|
258
|
+
c
|
|
259
|
+
);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const l = h.join(t.APP_API_CLIENT_DIRECTORY, a);
|
|
263
|
+
if (!w(l))
|
|
264
|
+
try {
|
|
265
|
+
I(l, { recursive: !0 }), console.log(
|
|
266
|
+
"api-client-generator %s Created build directory %s",
|
|
267
|
+
e.greenBright("success"),
|
|
268
|
+
e.whiteBright(l)
|
|
269
|
+
);
|
|
270
|
+
} catch (c) {
|
|
271
|
+
console.error(
|
|
272
|
+
"api-client-generator %s Failed to create build directory %s",
|
|
273
|
+
e.red("error"),
|
|
274
|
+
e.whiteBright(l),
|
|
275
|
+
c
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
const o = t.APP_PLATFORM_MODULES.replace(/[[\]]/g, "").split(","), s = [];
|
|
279
|
+
for (const c of o) {
|
|
280
|
+
const g = i.resolveApiClientPaths(c);
|
|
118
281
|
console.log(
|
|
119
282
|
"api-client-generator %s Generating API client for %s module on %s environment",
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
283
|
+
e.green("info"),
|
|
284
|
+
e.whiteBright(c),
|
|
285
|
+
e.whiteBright(r)
|
|
123
286
|
);
|
|
124
|
-
const
|
|
125
|
-
"
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
],
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
287
|
+
const f = [
|
|
288
|
+
"run",
|
|
289
|
+
i.nswagPaths.configuration,
|
|
290
|
+
`/variables:APP_PLATFORM_URL=${r},APP_PLATFORM_MODULE=${c},APP_AUTH_API_BASE_PATH=${i.nswagPaths.authApiBase},APP_TEMPLATE_DIRECTORY=${i.nswagPaths.templates},APP_API_CLIENT_PATH=${g.nswag}`,
|
|
291
|
+
"/runtime:Net60"
|
|
292
|
+
];
|
|
293
|
+
p && console.log("api-client-generator %s Running command: npx nswag %s", e.blue("debug"), f.join(" ")), D("npx nswag", f, {
|
|
294
|
+
stdio: ["ignore", "inherit", "ignore"],
|
|
295
|
+
shell: !0
|
|
296
|
+
}).status === 0 ? (console.log(
|
|
297
|
+
"api-client-generator %s Successfully generated %s",
|
|
298
|
+
e.greenBright("success"),
|
|
299
|
+
e.whiteBright(g.console)
|
|
300
|
+
), t.SKIP_BUILD || s.push(`${c.toLowerCase()}.ts`)) : console.error(
|
|
301
|
+
"api-client-generator %s Failed to generate %s",
|
|
302
|
+
e.red("error"),
|
|
303
|
+
e.whiteBright(g.console)
|
|
136
304
|
);
|
|
137
|
-
if (l.status === 0) {
|
|
138
|
-
if (console.log(
|
|
139
|
-
"api-client-generator %s Successfully generated %s",
|
|
140
|
-
g.greenBright("success"),
|
|
141
|
-
g.whiteBright(a.console)
|
|
142
|
-
), !t.SKIP_BUILD && o) {
|
|
143
|
-
const f = r.toLowerCase();
|
|
144
|
-
o.files.includes(`${f}.ts`) || o.files.push(`${f}.ts`);
|
|
145
|
-
const P = {
|
|
146
|
-
import: i !== "./" ? `./${i}/${f}.js` : `./${f}.js`,
|
|
147
|
-
types: i !== "./" ? `./${i}/types/${f}.d.ts` : `./types/${f}.d.ts`
|
|
148
|
-
}, h = Object.keys(s).find(
|
|
149
|
-
(p) => s[p].import.includes(`${f}.js`)
|
|
150
|
-
);
|
|
151
|
-
h ? s[h] = P : s[`./${f}`] = P;
|
|
152
|
-
}
|
|
153
|
-
} else
|
|
154
|
-
console.error(
|
|
155
|
-
"api-client-generator %s Failed to generate %s",
|
|
156
|
-
g.red("error"),
|
|
157
|
-
g.whiteBright(a.console),
|
|
158
|
-
l
|
|
159
|
-
);
|
|
160
305
|
}
|
|
161
|
-
if (!t.SKIP_BUILD
|
|
162
|
-
|
|
306
|
+
if (!t.SKIP_BUILD) {
|
|
307
|
+
const c = h.join(t.APP_API_CLIENT_DIRECTORY, "tsconfig.json"), g = M(c, s, n, a);
|
|
308
|
+
R(c, JSON.stringify(g, null, 2)), console.log("api-client-generator %s Compiling TypeScript files to JavaScript", e.green("info")), D("npx tsc", ["--project", c], {
|
|
163
309
|
stdio: ["ignore", "inherit", "ignore"],
|
|
164
310
|
shell: !0
|
|
165
|
-
}).status === 0 ? console.log("api-client-generator %s Successfully compiled TypeScript files",
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
name: t.APP_PACKAGE_NAME || "api-client",
|
|
169
|
-
version: t.APP_PACKAGE_VERSION || "1.0.0",
|
|
170
|
-
files: i !== "./" ? [i, "package.json"] : ["package.json"],
|
|
171
|
-
exports: s
|
|
172
|
-
};
|
|
173
|
-
if (D(a)) {
|
|
174
|
-
const f = JSON.parse(C(a, "utf-8"));
|
|
175
|
-
l = { ...l, ...f }, l.files = Array.from(new Set(l.files.concat(f.files || []))), l.exports = { ...f.exports, ...l.exports };
|
|
176
|
-
for (const [P, h] of Object.entries(s)) {
|
|
177
|
-
const p = h.import, d = A.basename(p), w = Object.keys(l.exports).find(
|
|
178
|
-
(x) => l.exports[x].import.includes(d)
|
|
179
|
-
);
|
|
180
|
-
w ? l.exports[w] = h : l.exports[P] = h;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
T(a, JSON.stringify(l, null, 2)), console.log("api-client-generator %s Generated package.json", g.greenBright("success"));
|
|
311
|
+
}).status === 0 ? console.log("api-client-generator %s Successfully compiled TypeScript files", e.greenBright("success")) : console.error("api-client-generator %s Failed to compile TypeScript files", e.red("error"));
|
|
312
|
+
const m = h.join(t.APP_API_CLIENT_DIRECTORY, "package.json"), P = $(m, s, t);
|
|
313
|
+
R(m, JSON.stringify(P, null, 2)), console.log("api-client-generator %s Generated package.json", e.greenBright("success"));
|
|
184
314
|
}
|
|
185
315
|
}
|
|
186
|
-
|
|
316
|
+
U();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vc-shell/api-client-generator",
|
|
3
3
|
"description": "Tool for API clients generation",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.9",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": "./dist/api-client-generator.js",
|
|
7
7
|
"files": [
|
|
@@ -13,12 +13,13 @@
|
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/cross-spawn": "^6.0.6",
|
|
16
|
-
"@vc-shell/ts-config": "^1.1.
|
|
16
|
+
"@vc-shell/ts-config": "^1.1.9",
|
|
17
17
|
"typescript": "^5.8.3"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"chalk": "^2.4.2",
|
|
21
21
|
"cross-spawn": "^7.0.3",
|
|
22
|
+
"mri": "^1.2.0",
|
|
22
23
|
"nswag": "^13.20.0",
|
|
23
24
|
"vite": "^6.3.3",
|
|
24
25
|
"vite-plugin-dts": "^3.6.4"
|