@ibiz-template/core 0.0.1-alpha.33 → 0.0.1-alpha.37
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/dist/system/index.system.js +1 -1
- package/out/environment/environment.d.ts.map +1 -1
- package/out/environment/environment.js +2 -0
- package/out/ibizsys.d.ts +8 -1
- package/out/ibizsys.d.ts.map +1 -1
- package/out/ibizsys.js +8 -1
- package/out/install.js +2 -2
- package/out/interface/i-environment/i-environment.d.ts +17 -0
- package/out/interface/i-environment/i-environment.d.ts.map +1 -1
- package/out/utils/index.d.ts +2 -1
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +2 -1
- package/out/utils/interceptor/core-interceptor.d.ts +15 -0
- package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
- package/out/utils/interceptor/core-interceptor.js +30 -0
- package/out/utils/interceptor/index.d.ts +3 -0
- package/out/utils/interceptor/index.d.ts.map +1 -0
- package/out/utils/interceptor/index.js +2 -0
- package/out/utils/interceptor/interceptor.d.ts +74 -3
- package/out/utils/interceptor/interceptor.d.ts.map +1 -1
- package/out/utils/interceptor/interceptor.js +75 -22
- package/out/utils/logger/logger.d.ts +3 -0
- package/out/utils/logger/logger.d.ts.map +1 -0
- package/out/utils/logger/logger.js +2 -0
- package/out/utils/net/net.d.ts +66 -22
- package/out/utils/net/net.d.ts.map +1 -1
- package/out/utils/net/net.js +97 -35
- package/package.json +5 -4
- package/src/environment/environment.ts +2 -0
- package/src/ibizsys.ts +9 -1
- package/src/install.ts +2 -2
- package/src/interface/i-environment/i-environment.ts +19 -0
- package/src/utils/index.ts +2 -1
- package/src/utils/interceptor/core-interceptor.ts +35 -0
- package/src/utils/interceptor/index.ts +2 -0
- package/src/utils/interceptor/interceptor.ts +111 -22
- package/src/utils/logger/logger.ts +3 -0
- package/src/utils/net/net.ts +122 -40
package/src/utils/net/net.ts
CHANGED
|
@@ -2,10 +2,15 @@ import axios, {
|
|
|
2
2
|
AxiosError,
|
|
3
3
|
RawAxiosRequestHeaders,
|
|
4
4
|
AxiosResponse,
|
|
5
|
+
AxiosRequestConfig,
|
|
6
|
+
AxiosInstance,
|
|
7
|
+
CreateAxiosDefaults,
|
|
5
8
|
} from 'axios';
|
|
9
|
+
import { merge } from 'lodash-es';
|
|
6
10
|
import { stringify } from 'qs';
|
|
7
11
|
import { notNilEmpty } from 'qx-util';
|
|
8
12
|
import { HttpError } from '../../error';
|
|
13
|
+
import { Interceptor } from '../interceptor/interceptor';
|
|
9
14
|
import { IHttpResponse } from './http-response';
|
|
10
15
|
|
|
11
16
|
/**
|
|
@@ -18,30 +23,96 @@ import { IHttpResponse } from './http-response';
|
|
|
18
23
|
*/
|
|
19
24
|
export class Net {
|
|
20
25
|
/**
|
|
21
|
-
*
|
|
26
|
+
* axios实例
|
|
22
27
|
*
|
|
23
|
-
* @author
|
|
24
|
-
* @date 2022-
|
|
28
|
+
* @author lxm
|
|
29
|
+
* @date 2022-10-27 17:10:18
|
|
25
30
|
* @protected
|
|
26
|
-
* @type {
|
|
31
|
+
* @type {AxiosInstance}
|
|
27
32
|
*/
|
|
28
|
-
protected
|
|
33
|
+
protected instance: AxiosInstance;
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Creates an instance of Net.
|
|
37
|
+
* @author lxm
|
|
38
|
+
* @date 2022-10-27 16:10:05
|
|
39
|
+
* @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
|
|
40
|
+
*/
|
|
41
|
+
constructor(config?: CreateAxiosDefaults) {
|
|
42
|
+
this.instance = axios.create(config);
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
/**
|
|
35
|
-
*
|
|
46
|
+
* 注册的拦截器
|
|
36
47
|
*
|
|
37
|
-
* @author
|
|
38
|
-
* @date 2022-
|
|
39
|
-
* @
|
|
48
|
+
* @author lxm
|
|
49
|
+
* @date 2022-10-27 17:10:18
|
|
50
|
+
* @type {Map<string, Interceptor>}
|
|
51
|
+
*/
|
|
52
|
+
interceptors: Map<string, Interceptor> = new Map();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 添加拦截器
|
|
56
|
+
*
|
|
57
|
+
* @author lxm
|
|
58
|
+
* @date 2022-10-27 17:10:42
|
|
59
|
+
* @param {string} name 唯一标识
|
|
60
|
+
* @param {Interceptor} interceptor 拦截器
|
|
61
|
+
*/
|
|
62
|
+
addInterceptor(name: string, interceptor: Interceptor) {
|
|
63
|
+
interceptor.use(this.instance);
|
|
64
|
+
this.interceptors.set(name, interceptor);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 删除拦截器
|
|
69
|
+
*
|
|
70
|
+
* @author lxm
|
|
71
|
+
* @date 2022-10-27 17:10:27
|
|
72
|
+
* @param {string} name 唯一标识
|
|
73
|
+
*/
|
|
74
|
+
removeInterceptor(name: string) {
|
|
75
|
+
const interceptor = this.interceptors.get(name);
|
|
76
|
+
if (interceptor) {
|
|
77
|
+
interceptor.eject(this.instance);
|
|
78
|
+
this.interceptors.delete(name);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 预置config,绑定动态的配置
|
|
84
|
+
*
|
|
85
|
+
* @author lxm
|
|
86
|
+
* @date 2022-10-27 16:10:48
|
|
87
|
+
* @readonly
|
|
88
|
+
* @protected
|
|
89
|
+
* @type {AxiosRequestConfig}
|
|
90
|
+
*/
|
|
91
|
+
protected get presetConfig(): AxiosRequestConfig {
|
|
92
|
+
return {
|
|
93
|
+
// 请求前缀路径
|
|
94
|
+
baseURL: this.instance.defaults.baseURL || ibiz.env.baseUrl,
|
|
95
|
+
headers: {
|
|
96
|
+
'Content-Type': 'application/json;charset=UTF-8',
|
|
97
|
+
Accept: 'application/json',
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 从左到右递归合并配置参数(内置第一个合并的预置参数)
|
|
104
|
+
*
|
|
105
|
+
* @author lxm
|
|
106
|
+
* @date 2022-10-27 16:10:09
|
|
107
|
+
* @protected
|
|
108
|
+
* @param {...AxiosRequestConfig[]} configs
|
|
109
|
+
* @returns {*}
|
|
40
110
|
*/
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
this.
|
|
111
|
+
protected mergeConfig(...configs: AxiosRequestConfig[]) {
|
|
112
|
+
if (configs.length === 0) {
|
|
113
|
+
return this.presetConfig;
|
|
44
114
|
}
|
|
115
|
+
return merge(this.presetConfig, ...configs);
|
|
45
116
|
}
|
|
46
117
|
|
|
47
118
|
/**
|
|
@@ -63,15 +134,10 @@ export class Net {
|
|
|
63
134
|
): Promise<IHttpResponse> {
|
|
64
135
|
url = this.handleAppPresetParam(url, params);
|
|
65
136
|
try {
|
|
66
|
-
const response = await
|
|
137
|
+
const response = await this.request(url, {
|
|
67
138
|
method: 'post',
|
|
68
|
-
url,
|
|
69
139
|
data,
|
|
70
|
-
headers
|
|
71
|
-
'Content-Type': 'application/json;charset=UTF-8',
|
|
72
|
-
Accept: 'application/json',
|
|
73
|
-
...headers,
|
|
74
|
-
},
|
|
140
|
+
headers,
|
|
75
141
|
});
|
|
76
142
|
return this.doResponseResult(response);
|
|
77
143
|
} catch (error) {
|
|
@@ -94,7 +160,7 @@ export class Net {
|
|
|
94
160
|
url: string,
|
|
95
161
|
params: IParams = {},
|
|
96
162
|
headers: RawAxiosRequestHeaders = {},
|
|
97
|
-
option:
|
|
163
|
+
option: AxiosRequestConfig = {},
|
|
98
164
|
): Promise<IHttpResponse> {
|
|
99
165
|
// if (params.srfparentdata) {
|
|
100
166
|
// Object.assign(params, params.srfparentdata);
|
|
@@ -102,7 +168,10 @@ export class Net {
|
|
|
102
168
|
// }
|
|
103
169
|
url = this.attachUrlParam(url, params);
|
|
104
170
|
try {
|
|
105
|
-
const response = await
|
|
171
|
+
const response = await this.request(
|
|
172
|
+
url,
|
|
173
|
+
merge({ method: 'get', headers }, option),
|
|
174
|
+
);
|
|
106
175
|
return this.doResponseResult(response);
|
|
107
176
|
} catch (error) {
|
|
108
177
|
throw new HttpError(error as AxiosError);
|
|
@@ -126,7 +195,7 @@ export class Net {
|
|
|
126
195
|
): Promise<IHttpResponse> {
|
|
127
196
|
url = this.handleAppPresetParam(url, params);
|
|
128
197
|
try {
|
|
129
|
-
const response = await
|
|
198
|
+
const response = await this.request(url, { method: 'delete', headers });
|
|
130
199
|
return this.doResponseResult(response);
|
|
131
200
|
} catch (error) {
|
|
132
201
|
throw new HttpError(error as AxiosError);
|
|
@@ -152,7 +221,11 @@ export class Net {
|
|
|
152
221
|
): Promise<IHttpResponse> {
|
|
153
222
|
url = this.handleAppPresetParam(url, params);
|
|
154
223
|
try {
|
|
155
|
-
const response = await
|
|
224
|
+
const response = await this.request(url, {
|
|
225
|
+
method: 'put',
|
|
226
|
+
data,
|
|
227
|
+
headers,
|
|
228
|
+
});
|
|
156
229
|
return this.doResponseResult(response);
|
|
157
230
|
} catch (error) {
|
|
158
231
|
throw new HttpError(error as AxiosError);
|
|
@@ -173,7 +246,7 @@ export class Net {
|
|
|
173
246
|
headers: RawAxiosRequestHeaders = {},
|
|
174
247
|
): Promise<IHttpResponse> {
|
|
175
248
|
try {
|
|
176
|
-
const response = await
|
|
249
|
+
const response = await this.instance.get(url, {
|
|
177
250
|
headers,
|
|
178
251
|
});
|
|
179
252
|
return this.doResponseResult(response);
|
|
@@ -182,6 +255,29 @@ export class Net {
|
|
|
182
255
|
}
|
|
183
256
|
}
|
|
184
257
|
|
|
258
|
+
/**
|
|
259
|
+
* 基础请求方法,会合并预置配置
|
|
260
|
+
*
|
|
261
|
+
* @author lxm
|
|
262
|
+
* @date 2022-10-27 14:10:06
|
|
263
|
+
* @param {string} url
|
|
264
|
+
* @param {AxiosRequestConfig} [config={}]
|
|
265
|
+
* @returns {*} {Promise<IHttpResponse>}
|
|
266
|
+
*/
|
|
267
|
+
async request(
|
|
268
|
+
url: string,
|
|
269
|
+
config: AxiosRequestConfig = {},
|
|
270
|
+
): Promise<IHttpResponse> {
|
|
271
|
+
try {
|
|
272
|
+
const response = await this.instance.request(
|
|
273
|
+
this.mergeConfig({ url }, config),
|
|
274
|
+
);
|
|
275
|
+
return this.doResponseResult(response);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
throw new HttpError(error as AxiosError);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
185
281
|
/**
|
|
186
282
|
* 统一处理请求返回
|
|
187
283
|
*
|
|
@@ -234,7 +330,6 @@ export class Net {
|
|
|
234
330
|
* @return {*} {string}
|
|
235
331
|
*/
|
|
236
332
|
private attachUrlParam(url: string, params: IParams): string {
|
|
237
|
-
url = this.attachUrl(url);
|
|
238
333
|
const strParams: string = stringify(params);
|
|
239
334
|
if (notNilEmpty(strParams)) {
|
|
240
335
|
if (url.endsWith('?')) {
|
|
@@ -249,17 +344,4 @@ export class Net {
|
|
|
249
344
|
}
|
|
250
345
|
return url;
|
|
251
346
|
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* 处理 url 参数
|
|
255
|
-
*
|
|
256
|
-
* @author chitanda
|
|
257
|
-
* @date 2022-07-20 20:07:17
|
|
258
|
-
* @private
|
|
259
|
-
* @param {string} url
|
|
260
|
-
* @return {*} {string}
|
|
261
|
-
*/
|
|
262
|
-
private attachUrl(url: string): string {
|
|
263
|
-
return `${this.prefix}${url}`;
|
|
264
|
-
}
|
|
265
347
|
}
|