@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.
Files changed (38) hide show
  1. package/dist/system/index.system.js +1 -1
  2. package/out/environment/environment.d.ts.map +1 -1
  3. package/out/environment/environment.js +2 -0
  4. package/out/ibizsys.d.ts +8 -1
  5. package/out/ibizsys.d.ts.map +1 -1
  6. package/out/ibizsys.js +8 -1
  7. package/out/install.js +2 -2
  8. package/out/interface/i-environment/i-environment.d.ts +17 -0
  9. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  10. package/out/utils/index.d.ts +2 -1
  11. package/out/utils/index.d.ts.map +1 -1
  12. package/out/utils/index.js +2 -1
  13. package/out/utils/interceptor/core-interceptor.d.ts +15 -0
  14. package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
  15. package/out/utils/interceptor/core-interceptor.js +30 -0
  16. package/out/utils/interceptor/index.d.ts +3 -0
  17. package/out/utils/interceptor/index.d.ts.map +1 -0
  18. package/out/utils/interceptor/index.js +2 -0
  19. package/out/utils/interceptor/interceptor.d.ts +74 -3
  20. package/out/utils/interceptor/interceptor.d.ts.map +1 -1
  21. package/out/utils/interceptor/interceptor.js +75 -22
  22. package/out/utils/logger/logger.d.ts +3 -0
  23. package/out/utils/logger/logger.d.ts.map +1 -0
  24. package/out/utils/logger/logger.js +2 -0
  25. package/out/utils/net/net.d.ts +66 -22
  26. package/out/utils/net/net.d.ts.map +1 -1
  27. package/out/utils/net/net.js +97 -35
  28. package/package.json +5 -4
  29. package/src/environment/environment.ts +2 -0
  30. package/src/ibizsys.ts +9 -1
  31. package/src/install.ts +2 -2
  32. package/src/interface/i-environment/i-environment.ts +19 -0
  33. package/src/utils/index.ts +2 -1
  34. package/src/utils/interceptor/core-interceptor.ts +35 -0
  35. package/src/utils/interceptor/index.ts +2 -0
  36. package/src/utils/interceptor/interceptor.ts +111 -22
  37. package/src/utils/logger/logger.ts +3 -0
  38. package/src/utils/net/net.ts +122 -40
@@ -1,4 +1,5 @@
1
1
  import axios from 'axios';
2
+ import { merge } from 'lodash-es';
2
3
  import { stringify } from 'qs';
3
4
  import { notNilEmpty } from 'qx-util';
4
5
  import { HttpError } from '../../error';
@@ -13,27 +14,80 @@ import { HttpError } from '../../error';
13
14
  export class Net {
14
15
  /**
15
16
  * Creates an instance of Net.
16
- *
17
- * @author chitanda
18
- * @date 2022-07-21 10:07:27
19
- * @param {string} [prefix] 请求前缀
17
+ * @author lxm
18
+ * @date 2022-10-27 16:10:05
19
+ * @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
20
20
  */
21
- constructor(prefix) {
21
+ constructor(config) {
22
22
  /**
23
- * 标准请求前缀
23
+ * 注册的拦截器
24
24
  *
25
- * @author chitanda
26
- * @date 2022-07-21 10:07:48
27
- * @protected
28
- * @type {string}
25
+ * @author lxm
26
+ * @date 2022-10-27 17:10:18
27
+ * @type {Map<string, Interceptor>}
29
28
  */
30
- this._prefix = '';
31
- if (prefix) {
32
- this._prefix = prefix;
29
+ this.interceptors = new Map();
30
+ this.instance = axios.create(config);
31
+ }
32
+ /**
33
+ * 添加拦截器
34
+ *
35
+ * @author lxm
36
+ * @date 2022-10-27 17:10:42
37
+ * @param {string} name 唯一标识
38
+ * @param {Interceptor} interceptor 拦截器
39
+ */
40
+ addInterceptor(name, interceptor) {
41
+ interceptor.use(this.instance);
42
+ this.interceptors.set(name, interceptor);
43
+ }
44
+ /**
45
+ * 删除拦截器
46
+ *
47
+ * @author lxm
48
+ * @date 2022-10-27 17:10:27
49
+ * @param {string} name 唯一标识
50
+ */
51
+ removeInterceptor(name) {
52
+ const interceptor = this.interceptors.get(name);
53
+ if (interceptor) {
54
+ interceptor.eject(this.instance);
55
+ this.interceptors.delete(name);
33
56
  }
34
57
  }
35
- get prefix() {
36
- return this._prefix || ibiz.env.baseUrl;
58
+ /**
59
+ * 预置config,绑定动态的配置
60
+ *
61
+ * @author lxm
62
+ * @date 2022-10-27 16:10:48
63
+ * @readonly
64
+ * @protected
65
+ * @type {AxiosRequestConfig}
66
+ */
67
+ get presetConfig() {
68
+ return {
69
+ // 请求前缀路径
70
+ baseURL: this.instance.defaults.baseURL || ibiz.env.baseUrl,
71
+ headers: {
72
+ 'Content-Type': 'application/json;charset=UTF-8',
73
+ Accept: 'application/json',
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * 从左到右递归合并配置参数(内置第一个合并的预置参数)
79
+ *
80
+ * @author lxm
81
+ * @date 2022-10-27 16:10:09
82
+ * @protected
83
+ * @param {...AxiosRequestConfig[]} configs
84
+ * @returns {*}
85
+ */
86
+ mergeConfig(...configs) {
87
+ if (configs.length === 0) {
88
+ return this.presetConfig;
89
+ }
90
+ return merge(this.presetConfig, ...configs);
37
91
  }
38
92
  /**
39
93
  * Post 请求
@@ -49,11 +103,10 @@ export class Net {
49
103
  async post(url, data, params = {}, headers = {}) {
50
104
  url = this.handleAppPresetParam(url, params);
51
105
  try {
52
- const response = await axios({
106
+ const response = await this.request(url, {
53
107
  method: 'post',
54
- url,
55
108
  data,
56
- headers: Object.assign({ 'Content-Type': 'application/json;charset=UTF-8', Accept: 'application/json' }, headers),
109
+ headers,
57
110
  });
58
111
  return this.doResponseResult(response);
59
112
  }
@@ -79,7 +132,7 @@ export class Net {
79
132
  // }
80
133
  url = this.attachUrlParam(url, params);
81
134
  try {
82
- const response = await axios.get(url, Object.assign({ headers }, option));
135
+ const response = await this.request(url, merge({ method: 'get', headers }, option));
83
136
  return this.doResponseResult(response);
84
137
  }
85
138
  catch (error) {
@@ -99,7 +152,7 @@ export class Net {
99
152
  async delete(url, params, headers = {}) {
100
153
  url = this.handleAppPresetParam(url, params);
101
154
  try {
102
- const response = await axios.delete(url, { headers });
155
+ const response = await this.request(url, { method: 'delete', headers });
103
156
  return this.doResponseResult(response);
104
157
  }
105
158
  catch (error) {
@@ -120,7 +173,11 @@ export class Net {
120
173
  async put(url, data, params = {}, headers = {}) {
121
174
  url = this.handleAppPresetParam(url, params);
122
175
  try {
123
- const response = await axios.put(url, data, { headers });
176
+ const response = await this.request(url, {
177
+ method: 'put',
178
+ data,
179
+ headers,
180
+ });
124
181
  return this.doResponseResult(response);
125
182
  }
126
183
  catch (error) {
@@ -138,7 +195,7 @@ export class Net {
138
195
  */
139
196
  async getModel(url, headers = {}) {
140
197
  try {
141
- const response = await axios.get(url, {
198
+ const response = await this.instance.get(url, {
142
199
  headers,
143
200
  });
144
201
  return this.doResponseResult(response);
@@ -147,6 +204,24 @@ export class Net {
147
204
  throw new HttpError(error);
148
205
  }
149
206
  }
207
+ /**
208
+ * 基础请求方法,会合并预置配置
209
+ *
210
+ * @author lxm
211
+ * @date 2022-10-27 14:10:06
212
+ * @param {string} url
213
+ * @param {AxiosRequestConfig} [config={}]
214
+ * @returns {*} {Promise<IHttpResponse>}
215
+ */
216
+ async request(url, config = {}) {
217
+ try {
218
+ const response = await this.instance.request(this.mergeConfig({ url }, config));
219
+ return this.doResponseResult(response);
220
+ }
221
+ catch (error) {
222
+ throw new HttpError(error);
223
+ }
224
+ }
150
225
  /**
151
226
  * 统一处理请求返回
152
227
  *
@@ -197,7 +272,6 @@ export class Net {
197
272
  * @return {*} {string}
198
273
  */
199
274
  attachUrlParam(url, params) {
200
- url = this.attachUrl(url);
201
275
  const strParams = stringify(params);
202
276
  if (notNilEmpty(strParams)) {
203
277
  if (url.endsWith('?')) {
@@ -215,16 +289,4 @@ export class Net {
215
289
  }
216
290
  return url;
217
291
  }
218
- /**
219
- * 处理 url 参数
220
- *
221
- * @author chitanda
222
- * @date 2022-07-20 20:07:17
223
- * @private
224
- * @param {string} url
225
- * @return {*} {string}
226
- */
227
- attachUrl(url) {
228
- return `${this.prefix}${url}`;
229
- }
230
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ibiz-template/core",
3
- "version": "0.0.1-alpha.33",
3
+ "version": "0.0.1-alpha.37",
4
4
  "description": "核心包",
5
5
  "type": "module",
6
6
  "main": "out/index.js",
@@ -30,6 +30,7 @@
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
32
  "axios": "^1.1.3",
33
+ "loglevel": "^1.8.0",
33
34
  "pluralize": "^8.0.0",
34
35
  "qs": "^6.11.0"
35
36
  },
@@ -37,11 +38,11 @@
37
38
  "@types/pluralize": "^0.0.29",
38
39
  "@types/qs": "^6.9.7",
39
40
  "lodash-es": "^4.17.21",
40
- "qx-util": "0.4.3"
41
+ "qx-util": "^0.4.4"
41
42
  },
42
43
  "peerDependencies": {
43
44
  "lodash-es": "^4.17.21",
44
- "qx-util": "^0.4.1"
45
+ "qx-util": "^0.4.4"
45
46
  },
46
- "gitHead": "4fa409c39d3cecd7605818a865abf2b91f2a9e72"
47
+ "gitHead": "5dc73da226eb8c5863add2ee60a25d0251a26ed3"
47
48
  }
@@ -6,8 +6,10 @@ import { IEnvironment } from '../interface';
6
6
  */
7
7
  export const Environment: IEnvironment = {
8
8
  dev: false,
9
+ logLevel: 'ERROR',
9
10
  jsCdn: 'https://cdn.jsdelivr.net',
10
11
  baseUrl: '',
12
+ pluginBaseUrl: './plugins',
11
13
  remoteModelUrl: '/remotemodel',
12
14
  assetsUrl: './assets',
13
15
  dcSystem: '',
package/src/ibizsys.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Environment } from './environment/environment';
2
2
  import { OrgData } from './interface';
3
- import { Net } from './utils';
3
+ import { Logger, Net } from './utils';
4
4
 
5
5
  /**
6
6
  * 全局对象
@@ -19,6 +19,14 @@ export class IBizSys {
19
19
  */
20
20
  env = Environment;
21
21
 
22
+ /**
23
+ * 日志对象
24
+ *
25
+ * @author chitanda
26
+ * @date 2022-10-25 20:10:22
27
+ */
28
+ log = Logger;
29
+
22
30
  /**
23
31
  * 网络请求工具类
24
32
  *
package/src/install.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { FactoryCenter } from './factory-center';
2
2
  import { IBizSys } from './ibizsys';
3
- import { Interceptor } from './utils';
3
+ import { CoreInterceptor } from './utils';
4
4
 
5
5
  /**
6
6
  * 初始化全局对象
@@ -15,5 +15,5 @@ export function install(): void {
15
15
  }
16
16
  window.ibiz = new IBizSys();
17
17
  window.___fc = new FactoryCenter();
18
- new Interceptor();
18
+ ibiz.net.addInterceptor('CoreInterceptor', new CoreInterceptor());
19
19
  }
@@ -1,3 +1,4 @@
1
+ import { LogLevelDesc } from 'loglevel';
1
2
  import { LoginMode, MenuPermissionMode } from '../../constant';
2
3
 
3
4
  /**
@@ -28,6 +29,15 @@ export interface IEnvironment {
28
29
  */
29
30
  dev: boolean;
30
31
 
32
+ /**
33
+ * 日志级别
34
+ *
35
+ * @author chitanda
36
+ * @date 2022-10-25 20:10:55
37
+ * @type {LogLevelDesc}
38
+ */
39
+ logLevel: LogLevelDesc;
40
+
31
41
  /**
32
42
  * js 静态文件 cdn 地址
33
43
  *
@@ -47,6 +57,15 @@ export interface IEnvironment {
47
57
  */
48
58
  baseUrl: string;
49
59
 
60
+ /**
61
+ * 插件默认所在目录
62
+ *
63
+ * @author chitanda
64
+ * @date 2022-10-31 14:10:49
65
+ * @type {string}
66
+ */
67
+ pluginBaseUrl: string;
68
+
50
69
  /**
51
70
  * 登录模式
52
71
  *
@@ -1,4 +1,5 @@
1
- export { Interceptor } from './interceptor/interceptor';
1
+ export * from './interceptor';
2
+ export { Logger } from './logger/logger';
2
3
  export { Namespace } from './namespace/namespace';
3
4
  export { IHttpResponse, HttpResponse } from './net/http-response';
4
5
  export { Net } from './net/net';
@@ -0,0 +1,35 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { getToken } from '../util/util';
3
+ import { Interceptor } from './interceptor';
4
+
5
+ /**
6
+ * 核心包拦截器
7
+ *
8
+ * @author lxm
9
+ * @date 2022-10-27 17:10:48
10
+ * @export
11
+ * @class CoreInterceptor
12
+ * @extends {Interceptor}
13
+ */
14
+ export class CoreInterceptor extends Interceptor {
15
+ protected async onBeforeRequest(
16
+ config: AxiosRequestConfig,
17
+ ): Promise<AxiosRequestConfig> {
18
+ await super.onBeforeRequest(config);
19
+
20
+ if (!config.headers) {
21
+ config.headers = {};
22
+ }
23
+ config.headers.Authorization = `Bearer ${getToken()}`;
24
+ const { orgData } = ibiz;
25
+ if (orgData) {
26
+ if (orgData.systemid) {
27
+ config.headers.srfsystemid = orgData.systemid;
28
+ }
29
+ if (orgData.orgid) {
30
+ config.headers.srforgid = orgData.orgid;
31
+ }
32
+ }
33
+ return config;
34
+ }
35
+ }
@@ -0,0 +1,2 @@
1
+ export * from './core-interceptor';
2
+ export * from './interceptor';
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
2
- import { getToken } from '../util/util';
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
3
3
 
4
4
  /**
5
- * 默认请求拦截器
5
+ * 拦截器基类
6
6
  *
7
7
  * @author chitanda
8
8
  * @date 2022-07-20 18:07:11
@@ -10,26 +10,115 @@ import { getToken } from '../util/util';
10
10
  * @class Interceptor
11
11
  */
12
12
  export class Interceptor {
13
- constructor() {
14
- this.init();
13
+ /**
14
+ * 请求拦截器绑定标识
15
+ *
16
+ * @author lxm
17
+ * @date 2022-10-27 17:10:20
18
+ * @private
19
+ * @type {number}
20
+ */
21
+ private requestTag?: number;
22
+
23
+ /**
24
+ * 响应拦截器绑定标识
25
+ *
26
+ * @author lxm
27
+ * @date 2022-10-27 17:10:19
28
+ * @private
29
+ * @type {number}
30
+ */
31
+ private responseTag?: number;
32
+
33
+ /**
34
+ * 请求之前处理
35
+ *
36
+ * @author lxm
37
+ * @date 2022-10-27 17:10:41
38
+ * @protected
39
+ * @param {AxiosRequestConfig} config
40
+ * @returns {*} {Promise<AxiosRequestConfig>}
41
+ */
42
+ protected async onBeforeRequest(
43
+ config: AxiosRequestConfig,
44
+ ): Promise<AxiosRequestConfig> {
45
+ return config;
46
+ }
47
+
48
+ /**
49
+ * 请求失败之后处理
50
+ *
51
+ * @author lxm
52
+ * @date 2022-10-27 17:10:40
53
+ * @protected
54
+ * @param {*} error
55
+ */
56
+ protected onRequestError(error: any) {
57
+ // 处理请求错误
58
+ console.log(error);
59
+ return Promise.reject(error);
60
+ }
61
+
62
+ /**
63
+ * 响应成功之后处理
64
+ *
65
+ * @author lxm
66
+ * @date 2022-10-27 17:10:38
67
+ * @protected
68
+ * @param {AxiosResponse} config
69
+ * @returns {*} {Promise<AxiosRequestConfig>}
70
+ */
71
+ protected async onResponseSuccess(
72
+ config: AxiosResponse,
73
+ ): Promise<AxiosResponse> {
74
+ return config;
75
+ }
76
+
77
+ /**
78
+ * 响应失败之后处理
79
+ *
80
+ * @author lxm
81
+ * @date 2022-10-27 17:10:37
82
+ * @protected
83
+ * @param {*} _error
84
+ */
85
+ protected onResponseError(error: any) {
86
+ // 处理响应错误
87
+ console.log(error);
88
+ return Promise.reject(error);
89
+ }
90
+
91
+ /**
92
+ * 使用拦截器
93
+ *
94
+ * @author lxm
95
+ * @date 2022-10-27 17:10:28
96
+ * @param {AxiosInstance} instance
97
+ */
98
+ use(instance: AxiosInstance): void {
99
+ this.requestTag = instance.interceptors.request.use(
100
+ this.onBeforeRequest,
101
+ this.onRequestError,
102
+ );
103
+ this.responseTag = instance.interceptors.response.use(
104
+ this.onResponseSuccess,
105
+ this.onResponseError,
106
+ );
15
107
  }
16
108
 
17
- protected init(): void {
18
- axios.interceptors.request.use(config => {
19
- if (!config.headers) {
20
- config.headers = {};
21
- }
22
- config.headers.Authorization = `Bearer ${getToken()}`;
23
- const { orgData } = ibiz;
24
- if (orgData) {
25
- if (orgData.systemid) {
26
- config.headers.srfsystemid = orgData.systemid;
27
- }
28
- if (orgData.orgid) {
29
- config.headers.srforgid = orgData.orgid;
30
- }
31
- }
32
- return config;
33
- });
109
+ /**
110
+ * 移出拦截器
111
+ *
112
+ * @author lxm
113
+ * @date 2022-10-27 17:10:27
114
+ * @param {AxiosInstance} instance
115
+ */
116
+ eject(instance: AxiosInstance) {
117
+ if (this.requestTag) {
118
+ instance.interceptors.request.eject(this.requestTag);
119
+ }
120
+ if (this.responseTag) {
121
+ instance.interceptors.response.eject(this.responseTag);
122
+ }
34
123
  }
35
124
  }
@@ -0,0 +1,3 @@
1
+ import * as Logger from 'loglevel';
2
+
3
+ export { Logger };