moyan-api 1.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/.eslintrc.js +28 -0
- package/.gitee/ISSUE_TEMPLATE.zh-CN.md +13 -0
- package/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +11 -0
- package/.prettierrc +5 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.en.md +36 -0
- package/README.md +153 -0
- package/dist/apiv2/axios.d.ts +13 -0
- package/dist/apiv2/axios.js +69 -0
- package/dist/apiv2/axios.js.map +1 -0
- package/dist/apiv2/base.d.ts +43 -0
- package/dist/apiv2/base.js +93 -0
- package/dist/apiv2/base.js.map +1 -0
- package/dist/apiv2/config.d.ts +1 -0
- package/dist/apiv2/config.js +26 -0
- package/dist/apiv2/config.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/axios.d.ts +13 -0
- package/dist/lib/axios.js +71 -0
- package/dist/lib/axios.js.map +1 -0
- package/dist/lib/base.d.ts +48 -0
- package/dist/lib/base.js +95 -0
- package/dist/lib/base.js.map +1 -0
- package/dist/lib/config.d.ts +1 -0
- package/dist/lib/config.js +26 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/main.d.ts +15 -0
- package/dist/main.js +75 -0
- package/dist/main.js.map +1 -0
- package/dist/template/api.d.ts +16 -0
- package/dist/template/api.js +90 -0
- package/dist/template/api.js.map +1 -0
- package/dist/template/schemas.d.ts +13 -0
- package/dist/template/schemas.js +89 -0
- package/dist/template/schemas.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/mo.d.ts +28 -0
- package/dist/utils/mo.js +254 -0
- package/dist/utils/mo.js.map +1 -0
- package/package.json +38 -0
- package/src/index.ts +38 -0
- package/src/lib/base.ts +180 -0
- package/src/main.ts +90 -0
- package/src/template/api.ts +102 -0
- package/src/template/schemas.ts +89 -0
- package/src/utils/mo.ts +402 -0
- package/tsconfig.json +20 -0
- package/view/api.ejs +25 -0
- package/view/schemas.ejs +12 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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
ADDED
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# moyan-api
|
|
2
|
+
|
|
3
|
+
#### Description
|
|
4
|
+
OpenApi 3.0 数据生成api调用sdk
|
|
5
|
+
|
|
6
|
+
#### Software Architecture
|
|
7
|
+
Software architecture description
|
|
8
|
+
|
|
9
|
+
#### Installation
|
|
10
|
+
|
|
11
|
+
1. xxxx
|
|
12
|
+
2. xxxx
|
|
13
|
+
3. xxxx
|
|
14
|
+
|
|
15
|
+
#### Instructions
|
|
16
|
+
|
|
17
|
+
1. xxxx
|
|
18
|
+
2. xxxx
|
|
19
|
+
3. xxxx
|
|
20
|
+
|
|
21
|
+
#### Contribution
|
|
22
|
+
|
|
23
|
+
1. Fork the repository
|
|
24
|
+
2. Create Feat_xxx branch
|
|
25
|
+
3. Commit your code
|
|
26
|
+
4. Create Pull Request
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
#### Gitee Feature
|
|
30
|
+
|
|
31
|
+
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
|
|
32
|
+
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
|
|
33
|
+
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
|
|
34
|
+
4. The most valuable open source project [GVP](https://gitee.com/gvp)
|
|
35
|
+
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
|
|
36
|
+
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
...
|
|
16
|
+
"moyan-api":{
|
|
17
|
+
"output":"src", // 生成的文件输出目录,默认值:src
|
|
18
|
+
"apijson":"moyan.api.json" // 生成sdk 的openApi 3.0 数据 ,默认值:moyan.api.json
|
|
19
|
+
"jsonurl":"远程获取数据的地址"
|
|
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
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import { ApiEntity } from './base';
|
|
3
|
+
declare const AXIOS: unique symbol;
|
|
4
|
+
export declare class MoAxios {
|
|
5
|
+
static [AXIOS]: AxiosInstance;
|
|
6
|
+
options: any;
|
|
7
|
+
constructor(options?: any);
|
|
8
|
+
get $axios(): AxiosInstance;
|
|
9
|
+
get config(): any;
|
|
10
|
+
init(): void;
|
|
11
|
+
request(apiEntity: ApiEntity): import("axios").AxiosPromise<any>;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MoAxios = void 0;
|
|
4
|
+
const config_1 = require("../../config");
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
const AXIOS = Symbol('mo#Api#axios');
|
|
7
|
+
class MoAxios {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = {};
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
get $axios() {
|
|
13
|
+
if (!MoAxios[AXIOS]) {
|
|
14
|
+
MoAxios[AXIOS] = axios_1.default.create(this.config.axios);
|
|
15
|
+
this.init();
|
|
16
|
+
}
|
|
17
|
+
return MoAxios[AXIOS];
|
|
18
|
+
}
|
|
19
|
+
get config() {
|
|
20
|
+
return (0, config_1.getConfig)();
|
|
21
|
+
}
|
|
22
|
+
init() {
|
|
23
|
+
this.$axios.interceptors.response.use((res) => {
|
|
24
|
+
if (typeof this.options.render === 'function') {
|
|
25
|
+
return this.options.render(res);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return res;
|
|
29
|
+
}
|
|
30
|
+
}, async (error) => {
|
|
31
|
+
if (error && error.response) {
|
|
32
|
+
switch (error.response.status) {
|
|
33
|
+
case 502:
|
|
34
|
+
error.message = '网关错误';
|
|
35
|
+
break;
|
|
36
|
+
case 504:
|
|
37
|
+
error.message = '网关超时';
|
|
38
|
+
break;
|
|
39
|
+
case 505:
|
|
40
|
+
error.message = '版本不受支持';
|
|
41
|
+
break;
|
|
42
|
+
default:
|
|
43
|
+
error.message = error.response.data.message;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
request(apiEntity) {
|
|
51
|
+
const url = `${apiEntity.path}`;
|
|
52
|
+
const requsetOption = {
|
|
53
|
+
responseType: 'json',
|
|
54
|
+
baseURL: this.config.moApi.baseURL,
|
|
55
|
+
url,
|
|
56
|
+
method: apiEntity.method,
|
|
57
|
+
headers: {}
|
|
58
|
+
};
|
|
59
|
+
if (apiEntity.method === 'GET') {
|
|
60
|
+
requsetOption.params = { params: apiEntity.params };
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
requsetOption.data = apiEntity.params;
|
|
64
|
+
}
|
|
65
|
+
return this.$axios(requsetOption);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.MoAxios = MoAxios;
|
|
69
|
+
//# sourceMappingURL=axios.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axios.js","sourceRoot":"","sources":["../../src/apiv2/axios.ts"],"names":[],"mappings":";;;AAAA,yCAAwC;AACxC,iCAAgE;AAEhE,MAAM,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,CAAA;AAEpC,MAAa,OAAO;IAIlB,YAAY,UAAe,EAAE;QAF7B,YAAO,GAAQ,EAAE,CAAA;QAGf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,IAAI,MAAM;QACR,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YACnB,OAAO,CAAC,KAAK,CAAC,GAAG,eAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAChD,IAAI,CAAC,IAAI,EAAE,CAAA;SACZ;QACD,OAAO,OAAO,CAAC,KAAK,CAAC,CAAA;IACvB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAA,kBAAS,GAAE,CAAA;IACpB,CAAC;IAED,IAAI;QACF,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CACnC,CAAC,GAAG,EAAE,EAAE;YACN,IAAI,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE;gBAC7C,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;aAChC;iBAAM;gBACL,OAAO,GAAG,CAAA;aACX;QACH,CAAC,EACD,KAAK,EAAE,KAAK,EAAE,EAAE;YAEd,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,EAAE;gBAC3B,QAAQ,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE;oBAC7B,KAAK,GAAG;wBACN,KAAK,CAAC,OAAO,GAAG,MAAM,CAAA;wBACtB,MAAK;oBACP,KAAK,GAAG;wBACN,KAAK,CAAC,OAAO,GAAG,MAAM,CAAA;wBACtB,MAAK;oBACP,KAAK,GAAG;wBACN,KAAK,CAAC,OAAO,GAAG,QAAQ,CAAA;wBACxB,MAAK;oBACP;wBACE,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAA;wBAC3C,MAAK;iBACR;aACF;YACD,MAAM,KAAK,CAAA;QACb,CAAC,CACF,CAAA;IACH,CAAC;IAED,OAAO,CAAC,SAAoB;QAC1B,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC,IAAI,EAAE,CAAA;QAC/B,MAAM,aAAa,GAAuB;YACxC,YAAY,EAAE,MAAM;YACpB,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO;YAClC,GAAG;YACH,MAAM,EAAE,SAAS,CAAC,MAAM;YACxB,OAAO,EAAE,EAAE;SACZ,CAAA;QAED,IAAI,SAAS,CAAC,MAAM,KAAK,KAAK,EAAE;YAC9B,aAAa,CAAC,MAAM,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,CAAA;SACpD;aAAM;YACL,aAAa,CAAC,IAAI,GAAG,SAAS,CAAC,MAAM,CAAA;SACtC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IACnC,CAAC;CACF;AArED,0BAqEC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { MoAxios } from '../apiv2/axios';
|
|
2
|
+
export declare type MoMethod = 'POST' | 'GET' | 'DELETE' | 'PUT';
|
|
3
|
+
export interface ObjectAny {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
export interface Option {
|
|
7
|
+
fileName?: string;
|
|
8
|
+
prefix?: boolean;
|
|
9
|
+
hintSuccess?: boolean;
|
|
10
|
+
hintFail?: boolean;
|
|
11
|
+
showLoading?: boolean;
|
|
12
|
+
successMsg?: ((res: any) => string) | string | null;
|
|
13
|
+
failMsg?: ((err: any) => string) | string | null;
|
|
14
|
+
loading?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface ApiCallProps<T> {
|
|
17
|
+
params?: T;
|
|
18
|
+
option?: Option | {};
|
|
19
|
+
}
|
|
20
|
+
export declare abstract class ApiCall<ReqType, ResType> extends Promise<ResType & ObjectAny> {
|
|
21
|
+
static MoCall: MoAxios;
|
|
22
|
+
static hasPrompted: boolean;
|
|
23
|
+
params: ReqType | {
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
};
|
|
26
|
+
result: ResType & ObjectAny;
|
|
27
|
+
option: Option;
|
|
28
|
+
static hintSuccessHandler: (result: any) => void;
|
|
29
|
+
static hintFailHandler: (err: any) => void;
|
|
30
|
+
abstract path: string;
|
|
31
|
+
abstract method: MoMethod;
|
|
32
|
+
error: any;
|
|
33
|
+
response: any;
|
|
34
|
+
resolve: (result: ApiCall<ReqType, ResType>['result']) => void;
|
|
35
|
+
reject: (err: any) => void;
|
|
36
|
+
get successMsg(): any;
|
|
37
|
+
get failMsg(): any;
|
|
38
|
+
constructor(props?: ApiCallProps<ReqType>);
|
|
39
|
+
init(props?: ApiCallProps<ReqType>): void;
|
|
40
|
+
exec(): void;
|
|
41
|
+
}
|
|
42
|
+
export interface ApiEntity extends ApiCall<any, any> {
|
|
43
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiCall = void 0;
|
|
4
|
+
const axios_1 = require("../apiv2/axios");
|
|
5
|
+
class ApiCall extends Promise {
|
|
6
|
+
constructor(props) {
|
|
7
|
+
if (typeof props === 'function') {
|
|
8
|
+
super(props);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
const slef = {};
|
|
12
|
+
super((resolve, reject) => {
|
|
13
|
+
slef.resolve = resolve;
|
|
14
|
+
slef.reject = reject;
|
|
15
|
+
});
|
|
16
|
+
Object.assign(this, slef);
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
this.init(props);
|
|
19
|
+
this.exec();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
get successMsg() {
|
|
24
|
+
if (typeof this.option.successMsg === 'string') {
|
|
25
|
+
return this.option.successMsg;
|
|
26
|
+
}
|
|
27
|
+
else if (typeof this.option.successMsg === 'function') {
|
|
28
|
+
return this.option.successMsg(this.result);
|
|
29
|
+
}
|
|
30
|
+
else if (this.result.message) {
|
|
31
|
+
return this.result.message;
|
|
32
|
+
}
|
|
33
|
+
return '请求成功';
|
|
34
|
+
}
|
|
35
|
+
get failMsg() {
|
|
36
|
+
if (this.error) {
|
|
37
|
+
if (typeof this.option.failMsg === 'string') {
|
|
38
|
+
return this.option.failMsg;
|
|
39
|
+
}
|
|
40
|
+
else if (typeof this.option.failMsg === 'function') {
|
|
41
|
+
return this.option.failMsg(this.error);
|
|
42
|
+
}
|
|
43
|
+
return this.error.message || this.error.data ? this.error.data.mssage : '请求错误';
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
init(props) {
|
|
48
|
+
const defaultOption = {
|
|
49
|
+
prefix: true,
|
|
50
|
+
hintSuccess: false,
|
|
51
|
+
hintFail: true,
|
|
52
|
+
showLoading: true,
|
|
53
|
+
loading: false,
|
|
54
|
+
successMsg: (res) => {
|
|
55
|
+
return res.message || '请求成功';
|
|
56
|
+
},
|
|
57
|
+
failMsg: (err) => {
|
|
58
|
+
return err.message;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
this.option = this.option || {};
|
|
62
|
+
if (props && typeof props.option === 'object') {
|
|
63
|
+
const option = Object.assign(defaultOption, this.option, props.option);
|
|
64
|
+
Object.assign(props.option, option);
|
|
65
|
+
this.option = props.option;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.option = Object.assign(defaultOption, this.option);
|
|
69
|
+
}
|
|
70
|
+
this.params = props && props.params ? props.params : {};
|
|
71
|
+
}
|
|
72
|
+
exec() {
|
|
73
|
+
ApiCall.MoCall.request(this)
|
|
74
|
+
.then((response) => {
|
|
75
|
+
this.result = response.data;
|
|
76
|
+
console.info('*()', this.option.hintSuccess && !ApiCall.hasPrompted);
|
|
77
|
+
this.option.hintSuccess && !ApiCall.hasPrompted && ApiCall.hintSuccessHandler(this);
|
|
78
|
+
this.resolve(response.data);
|
|
79
|
+
})
|
|
80
|
+
.catch((err) => {
|
|
81
|
+
this.error = err;
|
|
82
|
+
console.info('*****************>????', err);
|
|
83
|
+
this.option.hintSuccess && !ApiCall.hasPrompted && ApiCall.hintFailHandler(this);
|
|
84
|
+
this.reject(err);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ApiCall = ApiCall;
|
|
89
|
+
ApiCall.MoCall = new axios_1.MoAxios();
|
|
90
|
+
ApiCall.hasPrompted = false;
|
|
91
|
+
ApiCall.hintSuccessHandler = (result) => { };
|
|
92
|
+
ApiCall.hintFailHandler = (err) => { };
|
|
93
|
+
//# sourceMappingURL=base.js.map
|