vite-plugin-openapi-codegen 1.1.1 → 1.1.3
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/README.md +46 -6
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +812 -0
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +186 -65
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ The plugin reads your OpenAPI JSON or YAML spec, runs `openapi-typescript`, and
|
|
|
8
8
|
- `api.ts` for path builder functions
|
|
9
9
|
- `client.ts` for typed request helpers
|
|
10
10
|
|
|
11
|
-
It also
|
|
11
|
+
It also regenerates local input specs through Vite HMR when the spec changes. In dev mode, generation runs in the background so Vite can finish starting even if the remote spec is temporarily unavailable. Online `http://` and `https://` inputs are fetched once when Vite starts. Build mode skips the plugin entirely. For manual generation, the package also ships a `vg` CLI that reads your local `vite.config.*` and runs the same generator once.
|
|
12
12
|
|
|
13
13
|
## Installation
|
|
14
14
|
|
|
@@ -36,6 +36,22 @@ export default defineConfig({
|
|
|
36
36
|
});
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
If you want to reuse the same configuration outside Vite, run the CLI from the project root:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
vg
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
By default, `vg` searches for `vite.config.ts`, `vite.config.mts`, `vite.config.js`, `vite.config.mjs`, `vite.config.cjs`, or `vite.config.cts` in the current working directory. It reads the `openapiCodegen(...)` plugin options from that file, then generates once with those values.
|
|
46
|
+
|
|
47
|
+
You can also pass explicit flags. CLI flags always win over the config file:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
vg --root ./example/local
|
|
51
|
+
vg --input openapi.yaml --output src/generated
|
|
52
|
+
vg --path-prefix /v1/ --type-aliases
|
|
53
|
+
```
|
|
54
|
+
|
|
39
55
|
Online YAML inputs use the same option:
|
|
40
56
|
|
|
41
57
|
```ts
|
|
@@ -149,6 +165,25 @@ export default defineConfig({
|
|
|
149
165
|
});
|
|
150
166
|
```
|
|
151
167
|
|
|
168
|
+
If you want shorter generated type references, enable `typeAliases`. This keeps the type name suffixes, but avoids long chains like `components["schemas"]["CreateUserRequest"]` and `operations["list_users"]["parameters"]["query"]` in the generated output:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { defineConfig } from "vite-plus";
|
|
172
|
+
import { openapiCodegen } from "vite-plugin-openapi-codegen";
|
|
173
|
+
|
|
174
|
+
export default defineConfig({
|
|
175
|
+
plugins: [
|
|
176
|
+
openapiCodegen({
|
|
177
|
+
input: "openapi.json",
|
|
178
|
+
output: "src/generated",
|
|
179
|
+
typeAliases: true,
|
|
180
|
+
}),
|
|
181
|
+
],
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
When enabled, the plugin writes the raw OpenAPI types to `api-types.d.ts` and adds top-level aliases for schema and operation types that the generated `api.ts` and `client.ts` files import directly.
|
|
186
|
+
|
|
152
187
|
## Generated Output
|
|
153
188
|
|
|
154
189
|
Given a spec path like `/api/users/{user_id}`, the plugin generates a path builder:
|
|
@@ -201,6 +236,7 @@ interface Options {
|
|
|
201
236
|
output: string;
|
|
202
237
|
pathPrefix?: string;
|
|
203
238
|
stripPrefix?: boolean;
|
|
239
|
+
typeAliases?: boolean;
|
|
204
240
|
httpClient?: {
|
|
205
241
|
module?: string;
|
|
206
242
|
jsonFunction?: string;
|
|
@@ -213,25 +249,29 @@ interface Options {
|
|
|
213
249
|
|
|
214
250
|
### `input`
|
|
215
251
|
|
|
216
|
-
Path or URL to the OpenAPI JSON/YAML document. Local paths are resolved relative to the Vite project root and watched in dev mode. Online `http://` and `https://` URLs are fetched once at startup and are not watched.
|
|
252
|
+
Path or URL to the OpenAPI JSON/YAML document. Local paths are resolved relative to the Vite project root and watched in dev mode. Online `http://` and `https://` URLs are fetched once at startup and are not watched. The `vg` CLI reads this value from `vite.config.*` when present, unless you override it with `--input`.
|
|
217
253
|
|
|
218
254
|
In dev mode, generation errors are logged and do not stop Vite from starting. In build mode, the plugin is skipped entirely.
|
|
219
255
|
|
|
220
256
|
### `output`
|
|
221
257
|
|
|
222
|
-
Directory where generated files are written, relative to the Vite project root.
|
|
258
|
+
Directory where generated files are written, relative to the Vite project root. The `vg` CLI reads this value from `vite.config.*` when present, unless you override it with `--output`.
|
|
223
259
|
|
|
224
260
|
### `pathPrefix`
|
|
225
261
|
|
|
226
|
-
Only paths starting with this prefix are included. The default is `"/api/"`.
|
|
262
|
+
Only paths starting with this prefix are included. The default is `"/api/"`. The `vg` CLI reads this value from `vite.config.*` when present, unless you override it with `--path-prefix`.
|
|
227
263
|
|
|
228
264
|
### `stripPrefix`
|
|
229
265
|
|
|
230
|
-
Controls whether the `pathPrefix` is removed from generated path builders. The default is `true`.
|
|
266
|
+
Controls whether the `pathPrefix` is removed from generated path builders. The default is `true`. The `vg` CLI reads this value from `vite.config.*` when present, unless you override it with `--strip-prefix` or `--no-strip-prefix`.
|
|
267
|
+
|
|
268
|
+
### `typeAliases`
|
|
269
|
+
|
|
270
|
+
When enabled, the plugin generates top-level aliases for schema and operation types and makes the emitted `api.ts` and `client.ts` import those shorter names. The suffixes stay intact, so generated names remain readable while avoiding long `components["schemas"][...]` and `operations["..."][...]` chains. The default is `false` to preserve existing output. The `vg` CLI reads this value from `vite.config.*` when present, unless you override it with `--type-aliases` or `--no-type-aliases`.
|
|
231
271
|
|
|
232
272
|
### `httpClient`
|
|
233
273
|
|
|
234
|
-
Overrides the runtime import path and symbol names used by generated clients.
|
|
274
|
+
Overrides the runtime import path and symbol names used by generated clients. The `vg` CLI reads this object from `vite.config.*` when present, unless you override the corresponding HTTP client flags.
|
|
235
275
|
|
|
236
276
|
## Programmatic Usage
|
|
237
277
|
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|