vite-plugin-swagger-typescript-api-transform 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Anthony Fu <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # vite-plugin-swagger-typescript-api
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/vite-plugin-swagger-typescript-api?color=a1b858&label=)](https://npm.npmjs.com/package/vite-plugin-swagger-typescript-api)
4
+
5
+ 基于 [unplugin](https://github.com/unjs/unplugin) 的插件,构建时自动扫描 Swagger/OpenAPI YAML 文件生成 TypeScript API 客户端。支持 Vite、Rollup、Webpack、Rspack、Nuxt、esbuild、Farms、Astro 等。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm i vite-plugin-swagger-typescript-api
11
+ ```
12
+
13
+ ## 快速开始
14
+
15
+ 1. 项目根目录创建 `yaml/` 文件夹,放入 Swagger YAML 文件
16
+ 2. 配置插件
17
+ 3. 自动生成 `src/api/` 下的 TypeScript API 代码
18
+
19
+ ## 配置选项
20
+
21
+ ```ts
22
+ SwaggerTSApi({
23
+ rootDir?: string // 扫描根目录,默认 process.cwd()
24
+ outputDir?: string // 输出目录,默认 'src/api'
25
+ generateClient?: boolean // 生成请求函数,默认 true
26
+ httpClient?: 'fetch' | 'axios' // HTTP 客户端,默认 'fetch'
27
+ baseURL?: string // API 基础 URL(生成 request.ts)
28
+ watch?: boolean // 监听文件变化,默认 true
29
+ moduleNameFormat?: 'kebab' | 'camel' | 'pascal' // 模块名格式,默认 'kebab'
30
+ })
31
+ ```
32
+
33
+ ## 构建工具集成
34
+
35
+ ### Vite
36
+
37
+ ```ts
38
+ // vite.config.ts
39
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api'
40
+
41
+ export default {
42
+ plugins: [SwaggerTSApi({ outputDir: 'src/api' })]
43
+ }
44
+ ```
45
+
46
+ ### Rollup
47
+
48
+ ```ts
49
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api/rollup'
50
+
51
+ export default { plugins: [SwaggerTSApi({ outputDir: 'src/api' })] }
52
+ ```
53
+
54
+ ### Webpack
55
+
56
+ ```js
57
+ const SwaggerTSApi = require('vite-plugin-swagger-typescript-api/webpack')
58
+
59
+ module.exports = {
60
+ plugins: [new SwaggerTSApi({ outputDir: 'src/api' })]
61
+ }
62
+ ```
63
+
64
+ ### Rspack
65
+
66
+ ```ts
67
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api/rspack'
68
+
69
+ export default { plugins: [SwaggerTSApi({ outputDir: 'src/api' })] }
70
+ ```
71
+
72
+ ### Nuxt
73
+
74
+ ```ts
75
+ // nuxt.config.ts
76
+ export default {
77
+ modules: ['vite-plugin-swagger-typescript-api/nuxt'],
78
+ swaggerTsApi: { outputDir: 'src/api' }
79
+ }
80
+ ```
81
+
82
+ ### Vue CLI
83
+
84
+ ```js
85
+ // vue.config.js
86
+ module.exports = {
87
+ configureWebpack: {
88
+ plugins: [new (require('vite-plugin-swagger-typescript-api/webpack'))({})]
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### esbuild
94
+
95
+ ```ts
96
+ import { build } from 'esbuild'
97
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api/esbuild'
98
+
99
+ build({ plugins: [SwaggerTSApi({})] })
100
+ ```
101
+
102
+ ### Farm
103
+
104
+ ```ts
105
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api/farm'
106
+
107
+ export default { plugins: [SwaggerTSApi({})] }
108
+ ```
109
+
110
+ ### Astro
111
+
112
+ ```ts
113
+ // astro.config.mjs
114
+ import SwaggerTSApi from 'vite-plugin-swagger-typescript-api'
115
+
116
+ export default {
117
+ vite: { plugins: [SwaggerTSApi({ outputDir: 'src/api' })] }
118
+ }
119
+ ```
120
+
121
+ ## 部署项目
122
+
123
+ ### 构建
124
+
125
+ ```bash
126
+ # 开发环境(带 watch 模式,自动重新生成 API)
127
+ npm run dev
128
+
129
+ # 生产构建
130
+ npm run build
131
+ ```
132
+
133
+ ### 生成产物
134
+
135
+ 插件在 **构建时** 生成 API 代码到 `outputDir`,生成的代码是普通 TypeScript,无需特殊处理:
136
+
137
+ ```bash
138
+ # 构建后 src/api/ 目录结构示例
139
+ src/api/
140
+ ├── index.ts # 统一入口
141
+ ├── request.ts # HTTP 客户端(配置 baseURL 时生成)
142
+ ├── user/ # 根据 yaml/user.yaml 生成
143
+ │ ├── Api.ts
144
+ │ ├── index.ts
145
+ │ └── example.ts
146
+ └── product/
147
+ ├── Api.ts
148
+ ├── index.ts
149
+ └── example.ts
150
+ ```
151
+
152
+ ### 提交到 Git
153
+
154
+ 建议将 `outputDir`(如 `src/api`)**一起提交**,因为:
155
+ - API 代码由 YAML 驱动,提交后可追溯
156
+ - 团队成员无需运行插件即可开发
157
+ - 构建时即使 YAML 解析失败也有可用代码
158
+
159
+ 如果只想在构建时生成,可将 `src/api` 加入 `.gitignore`:
160
+
161
+ ```gitignore
162
+ # 取消注释如果你不希望提交生成的 API 代码
163
+ # src/api/
164
+ ```
165
+
166
+ ### CI/CD 注意事项
167
+
168
+ 确保 CI 环境安装依赖后执行构建命令:
169
+
170
+ ```yaml
171
+ # .github/workflows/deploy.yml 示例
172
+ - uses: actions/checkout@v4
173
+ - uses: pnpm/action-setup@v4
174
+ - run: pnpm install
175
+ - run: pnpm run build # 插件在此阶段生成 API 代码
176
+ ```
177
+
178
+ ## 项目架构
179
+
180
+ ```
181
+ src/
182
+ ├── index.ts # 插件入口
183
+ ├── types.ts # 类型定义
184
+ ├── utils.ts # 工具函数
185
+ ├── core/ # 核心模块
186
+ │ ├── constants.ts # 常量
187
+ │ ├── plugin.ts # unplugin 工厂
188
+ │ ├── scanner.ts # 扫描协调
189
+ │ └── generator.ts # API 生成
190
+ └── generators/ # 代码生成器
191
+ ├── example.ts
192
+ ├── module-index.ts
193
+ ├── entry-index.ts
194
+ └── request.ts
195
+ ```
@@ -0,0 +1,7 @@
1
+ import { Options } from "./types-CpgPraRT.js";
2
+
3
+ //#region src/astro.d.ts
4
+ declare const _default: (options: Options) => any;
5
+
6
+ //#endregion
7
+ export { _default as default };
package/dist/astro.js ADDED
@@ -0,0 +1,14 @@
1
+ import { unplugin } from "./src-DPpaOQvV.js";
2
+ import "./utils-BypLbD1p.js";
3
+
4
+ //#region src/astro.ts
5
+ var astro_default = (options) => ({
6
+ name: "unplugin-starter",
7
+ hooks: { "astro:config:setup": async (astro) => {
8
+ astro.config.vite.plugins ||= [];
9
+ astro.config.vite.plugins.push(unplugin.vite(options));
10
+ } }
11
+ });
12
+
13
+ //#endregion
14
+ export { astro_default as default };
@@ -0,0 +1,8 @@
1
+ import { Options } from "./types-CpgPraRT.js";
2
+ import * as esbuild13 from "esbuild";
3
+
4
+ //#region src/esbuild.d.ts
5
+ declare const _default: (options?: Options | undefined) => esbuild13.Plugin;
6
+
7
+ //#endregion
8
+ export { _default as default };
@@ -0,0 +1,9 @@
1
+ import { unpluginFactory } from "./src-DPpaOQvV.js";
2
+ import "./utils-BypLbD1p.js";
3
+ import { createEsbuildPlugin } from "unplugin";
4
+
5
+ //#region src/esbuild.ts
6
+ var esbuild_default = createEsbuildPlugin(unpluginFactory);
7
+
8
+ //#endregion
9
+ export { esbuild_default as default };
package/dist/farm.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Options } from "./types-CpgPraRT.js";
2
+ import * as _farmfe_core1 from "@farmfe/core";
3
+
4
+ //#region src/farm.d.ts
5
+ declare const _default: (options?: Options | undefined) => _farmfe_core1.JsPlugin;
6
+
7
+ //#endregion
8
+ export { _default as default };
package/dist/farm.js ADDED
@@ -0,0 +1,9 @@
1
+ import { unpluginFactory } from "./src-DPpaOQvV.js";
2
+ import "./utils-BypLbD1p.js";
3
+ import { createFarmPlugin } from "unplugin";
4
+
5
+ //#region src/farm.ts
6
+ var farm_default = createFarmPlugin(unpluginFactory);
7
+
8
+ //#endregion
9
+ export { farm_default as default };
@@ -0,0 +1,18 @@
1
+ import { HttpClientType, MergedOptions, Options } from "./types-CpgPraRT.js";
2
+ import * as unplugin14 from "unplugin";
3
+ import { UnpluginFactory } from "unplugin";
4
+
5
+ //#region src/core/constants.d.ts
6
+ declare const PLUGIN_NAME = "swagger-typescript-api";
7
+ declare const DEFAULT_OPTIONS: MergedOptions;
8
+
9
+ //#endregion
10
+ //#region src/core/plugin.d.ts
11
+ /**
12
+ * 插件工厂函数
13
+ */
14
+ declare const unpluginFactory: UnpluginFactory<Options | undefined>;
15
+ declare const unplugin: unplugin14.UnpluginInstance<Options | undefined, boolean>;
16
+
17
+ //#endregion
18
+ export { DEFAULT_OPTIONS, HttpClientType, MergedOptions, Options, PLUGIN_NAME, unplugin as default, unplugin, unpluginFactory };
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { DEFAULT_OPTIONS, PLUGIN_NAME, unplugin, unpluginFactory } from "./src-DPpaOQvV.js";
2
+ import "./utils-BypLbD1p.js";
3
+
4
+ export { DEFAULT_OPTIONS, PLUGIN_NAME, unplugin as default, unplugin, unpluginFactory };
package/dist/nuxt.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Options } from "./types-CpgPraRT.js";
2
+ import * as _nuxt_schema10 from "@nuxt/schema";
3
+
4
+ //#region src/nuxt.d.ts
5
+ interface ModuleOptions extends Options {}
6
+ declare const _default: _nuxt_schema10.NuxtModule<ModuleOptions, ModuleOptions, false>;
7
+
8
+ //#endregion
9
+ export { ModuleOptions, _default as default };
package/dist/nuxt.js ADDED
@@ -0,0 +1,21 @@
1
+ import "./src-DPpaOQvV.js";
2
+ import "./utils-BypLbD1p.js";
3
+ import { vite_default } from "./vite-BKknryyS.js";
4
+ import { webpack_default } from "./webpack-9obP3bLC.js";
5
+ import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
6
+
7
+ //#region src/nuxt.ts
8
+ var nuxt_default = defineNuxtModule({
9
+ meta: {
10
+ name: "nuxt-unplugin-starter",
11
+ configKey: "unpluginStarter"
12
+ },
13
+ defaults: {},
14
+ setup(options, _nuxt) {
15
+ addVitePlugin(() => vite_default(options));
16
+ addWebpackPlugin(() => webpack_default(options));
17
+ }
18
+ });
19
+
20
+ //#endregion
21
+ export { nuxt_default as default };