moyan-api 1.0.84 → 1.1.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/package.json +5 -2
- package/.eslintrc.js +0 -28
- package/.gitee/ISSUE_TEMPLATE.zh-CN.md +0 -13
- package/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +0 -11
- package/.prettierrc +0 -5
- package/CHANGELOG.md +0 -201
- package/src/creator.ts +0 -41
- package/src/index.ts +0 -11
- package/src/lib/base.ts +0 -260
- package/src/main.ts +0 -110
- package/src/program.ts +0 -17
- package/src/template/api.ts +0 -123
- package/src/template/schemas.ts +0 -95
- package/src/utils/mo.ts +0 -402
- package/tsconfig.json +0 -21
- package/view/api.ejs +0 -35
- package/view/schemas.ejs +0 -19
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moyan-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "使用OpenApi 生成TypeScript的api调用skd",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
|
-
"types": "
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
7
10
|
"bin": {
|
|
8
11
|
"moyan-api": "./dist/creator.js"
|
|
9
12
|
},
|
package/.eslintrc.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
parser: "@typescript-eslint/parser",
|
|
3
|
-
parserOptions: {
|
|
4
|
-
project: "tsconfig.json",
|
|
5
|
-
sourceType: "module"
|
|
6
|
-
},
|
|
7
|
-
plugins: ["@typescript-eslint/eslint-plugin"],
|
|
8
|
-
extends: [
|
|
9
|
-
"plugin:@typescript-eslint/recommended",
|
|
10
|
-
"plugin:prettier/recommended"
|
|
11
|
-
],
|
|
12
|
-
root: true,
|
|
13
|
-
env: {
|
|
14
|
-
node: true,
|
|
15
|
-
jest: true
|
|
16
|
-
},
|
|
17
|
-
ignorePatterns: [".eslintrc.js"],
|
|
18
|
-
rules: {
|
|
19
|
-
semi: "off",
|
|
20
|
-
quotes: "off",
|
|
21
|
-
indent: "off",
|
|
22
|
-
"dot-notation": "off",
|
|
23
|
-
"@typescript-eslint/interface-name-prefix": "off",
|
|
24
|
-
"@typescript-eslint/explicit-function-return-type": "off",
|
|
25
|
-
"@typescript-eslint/explicit-module-boundary-types": "off",
|
|
26
|
-
"@typescript-eslint/no-explicit-any": "off"
|
|
27
|
-
}
|
|
28
|
-
};
|
package/.prettierrc
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
### [1.0.84](https://gitee.com/ymoo/moyan-api/compare/v1.0.83...v1.0.84) (2026-04-01)
|
|
6
|
-
|
|
7
|
-
### [1.0.83](https://gitee.com/ymoo/moyan-api/compare/v1.0.82...v1.0.83) (2026-04-01)
|
|
8
|
-
|
|
9
|
-
### [1.0.82](https://gitee.com/ymoo/moyan-api/compare/v1.0.81...v1.0.82) (2026-03-24)
|
|
10
|
-
|
|
11
|
-
### [1.0.81](https://gitee.com/ymoo/moyan-api/compare/v1.0.80...v1.0.81) (2026-03-24)
|
|
12
|
-
|
|
13
|
-
### [1.0.80](https://gitee.com/ymoo/moyan-api/compare/v1.0.79...v1.0.80) (2026-03-20)
|
|
14
|
-
|
|
15
|
-
### [1.0.78](https://gitee.com/ymoo/moyan-api/compare/v1.0.79...v1.0.78) (2026-03-20)
|
|
16
|
-
|
|
17
|
-
### [1.0.79](https://gitee.com/ymoo/moyan-api/compare/v1.0.78...v1.0.79) (2026-03-20)
|
|
18
|
-
|
|
19
|
-
### [1.0.78](https://gitee.com/ymoo/moyan-api/compare/v1.0.77...v1.0.78) (2026-03-20)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
### Bug Fixes
|
|
23
|
-
|
|
24
|
-
* **api:** 处理字段名包含点号时未正确转义的问题 ([8b05651](https://gitee.com/ymoo/moyan-api/commit/8b05651f2b3fe65bd2fdf403a8ee64c76c074c3c))
|
|
25
|
-
|
|
26
|
-
### [1.0.77](https://gitee.com/ymoo/moyan-api/compare/v1.0.76...v1.0.77) (2025-12-15)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
### Bug Fixes
|
|
30
|
-
|
|
31
|
-
* **api:** 修复模板生成代码中的换行符问题 ([6a68cb3](https://gitee.com/ymoo/moyan-api/commit/6a68cb3eefbc133b1f4e6ecc3ac17cc9f28a224b))
|
|
32
|
-
|
|
33
|
-
### [1.0.76](https://gitee.com/ymoo/moyan-api/compare/v1.0.75...v1.0.76) (2025-12-15)
|
|
34
|
-
|
|
35
|
-
### [1.0.75](https://gitee.com/ymoo/moyan-api/compare/v1.0.74...v1.0.75) (2025-12-15)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
### Bug Fixes
|
|
39
|
-
|
|
40
|
-
* **api:** 处理schema引用类型时使用正确的字段类型 ([eaf872e](https://gitee.com/ymoo/moyan-api/commit/eaf872ebec22ba7318ee0524e43511cd92aa41b5))
|
|
41
|
-
* **api:** 修复模板字符串中特殊字符转义问题 ([c7e403e](https://gitee.com/ymoo/moyan-api/commit/c7e403e59f9e8baa626fb2e6a79ee4f59ba5bdfc))
|
|
42
|
-
|
|
43
|
-
### [1.0.74](https://gitee.com/ymoo/moyan-api/compare/v1.0.73...v1.0.74) (2025-12-15)
|
|
44
|
-
|
|
45
|
-
### [1.0.73](https://gitee.com/ymoo/moyan-api/compare/v1.0.72...v1.0.73) (2025-12-15)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### Bug Fixes
|
|
49
|
-
|
|
50
|
-
* 清理描述字段中的特殊字符和空值处理 ([edd5a78](https://gitee.com/ymoo/moyan-api/commit/edd5a78e6db84d86bc0eb2a0ef5bfea04286f33a))
|
|
51
|
-
|
|
52
|
-
### [1.0.72](https://gitee.com/ymoo/moyan-api/compare/v1.0.71...v1.0.72) (2025-12-15)
|
|
53
|
-
|
|
54
|
-
### [1.0.71](https://gitee.com/ymoo/moyan-api/compare/v1.0.70...v1.0.71) (2025-12-15)
|
|
55
|
-
|
|
56
|
-
### [1.0.70](https://gitee.com/ymoo/moyan-api/compare/v1.0.69...v1.0.70) (2025-12-15)
|
|
57
|
-
|
|
58
|
-
### [1.0.69](https://gitee.com/ymoo/moyan-api/compare/v1.0.68...v1.0.69) (2025-12-15)
|
|
59
|
-
|
|
60
|
-
### [1.0.68](https://gitee.com/ymoo/moyan-api/compare/v1.0.67...v1.0.68) (2025-12-15)
|
|
61
|
-
|
|
62
|
-
### [1.0.67](https://gitee.com/ymoo/moyan-api/compare/v1.0.66...v1.0.67) (2025-12-15)
|
|
63
|
-
|
|
64
|
-
### [1.0.66](https://gitee.com/ymoo/moyan-api/compare/v1.0.65...v1.0.66) (2025-12-12)
|
|
65
|
-
|
|
66
|
-
### [1.0.65](https://gitee.com/ymoo/moyan-api/compare/v1.0.64...v1.0.65) (2025-12-12)
|
|
67
|
-
|
|
68
|
-
### [1.0.64](https://gitee.com/ymoo/moyan-api/compare/v1.0.63...v1.0.64) (2025-12-12)
|
|
69
|
-
|
|
70
|
-
### [1.0.63](https://gitee.com/ymoo/moyan-api/compare/v1.0.62...v1.0.63) (2025-12-12)
|
|
71
|
-
|
|
72
|
-
### [1.0.62](https://gitee.com/ymoo/moyan-api/compare/v1.0.61...v1.0.62) (2025-12-12)
|
|
73
|
-
|
|
74
|
-
### [1.0.61](https://gitee.com/ymoo/moyan-api/compare/v1.0.60...v1.0.61) (2025-12-11)
|
|
75
|
-
|
|
76
|
-
### [1.0.60](https://gitee.com/ymoo/moyan-api/compare/v1.0.59...v1.0.60) (2025-12-11)
|
|
77
|
-
|
|
78
|
-
### [1.0.59](https://gitee.com/ymoo/moyan-api/compare/v1.0.58...v1.0.59) (2025-12-11)
|
|
79
|
-
|
|
80
|
-
### [1.0.58](https://gitee.com/ymoo/moyan-api/compare/v1.0.57...v1.0.58) (2025-12-11)
|
|
81
|
-
|
|
82
|
-
### [1.0.57](https://gitee.com/ymoo/moyan-api/compare/v1.0.56...v1.0.57) (2025-12-11)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
### Bug Fixes
|
|
86
|
-
|
|
87
|
-
* 修复类型声明和接口定义问题 ([06f6ddd](https://gitee.com/ymoo/moyan-api/commit/06f6ddd5a033da4f72088c446fc5c5fb6c59a47e))
|
|
88
|
-
|
|
89
|
-
### [1.0.56](https://gitee.com/ymoo/moyan-api/compare/v1.0.55...v1.0.56) (2025-09-16)
|
|
90
|
-
|
|
91
|
-
### [1.0.55](https://gitee.com/ymoo/moyan-api/compare/v1.0.54...v1.0.55) (2025-09-16)
|
|
92
|
-
|
|
93
|
-
### [1.0.54](https://gitee.com/ymoo/moyan-api/compare/v1.0.53...v1.0.54) (2025-09-16)
|
|
94
|
-
|
|
95
|
-
### [1.0.53](https://gitee.com/ymoo/moyan-api/compare/v1.0.52...v1.0.53) (2025-09-15)
|
|
96
|
-
|
|
97
|
-
### [1.0.52](https://gitee.com/ymoo/moyan-api/compare/v1.0.51...v1.0.52) (2025-09-15)
|
|
98
|
-
|
|
99
|
-
### [1.0.51](https://gitee.com/ymoo/moyan-api/compare/v1.0.50...v1.0.51) (2025-08-12)
|
|
100
|
-
|
|
101
|
-
### 1.0.50 (2025-08-12)
|
|
102
|
-
|
|
103
|
-
### [1.0.49](https://gitee.com/ymoo/moyan-api/compare/v1.0.48...v1.0.49) (2025-08-12)
|
|
104
|
-
|
|
105
|
-
### [1.0.48](https://gitee.com/ymoo/moyan-api/compare/v1.0.46...v1.0.48) (2025-08-12)
|
|
106
|
-
|
|
107
|
-
### [1.0.47](https://gitee.com/ymoo/moyan-api/compare/v1.0.46...v1.0.47) (2025-08-12)
|
|
108
|
-
|
|
109
|
-
### [1.0.46](https://gitee.com/ymoo/moyan-api/compare/v1.0.47...v1.0.46) (2025-08-12)
|
|
110
|
-
|
|
111
|
-
### 1.0.47 (2025-08-12)
|
|
112
|
-
|
|
113
|
-
### [1.0.45](https://gitee.com/ymoo/moyan-api/compare/v1.0.44...v1.0.45) (2023-08-24)
|
|
114
|
-
|
|
115
|
-
### [1.0.44](https://gitee.com/ymoo/moyan-api/compare/v1.0.43...v1.0.44) (2023-08-24)
|
|
116
|
-
|
|
117
|
-
### [1.0.43](https://gitee.com/ymoo/moyan-api/compare/v1.0.42...v1.0.43) (2023-08-24)
|
|
118
|
-
|
|
119
|
-
### [1.0.42](https://gitee.com/ymoo/moyan-api/compare/v1.0.41...v1.0.42) (2023-08-24)
|
|
120
|
-
|
|
121
|
-
### [1.0.41](https://gitee.com/ymoo/moyan-api/compare/v1.0.40...v1.0.41) (2023-08-24)
|
|
122
|
-
|
|
123
|
-
### [1.0.40](https://gitee.com/ymoo/moyan-api/compare/v1.0.39...v1.0.40) (2023-08-24)
|
|
124
|
-
|
|
125
|
-
### [1.0.39](https://gitee.com/ymoo/moyan-api/compare/v1.0.38...v1.0.39) (2023-08-24)
|
|
126
|
-
|
|
127
|
-
### [1.0.38](https://gitee.com/ymoo/moyan-api/compare/v1.0.37...v1.0.38) (2023-08-24)
|
|
128
|
-
|
|
129
|
-
### [1.0.37](https://gitee.com/ymoo/moyan-api/compare/v1.0.36...v1.0.37) (2023-08-17)
|
|
130
|
-
|
|
131
|
-
### [1.0.36](https://gitee.com/ymoo/moyan-api/compare/v1.0.35...v1.0.36) (2023-08-17)
|
|
132
|
-
|
|
133
|
-
### [1.0.35](https://gitee.com/ymoo/moyan-api/compare/v1.0.34...v1.0.35) (2023-06-08)
|
|
134
|
-
|
|
135
|
-
### [1.0.34](https://gitee.com/ymoo/moyan-api/compare/v1.0.33...v1.0.34) (2023-06-08)
|
|
136
|
-
|
|
137
|
-
### [1.0.33](https://gitee.com/ymoo/moyan-api/compare/v1.0.32...v1.0.33) (2022-08-31)
|
|
138
|
-
|
|
139
|
-
### [1.0.32](https://gitee.com/ymoo/moyan-api/compare/v1.0.31...v1.0.32) (2022-04-26)
|
|
140
|
-
|
|
141
|
-
### [1.0.31](https://gitee.com/ymoo/moyan-api/compare/v1.0.30...v1.0.31) (2022-04-11)
|
|
142
|
-
|
|
143
|
-
### [1.0.30](https://gitee.com/ymoo/moyan-api/compare/v1.0.29...v1.0.30) (2022-04-06)
|
|
144
|
-
|
|
145
|
-
### [1.0.29](https://gitee.com/ymoo/moyan-api/compare/v1.0.28...v1.0.29) (2022-04-06)
|
|
146
|
-
|
|
147
|
-
### [1.0.28](https://gitee.com/ymoo/moyan-api/compare/v1.0.27...v1.0.28) (2022-04-06)
|
|
148
|
-
|
|
149
|
-
### [1.0.27](https://gitee.com/ymoo/moyan-api/compare/v1.0.26...v1.0.27) (2022-04-06)
|
|
150
|
-
|
|
151
|
-
### [1.0.26](https://gitee.com/ymoo/moyan-api/compare/v1.0.25...v1.0.26) (2022-04-06)
|
|
152
|
-
|
|
153
|
-
### [1.0.25](https://gitee.com/ymoo/moyan-api/compare/v1.0.24...v1.0.25) (2022-04-06)
|
|
154
|
-
|
|
155
|
-
### [1.0.24](https://gitee.com/ymoo/moyan-api/compare/v1.0.23...v1.0.24) (2022-04-06)
|
|
156
|
-
|
|
157
|
-
### [1.0.23](https://gitee.com/ymoo/moyan-api/compare/v1.0.22...v1.0.23) (2022-04-06)
|
|
158
|
-
|
|
159
|
-
### [1.0.22](https://gitee.com/ymoo/moyan-api/compare/v1.0.21...v1.0.22) (2022-04-06)
|
|
160
|
-
|
|
161
|
-
### [1.0.21](https://gitee.com/ymoo/moyan-api/compare/v1.0.20...v1.0.21) (2022-04-06)
|
|
162
|
-
|
|
163
|
-
### [1.0.20](https://gitee.com/ymoo/moyan-api/compare/v1.0.19...v1.0.20) (2022-04-06)
|
|
164
|
-
|
|
165
|
-
### [1.0.19](https://gitee.com/ymoo/moyan-api/compare/v1.0.18...v1.0.19) (2022-03-31)
|
|
166
|
-
|
|
167
|
-
### [1.0.18](https://gitee.com/ymoo/moyan-api/compare/v1.0.17...v1.0.18) (2022-03-31)
|
|
168
|
-
|
|
169
|
-
### [1.0.17](https://gitee.com/ymoo/moyan-api/compare/v1.0.16...v1.0.17) (2022-03-31)
|
|
170
|
-
|
|
171
|
-
### [1.0.16](https://gitee.com/ymoo/moyan-api/compare/v1.0.15...v1.0.16) (2022-03-31)
|
|
172
|
-
|
|
173
|
-
### [1.0.15](https://gitee.com/ymoo/moyan-api/compare/v1.0.14...v1.0.15) (2022-03-30)
|
|
174
|
-
|
|
175
|
-
### [1.0.14](https://gitee.com/ymoo/moyan-api/compare/v1.0.13...v1.0.14) (2022-03-21)
|
|
176
|
-
|
|
177
|
-
### [1.0.13](https://gitee.com/ymoo/moyan-api/compare/v1.0.12...v1.0.13) (2022-03-17)
|
|
178
|
-
|
|
179
|
-
### [1.0.12](https://gitee.com/ymoo/moyan-api/compare/v1.0.11...v1.0.12) (2022-03-17)
|
|
180
|
-
|
|
181
|
-
### [1.0.11](https://gitee.com/ymoo/moyan-api/compare/v1.0.10...v1.0.11) (2022-03-17)
|
|
182
|
-
|
|
183
|
-
### [1.0.10](https://gitee.com/ymoo/moyan-api/compare/v1.0.9...v1.0.10) (2022-03-17)
|
|
184
|
-
|
|
185
|
-
### [1.0.9](https://gitee.com/ymoo/moyan-api/compare/v1.0.8...v1.0.9) (2022-03-02)
|
|
186
|
-
|
|
187
|
-
### [1.0.8](https://gitee.com/ymoo/moyan-api/compare/v1.0.7...v1.0.8) (2022-03-02)
|
|
188
|
-
|
|
189
|
-
### [1.0.7](https://gitee.com/ymoo/moyan-api/compare/v1.0.6...v1.0.7) (2022-02-28)
|
|
190
|
-
|
|
191
|
-
### [1.0.6](https://gitee.com/ymoo/moyan-api/compare/v1.0.5...v1.0.6) (2022-02-28)
|
|
192
|
-
|
|
193
|
-
### [1.0.5](https://gitee.com/ymoo/moyan-api/compare/v1.0.4...v1.0.5) (2022-02-28)
|
|
194
|
-
|
|
195
|
-
### [1.0.4](https://gitee.com/ymoo/moyan-api/compare/v1.0.3...v1.0.4) (2022-02-28)
|
|
196
|
-
|
|
197
|
-
### [1.0.3](https://gitee.com/ymoo/moyan-api/compare/v1.0.2...v1.0.3) (2022-02-28)
|
|
198
|
-
|
|
199
|
-
### [1.0.2](https://gitee.com/ymoo/moyan-api/compare/v1.0.1...v1.0.2) (2022-02-28)
|
|
200
|
-
|
|
201
|
-
### 1.0.1 (2022-02-28)
|
package/src/creator.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {program} from 'commander'
|
|
3
|
-
import 'colors'
|
|
4
|
-
import {ApisdkCreator} from './main'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
program
|
|
9
|
-
.version(require('../package').version) // 设置版本
|
|
10
|
-
.option('-a, --apijson [apijson]', 'openApi json 文件路径') // 自定义形参[port]
|
|
11
|
-
.option('-o, --output [output]', '输出路径') // 自定义形参[dev]
|
|
12
|
-
.option('-d, --dirname [dirname]', '生成的sdk目录名') //
|
|
13
|
-
.option('-j, --jsonurl [jsonurl]', '远程openApi 地址') // 自定义形参[dev]
|
|
14
|
-
.option('-af, --apiprefix [apiprefix]', '生成的skd 类名前缀') // 自定义形参[dev]
|
|
15
|
-
.option('-pp, --pathprefix [pathprefix]', '添加的路径请求前缀') // 自定义形参[dev]
|
|
16
|
-
.parse(process.argv) // 参数数组
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// console.info(`
|
|
20
|
-
// #######################################################################################
|
|
21
|
-
// #生成 openApi 3.0 调用SDK package.json 配置
|
|
22
|
-
// {
|
|
23
|
-
// ...
|
|
24
|
-
// "moyan-api":{
|
|
25
|
-
// "output":"src", // 生成的文件输出目录,默认值:src
|
|
26
|
-
// "apijson":"moyan.api.json" // 生成sdk 的openApi 3.0 数据 ,默认值:moyan.api.json
|
|
27
|
-
// "jsonurl":"远程获取数据的地址"
|
|
28
|
-
// }
|
|
29
|
-
// }
|
|
30
|
-
// #######################################################################################
|
|
31
|
-
// `)
|
|
32
|
-
|
|
33
|
-
console.info('生成 openApi 3.0 调用SDK... ')
|
|
34
|
-
console.log('apijson',program.getOptionValue('apijson'));
|
|
35
|
-
console.log('output',program.getOptionValue('output'));
|
|
36
|
-
console.log('jsonurl',program.getOptionValue('jsonurl'));
|
|
37
|
-
console.log('apiprefix',program.getOptionValue('apiprefix'));
|
|
38
|
-
console.log('pathprefix',program.getOptionValue('pathprefix'));
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
new ApisdkCreator(program).create()
|
package/src/index.ts
DELETED
package/src/lib/base.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "events"
|
|
2
|
-
|
|
3
|
-
export type MoMethod = "POST" | "GET" | "DELETE" | "PUT" | "ALL"
|
|
4
|
-
export interface ObjectAny {
|
|
5
|
-
[key: string]: any //一般用于字段字段
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Option {
|
|
9
|
-
/**
|
|
10
|
-
* 下载文件的文件名
|
|
11
|
-
*/
|
|
12
|
-
fileName?: string
|
|
13
|
-
/**
|
|
14
|
-
* 是否使用 api的prefix;
|
|
15
|
-
* @default true
|
|
16
|
-
*/
|
|
17
|
-
prefix?: boolean
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 提示成功信息
|
|
21
|
-
* @default true
|
|
22
|
-
*/
|
|
23
|
-
hintSuccess?: boolean
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 提示错误信息
|
|
27
|
-
* @default true
|
|
28
|
-
*/
|
|
29
|
-
hintFail?: boolean
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 是否显示加载蒙版
|
|
33
|
-
* @default false
|
|
34
|
-
*/
|
|
35
|
-
showLoading?: boolean
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 成功提示消息,successMsg可以为string和Function,Function返回提示语, 如果没有定义了改属性,则默认使用api接口返回的message
|
|
39
|
-
* @default null
|
|
40
|
-
*/
|
|
41
|
-
successMsg?: ((res: any) => string) | string | null
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 失败提示消息,failMsg可以为string和Function,Function返回提示语,如果没有定义了改属性,则默认使用api接口返回的message
|
|
45
|
-
* @default null
|
|
46
|
-
*/
|
|
47
|
-
failMsg?: ((err: any) => string) | string | null
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 加载状态
|
|
51
|
-
*/
|
|
52
|
-
loading?: boolean
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 扩展
|
|
56
|
-
*/
|
|
57
|
-
ext?:Record<string,any>
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface ApiCallProps<T> {
|
|
61
|
-
params?: T
|
|
62
|
-
query?:Record<string,string|number>
|
|
63
|
-
option?: Option | {}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export abstract class RequestClass {
|
|
67
|
-
abstract request(apiEntity: ApiEntity): Promise<any>
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface SubApiEntity extends RequestClass {}
|
|
71
|
-
|
|
72
|
-
export enum ApiEvents {
|
|
73
|
-
HintSuccess = "hintSuccess", // 处理请求成功 提示 监听器
|
|
74
|
-
Success = "success", // 处理请求成功 监听器
|
|
75
|
-
HintFail = "hintFail", // 处理请求失败 提示 监听器
|
|
76
|
-
Unauthorized = "unauthorized", // 处理请求失败 401 监听器
|
|
77
|
-
Fail = "fail" // 处理请求失败 监听器
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export abstract class ApiCall<ReqType, ResType> extends Promise<ResType> {
|
|
81
|
-
static MoCall: SubApiEntity
|
|
82
|
-
|
|
83
|
-
static use(moCall: SubApiEntity) {
|
|
84
|
-
ApiCall.MoCall = moCall
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
static hasPrompted = false
|
|
88
|
-
|
|
89
|
-
params!: ReqType | { [key: string]: any }
|
|
90
|
-
result!: ResType & { message?: string }
|
|
91
|
-
|
|
92
|
-
option!: Option
|
|
93
|
-
|
|
94
|
-
static hintSuccessHandler = (result: ApiCall<any, any>) => {}
|
|
95
|
-
|
|
96
|
-
static hintFailHandler = (err: ApiCall<any, any>) => {}
|
|
97
|
-
|
|
98
|
-
static handler401 = (err: ApiCall<any, any>) => {}
|
|
99
|
-
|
|
100
|
-
static emitter = new EventEmitter()
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 请求路径
|
|
104
|
-
*/
|
|
105
|
-
abstract path: string
|
|
106
|
-
|
|
107
|
-
abstract method: MoMethod
|
|
108
|
-
|
|
109
|
-
error: any
|
|
110
|
-
|
|
111
|
-
query: Record<string,string|number>
|
|
112
|
-
|
|
113
|
-
get status() {
|
|
114
|
-
try {
|
|
115
|
-
return this.response.status
|
|
116
|
-
} catch {
|
|
117
|
-
return 0
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
get statusCode() {
|
|
122
|
-
try {
|
|
123
|
-
try {
|
|
124
|
-
this.response.data.statusCode
|
|
125
|
-
} catch {
|
|
126
|
-
return this.response.status
|
|
127
|
-
}
|
|
128
|
-
} catch {
|
|
129
|
-
return 0
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
response: any
|
|
134
|
-
|
|
135
|
-
resolve!: (result: ApiCall<ReqType, ResType>["result"]) => void
|
|
136
|
-
reject!: (err: any) => void
|
|
137
|
-
|
|
138
|
-
get successMsg() {
|
|
139
|
-
if (typeof this.option.successMsg === "string") {
|
|
140
|
-
return this.option.successMsg
|
|
141
|
-
} else if (typeof this.option.successMsg === "function") {
|
|
142
|
-
return this.option.successMsg(this.result)
|
|
143
|
-
} else if (this.result["message"]) {
|
|
144
|
-
return this.result["message"]
|
|
145
|
-
}
|
|
146
|
-
return "请求成功"
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
get failMsg() {
|
|
150
|
-
if (this.error) {
|
|
151
|
-
if (typeof this.option.failMsg === "string") {
|
|
152
|
-
return this.option.failMsg
|
|
153
|
-
} else if (typeof this.option.failMsg === "function") {
|
|
154
|
-
return this.option.failMsg(this.error)
|
|
155
|
-
}
|
|
156
|
-
return this.error.message || this.error.data
|
|
157
|
-
? this.error.data.mssage
|
|
158
|
-
: "请求错误"
|
|
159
|
-
}
|
|
160
|
-
return false
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
constructor(props?: ApiCallProps<ReqType>) {
|
|
165
|
-
if (typeof props === "function") {
|
|
166
|
-
// @ts-ignore
|
|
167
|
-
super(props)
|
|
168
|
-
} else {
|
|
169
|
-
const slef: any = {}
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
super((resolve, reject) => {
|
|
172
|
-
slef.resolve = resolve
|
|
173
|
-
slef.reject = reject
|
|
174
|
-
})
|
|
175
|
-
Object.assign(this, slef)
|
|
176
|
-
setTimeout(() => {
|
|
177
|
-
this.init(props)
|
|
178
|
-
this.exec()
|
|
179
|
-
})
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 安全替换URL模板中的路径参数
|
|
185
|
-
* @param urlTemplate 包含{param}占位符的URL模板
|
|
186
|
-
* @param params 参数键值对对象
|
|
187
|
-
* @returns 替换后的完整URL
|
|
188
|
-
* @throws 当缺少必需参数时抛出错误
|
|
189
|
-
*/
|
|
190
|
-
replaceUrlParams<T extends Record<string, string | number>>(
|
|
191
|
-
urlTemplate: string,
|
|
192
|
-
params: T
|
|
193
|
-
): string {
|
|
194
|
-
return urlTemplate.replace(/\{(\w+)\}/g, (match, paramName) => {
|
|
195
|
-
const value = params[paramName as keyof T];
|
|
196
|
-
if (value === undefined) {
|
|
197
|
-
throw new Error(`Missing required parameter: ${paramName}`);
|
|
198
|
-
}
|
|
199
|
-
return String(value);
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
init(props?: ApiCallProps<ReqType>) {
|
|
204
|
-
const defaultOption: Option = {
|
|
205
|
-
prefix: true,
|
|
206
|
-
hintSuccess: false,
|
|
207
|
-
hintFail: true,
|
|
208
|
-
showLoading: true,
|
|
209
|
-
loading: false,
|
|
210
|
-
successMsg: res => {
|
|
211
|
-
return res.message || "请求成功"
|
|
212
|
-
},
|
|
213
|
-
failMsg: err => {
|
|
214
|
-
return err.message
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
this.query = props?.query ||{}
|
|
219
|
-
this.option = this.option || {}
|
|
220
|
-
if (props && typeof props.option === "object") {
|
|
221
|
-
const option = Object.assign(defaultOption, this.option, props.option)
|
|
222
|
-
Object.assign(props.option, option)
|
|
223
|
-
this.option = props.option
|
|
224
|
-
} else {
|
|
225
|
-
this.option = Object.assign(defaultOption, this.option)
|
|
226
|
-
}
|
|
227
|
-
this.params = props && props.params ? props.params : {}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
exec() {
|
|
231
|
-
this.path = this.replaceUrlParams(this.path,this.query)
|
|
232
|
-
ApiCall.MoCall.request(this)
|
|
233
|
-
.then(response => {
|
|
234
|
-
this.result = response.data
|
|
235
|
-
this.response = response
|
|
236
|
-
if (this.option.hintSuccess && !ApiCall.hasPrompted) {
|
|
237
|
-
ApiCall.hintSuccessHandler(this)
|
|
238
|
-
ApiCall.emitter.emit(ApiEvents.HintSuccess, this)
|
|
239
|
-
}
|
|
240
|
-
ApiCall.emitter.emit(ApiEvents.Success, this)
|
|
241
|
-
this.resolve(response.data)
|
|
242
|
-
})
|
|
243
|
-
.catch(err => {
|
|
244
|
-
this.error = err
|
|
245
|
-
this.response = err.response || {}
|
|
246
|
-
if (this.option.hintFail && !ApiCall.hasPrompted) {
|
|
247
|
-
ApiCall.hintFailHandler(this)
|
|
248
|
-
ApiCall.emitter.emit(ApiEvents.HintFail, this)
|
|
249
|
-
}
|
|
250
|
-
if (this.status === 401) {
|
|
251
|
-
ApiCall.handler401(this)
|
|
252
|
-
ApiCall.emitter.emit(ApiEvents.Unauthorized, this)
|
|
253
|
-
}
|
|
254
|
-
ApiCall.emitter.emit(ApiEvents.Fail, this)
|
|
255
|
-
this.reject(err)
|
|
256
|
-
})
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export interface ApiEntity extends ApiCall<any, any> {}
|