moyan-api 1.0.80 → 1.0.82

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 (3) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +333 -165
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.0.82](https://gitee.com/ymoo/moyan-api/compare/v1.0.81...v1.0.82) (2026-03-24)
6
+
7
+ ### [1.0.81](https://gitee.com/ymoo/moyan-api/compare/v1.0.80...v1.0.81) (2026-03-24)
8
+
5
9
  ### [1.0.80](https://gitee.com/ymoo/moyan-api/compare/v1.0.79...v1.0.80) (2026-03-20)
6
10
 
7
11
  ### [1.0.78](https://gitee.com/ymoo/moyan-api/compare/v1.0.79...v1.0.78) (2026-03-20)
package/README.md CHANGED
@@ -1,165 +1,333 @@
1
- # moyan-api 使用
2
-
3
- ### 描述
4
- 用于生成OAS3 的调用api
5
-
6
- ### 安装
7
- ```sh
8
- npm i moyan-api -S
9
- ```
10
-
11
- ### pageage配置
12
- ```json
13
- #pageage.json
14
- {
15
- "moyan-api":{
16
- "output":"src", // 生成的文件输出目录,默认值:src
17
- "apijson":"moyan.api.json" ,// 生成sdk 的openApi 3.0 数据 ,默认值:moyan.api.json
18
- "jsonurl":"远程获取数据的地址",
19
- "apiprefix":"Api",
20
- }
21
- }
22
- ```
23
-
24
- ### 使用
25
- * 调用命令前,必须保证你的项目根目录里面存在moyan.api.json(openApi 3.0格式的数据)或者在ageage.json中配置
26
- ```json
27
- {
28
- "moyan-api":{
29
- "jsonurl":"远程获取数据的地址"
30
- }
31
- }
32
- ```
33
- ```sh
34
- cd 你的项目根目录
35
- moyan-api
36
- ```
37
- 调用命令后会在我们的src/api 生成index.ts、schemas.ts两个文件,其中index.ts中的class就是我们的api调用类了。
38
-
39
- ### 调用
40
- * 在我们开始调用api之前,我们需要对我们的 ApiCall 类做一些配置处理。
41
- * 我们可以在我们的src/lib/api下创建一个config.ts (具体文件名、路径可根据自己的喜好创建修改)。
42
- ```TypeScript
43
-
44
- import { ElMessage } from 'element-plus'
45
- import { getConfig } from '../../config'
46
- import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
47
- import { ApiCall, ApiEntity } from 'moyan-api'
48
- const AXIOS = Symbol('mo#Api#axios')
49
-
50
- export class MoAxios {
51
- static [AXIOS]: AxiosInstance
52
- options: any = {}
53
-
54
- constructor(options: any = {}) {
55
- this.options = options
56
- }
57
-
58
- get $axios() {
59
- if (!MoAxios[AXIOS]) {
60
- MoAxios[AXIOS] = axios.create(this.config.axios)
61
- this.init()
62
- }
63
- return MoAxios[AXIOS]
64
- }
65
-
66
- get config() {
67
- return getConfig()
68
- }
69
-
70
- init() {
71
- this.$axios.interceptors.response.use(
72
- (res) => {
73
- if (typeof this.options.render === 'function') {
74
- return this.options.render(res)
75
- } else {
76
- return res
77
- }
78
- },
79
- async (error) => {
80
- // Do something with response error
81
- if (error && error.response) {
82
- switch (error.response.status) {
83
- case 502:
84
- error.message = '网关错误'
85
- break
86
- case 504:
87
- error.message = '网关超时'
88
- break
89
- case 505:
90
- error.message = '版本不受支持'
91
- break
92
- default:
93
- error.message = error.response.data.message
94
- break
95
- }
96
- }
97
- throw error
98
- }
99
- )
100
- }
101
-
102
- request(apiEntity: ApiEntity) {
103
- const url = `${apiEntity.path}`
104
- const requsetOption: AxiosRequestConfig = {
105
- responseType: 'json',
106
- baseURL: this.config.moApi.baseURL,
107
- url,
108
- method: apiEntity.method,
109
- headers: {}
110
- }
111
-
112
- if (apiEntity.method === 'GET') {
113
- requsetOption.params = { params: apiEntity.params }
114
- } else {
115
- requsetOption.data = apiEntity.params
116
- }
117
- return this.$axios(requsetOption)
118
- }
119
- }
120
-
121
- ApiCall.hintSuccessHandler = (apiCall) => {
122
- console.info('请求成功!!')
123
- ApiCall.hasPrompted = true
124
- ElMessage.success({
125
- message: apiCall.successMsg,
126
- onClose: () => {
127
- ApiCall.hasPrompted = false
128
- }
129
- })
130
- }
131
-
132
- ApiCall.hintFailHandler = (apiCall) => {
133
- console.info('请求错误!!')
134
- ApiCall.hasPrompted = true
135
- apiCall.failMsg &&
136
- ElMessage.error({
137
- message: apiCall.failMsg,
138
- onClose: () => {
139
- ApiCall.hasPrompted = false
140
- }
141
- })
142
- }
143
-
144
- ApiCall.use(new MoAxios())
145
-
146
- ```
147
- * 现在我们就可以在我们的项目中调用api了
148
- ```TypeScript
149
- new ApiGetUser({params:{id:1}}).then((res)=>{
150
- ...
151
- })
152
-
153
-
154
- new ApiGetUser({query:{id:1}}).then((res)=>{
155
- ...
156
- })
157
-
158
- new ApiUpdateUser({query:{id:1},params:{name:'张三'}}).then((res)=>{
159
- ...
160
- })
161
-
162
- ```
163
-
164
- ### Gitee
165
- https://gitee.com/ymoo/moyan-api
1
+ # moyan-api
2
+
3
+ 基于 OpenAPI 3.0 (OAS3) 规范自动生成 TypeScript API 调用 SDK 的工具。
4
+
5
+ ## 功能特点
6
+
7
+ - 根据 OpenAPI 3.0 JSON 自动生成 TypeScript 类型安全的 API 调用类
8
+ - 支持本地文件和远程 URL 获取 API 定义
9
+ - 自动生成接口类型定义和 Schema 类型
10
+ - 支持自定义 API 类名前缀和请求路径前缀
11
+ - 内置请求拦截器和响应处理器
12
+ - 支持事件监听和自定义错误处理
13
+
14
+ ## 安装
15
+
16
+ ```sh
17
+ npm i moyan-api -S
18
+ ```
19
+
20
+ ## 快速开始
21
+
22
+ ### 1. 项目配置
23
+
24
+ 在项目的 `package.json` 中添加配置:
25
+
26
+ ```json
27
+ {
28
+ "moyan-api": {
29
+ "output": "src", // 生成文件的输出目录,默认:src
30
+ "apijson": "moyan.api.json", // OpenAPI 3.0 本地 JSON 文件路径,默认:moyan.api.json
31
+ "jsonurl": "https://...", // 远程 OpenAPI JSON 地址(二选一)
32
+ "apiprefix": "Api", // 生成的 SDK 类名前缀,默认:Api
33
+ "pathprefix": "/api", // 请求路径前缀,默认:空
34
+ "dirname": "api" // 生成的 SDK 目录名,默认:api
35
+ }
36
+ }
37
+ ```
38
+
39
+ ### 2. 准备 OpenAPI 数据
40
+
41
+ 确保项目根目录存在 OpenAPI 3.0 格式的 JSON 文件(如 `moyan.api.json`),或在 `package.json` 中配置 `jsonurl` 从远程获取。
42
+
43
+ ### 3. 生成 SDK
44
+
45
+ 在项目根目录运行命令:
46
+
47
+ ```sh
48
+ npx moyan-api
49
+ ```
50
+
51
+ 或使用 CLI 参数:
52
+
53
+ ```sh
54
+ npx moyan-api -a moyan.api.json -o src -af Api -pp /api
55
+ ```
56
+
57
+ 命令执行后,会在指定目录(默认 `src/api`)生成以下文件:
58
+
59
+ | 文件 | 说明 |
60
+ |------|------|
61
+ | `index.ts` | API 调用类定义 |
62
+ | `schemas.ts` | 数据类型定义 |
63
+
64
+ ### 4. 配置请求类
65
+
66
+ 在使用 API 之前,需要配置 `ApiCall` 的请求实现。建议在 `src/lib/api/config.ts`(路径可自定义)中创建配置:
67
+
68
+ ```typescript
69
+ import { ElMessage } from 'element-plus'
70
+ import { getConfig } from '../../config'
71
+ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
72
+ import { ApiCall, ApiEntity } from 'moyan-api'
73
+
74
+ const AXIOS = Symbol('mo#Api#axios')
75
+
76
+ export class MoAxios {
77
+ static [AXIOS]: AxiosInstance
78
+ options: any = {}
79
+
80
+ constructor(options: any = {}) {
81
+ this.options = options
82
+ }
83
+
84
+ get $axios() {
85
+ if (!MoAxios[AXIOS]) {
86
+ MoAxios[AXIOS] = axios.create(this.config.axios)
87
+ this.init()
88
+ }
89
+ return MoAxios[AXIOS]
90
+ }
91
+
92
+ get config() {
93
+ return getConfig()
94
+ }
95
+
96
+ init() {
97
+ this.$axios.interceptors.response.use(
98
+ (res) => {
99
+ if (typeof this.options.render === 'function') {
100
+ return this.options.render(res)
101
+ }
102
+ return res
103
+ },
104
+ async (error) => {
105
+ if (error && error.response) {
106
+ switch (error.response.status) {
107
+ case 502:
108
+ error.message = '网关错误'
109
+ break
110
+ case 504:
111
+ error.message = '网关超时'
112
+ break
113
+ case 505:
114
+ error.message = '版本不受支持'
115
+ break
116
+ default:
117
+ error.message = error.response.data.message
118
+ break
119
+ }
120
+ }
121
+ throw error
122
+ }
123
+ )
124
+ }
125
+
126
+ request(apiEntity: ApiEntity) {
127
+ const url = `${apiEntity.path}`
128
+ const requestOptions: AxiosRequestConfig = {
129
+ responseType: 'json',
130
+ baseURL: this.config.moApi.baseURL,
131
+ url,
132
+ method: apiEntity.method,
133
+ headers: {}
134
+ }
135
+
136
+ if (apiEntity.method === 'GET') {
137
+ requestOptions.params = apiEntity.params
138
+ } else {
139
+ requestOptions.data = apiEntity.params
140
+ }
141
+ return this.$axios(requestOptions)
142
+ }
143
+ }
144
+
145
+ // 成功提示处理器
146
+ ApiCall.hintSuccessHandler = (apiCall) => {
147
+ ApiCall.hasPrompted = true
148
+ ElMessage.success({
149
+ message: apiCall.successMsg,
150
+ onClose: () => {
151
+ ApiCall.hasPrompted = false
152
+ }
153
+ })
154
+ }
155
+
156
+ // 失败提示处理器
157
+ ApiCall.hintFailHandler = (apiCall) => {
158
+ ApiCall.hasPrompted = true
159
+ apiCall.failMsg &&
160
+ ElMessage.error({
161
+ message: apiCall.failMsg,
162
+ onClose: () => {
163
+ ApiCall.hasPrompted = false
164
+ }
165
+ })
166
+ }
167
+
168
+ // 注册请求实现
169
+ ApiCall.use(new MoAxios())
170
+ ```
171
+
172
+ ### 5. 调用 API
173
+
174
+ 配置完成后,即可在项目中调用生成的 API:
175
+
176
+ ```typescript
177
+ // GET 请求 - 使用 params
178
+ new ApiGetUser({ params: { id: 1 } }).then((res) => {
179
+ // 处理响应
180
+ })
181
+
182
+ // GET 请求 - 使用 query(路径参数)
183
+ new ApiGetUser({ query: { id: 1 } }).then((res) => {
184
+ // 处理响应
185
+ })
186
+
187
+ // PUT 请求 - 带路径参数和请求体
188
+ new ApiUpdateUser({
189
+ query: { id: 1 },
190
+ params: { name: '张三' }
191
+ }).then((res) => {
192
+ // 处理响应
193
+ })
194
+ ```
195
+
196
+ ## CLI 参数
197
+
198
+ | 参数 | 简写 | 说明 |
199
+ |------|------|------|
200
+ | `--apijson` | `-a` | OpenAPI JSON 文件路径 |
201
+ | `--output` | `-o` | 输出目录路径 |
202
+ | `--dirname` | `-d` | 生成的 SDK 目录名 |
203
+ | `--jsonurl` | `-j` | 远程 OpenAPI 数据地址 |
204
+ | `--apiprefix` | `-af` | 生成的 SDK 类名前缀 |
205
+ | `--pathprefix` | `-pp` | 请求路径前缀 |
206
+
207
+ ## 编程方式使用(多配置批量生成)
208
+
209
+ 如果需要同时生成多个模块的 API SDK,可以创建配置文件使用编程方式调用:
210
+
211
+ ```javascript
212
+ // generate-api.js
213
+ const { ApisdkCreator } = require('moyan-api/dist/main.js')
214
+ const { Program } = require('moyan-api/dist/program.js')
215
+
216
+ const configs = [
217
+ {
218
+ jsonurl: 'http://localhost:8080/apiDoc/main-app',
219
+ output: './src/apis',
220
+ dirname: 'main-app'
221
+ },
222
+ {
223
+ jsonurl: 'http://localhost:8080/apiDoc/micro-system',
224
+ output: './src/apis',
225
+ dirname: 'micro-system'
226
+ },
227
+ ]
228
+
229
+ const create = async () => {
230
+ for (let i = 0; i < configs.length; i++) {
231
+ await new ApisdkCreator(new Program(configs[i])).create()
232
+ }
233
+ }
234
+
235
+ create()
236
+ ```
237
+
238
+ 在 `package.json` 中添加脚本:
239
+
240
+ ```json
241
+ {
242
+ "scripts": {
243
+ "generate-api": "node generate-api.js"
244
+ }
245
+ }
246
+ ```
247
+
248
+ 然后运行:
249
+
250
+ ```sh
251
+ npm run generate-api
252
+ ```
253
+
254
+ ### Program 配置项
255
+
256
+ | 属性 | 说明 |
257
+ |------|------|
258
+ | `jsonurl` | 远程 OpenAPI JSON 地址 |
259
+ | `apijson` | 本地 OpenAPI JSON 文件路径 |
260
+ | `output` | 输出目录路径 |
261
+ | `dirname` | 生成的 SDK 目录名 |
262
+ | `apiprefix` | 生成的 SDK 类名前缀 |
263
+ | `pathprefix` | 请求路径前缀 |
264
+
265
+ ## 导出的类型和接口
266
+
267
+ ```typescript
268
+ import {
269
+ ApiCall, // API 调用基类
270
+ ApiEntity, // API 实体接口
271
+ ApiCallProps, // API 调用参数类型
272
+ SubApiEntity, // 子 API 实体接口
273
+ ObjectAny, // 任意对象类型
274
+ Option, // 请求选项类型
275
+ MoMethod // HTTP 方法类型
276
+ } from 'moyan-api'
277
+ ```
278
+
279
+ ## 请求选项 (Option)
280
+
281
+ ```typescript
282
+ interface Option {
283
+ fileName?: string // 下载文件的文件名
284
+ prefix?: boolean // 是否使用 API 的 prefix,默认 true
285
+ hintSuccess?: boolean // 是否显示成功提示,默认 true
286
+ hintFail?: boolean // 是否显示失败提示,默认 true
287
+ showLoading?: boolean // 是否显示加载蒙版,默认 false
288
+ loading?: boolean // 加载状态
289
+ successMsg?: string | ((res: any) => string) // 成功提示消息
290
+ failMsg?: string | ((err: any) => string) // 失败提示消息
291
+ ext?: Record<string, any> // 扩展字段
292
+ }
293
+ ```
294
+
295
+ ## 事件监听
296
+
297
+ `ApiCall` 内置了事件系统,可用于监听请求状态:
298
+
299
+ ```typescript
300
+ import { ApiEvents } from 'moyan-api'
301
+
302
+ // 监听请求成功
303
+ ApiCall.emitter.on(ApiEvents.HintSuccess, (apiCall) => {
304
+ console.log('请求成功提示已显示')
305
+ })
306
+
307
+ // 监听请求失败
308
+ ApiCall.emitter.on(ApiEvents.HintFail, (apiCall) => {
309
+ console.log('请求失败提示已显示')
310
+ })
311
+
312
+ // 监听 401 未授权
313
+ ApiCall.emitter.on(ApiEvents.Unauthorized, (apiCall) => {
314
+ // 处理登出逻辑
315
+ })
316
+ ```
317
+
318
+ ## 生成的 API 类结构
319
+
320
+ 每个 API 类继承自 `ApiCall<ReqType, ResType>`:
321
+
322
+ ```typescript
323
+ export class ApiGetUser extends ApiCall<GetUserReq, GetUserRes> {
324
+ path = '/api/user/{id}'
325
+ method: MoMethod = 'GET'
326
+ auth = true
327
+ }
328
+ ```
329
+
330
+ ## 项目链接
331
+
332
+ - Gitee: https://gitee.com/ymoo/moyan-api
333
+ - npm: https://www.npmjs.com/package/moyan-api
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moyan-api",
3
- "version": "1.0.80",
3
+ "version": "1.0.82",
4
4
  "description": "使用OpenApi 生成TypeScript的api调用skd",
5
5
  "main": "./dist/index.js",
6
6
  "types": "src",