@hzab/data-model 2.0.0-alpha.4 → 2.0.1-alpha.0
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/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/src/ArrayUtils.ts +712 -712
- package/src/array-data-model.ts +480 -480
- package/src/axios.ts +210 -210
- package/src/data-model.ts +850 -850
- package/src/hooks.ts +38 -38
- package/src/index.ts +11 -11
- package/src/public-utils.ts +26 -26
- package/src/utils.ts +98 -98
package/src/axios.ts
CHANGED
|
@@ -1,210 +1,210 @@
|
|
|
1
|
-
import axiosDef, { AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
2
|
-
import Cookies from "js-cookie";
|
|
3
|
-
import _ from "lodash";
|
|
4
|
-
|
|
5
|
-
export const TOKEN = "token";
|
|
6
|
-
|
|
7
|
-
const axios = axiosDef.create();
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* setToken 选项接口
|
|
11
|
-
*/
|
|
12
|
-
export interface SetTokenOptions {
|
|
13
|
-
/** 是否同时设置 cookie */
|
|
14
|
-
hasCookie?: boolean;
|
|
15
|
-
/** cookie 属性配置 */
|
|
16
|
-
cookieAttrs?: Cookies.CookieAttributes;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* getToken 选项接口
|
|
21
|
-
*/
|
|
22
|
-
export interface GetTokenOptions {
|
|
23
|
-
/** 是否从 cookie 获取 */
|
|
24
|
-
hasCookie?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 处理错误回调函数的选项接口
|
|
29
|
-
*/
|
|
30
|
-
export interface HandleErrCbOptions {
|
|
31
|
-
/** 直接替换回调函数 */
|
|
32
|
-
replaceErrCb?: boolean;
|
|
33
|
-
/** 日志前缀 */
|
|
34
|
-
logPrefix?: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 设置 token
|
|
39
|
-
* @param token token 值
|
|
40
|
-
* @param opt 选项
|
|
41
|
-
*/
|
|
42
|
-
export function setToken(token = "", opt?: SetTokenOptions) {
|
|
43
|
-
const { hasCookie = false } = opt || {};
|
|
44
|
-
const _t = token || "";
|
|
45
|
-
if (hasCookie) {
|
|
46
|
-
const { cookieAttrs } = opt || {};
|
|
47
|
-
const cookieOpt = _.merge(
|
|
48
|
-
{
|
|
49
|
-
expires: 365,
|
|
50
|
-
},
|
|
51
|
-
cookieAttrs,
|
|
52
|
-
);
|
|
53
|
-
Cookies.set(TOKEN, _t, cookieOpt);
|
|
54
|
-
}
|
|
55
|
-
localStorage.setItem(TOKEN, _t);
|
|
56
|
-
setAxAuthorization(_t);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 获取 token
|
|
61
|
-
* @param opt 选项
|
|
62
|
-
* @returns token 值
|
|
63
|
-
*/
|
|
64
|
-
export function getToken(opt?: GetTokenOptions): string {
|
|
65
|
-
const { hasCookie = false } = opt || {};
|
|
66
|
-
let token = "";
|
|
67
|
-
if (hasCookie) {
|
|
68
|
-
token = Cookies.get(TOKEN) || "";
|
|
69
|
-
if (token) {
|
|
70
|
-
return token;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
token = localStorage.getItem(TOKEN);
|
|
74
|
-
return token;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export const temp = { axiosList: [axios, axiosDef] };
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 设置 axios 列表,用于批量设置 axios 相关配置
|
|
81
|
-
* @param axiosList axios 实例列表
|
|
82
|
-
* @returns 更新后的 axios 实例列表
|
|
83
|
-
*/
|
|
84
|
-
export function setAxios(axiosList: (typeof axios | typeof axiosDef)[]) {
|
|
85
|
-
temp.axiosList = [...temp.axiosList, ...axiosList];
|
|
86
|
-
return temp.axiosList;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* axios 请求拦截器
|
|
91
|
-
* @param cb 成功回调
|
|
92
|
-
* @param errCb 错误回调
|
|
93
|
-
* @param opt 选项
|
|
94
|
-
*/
|
|
95
|
-
export function setAxRequest(
|
|
96
|
-
cb: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>,
|
|
97
|
-
errCb?: (error: any) => Promise<any>,
|
|
98
|
-
opt?: HandleErrCbOptions,
|
|
99
|
-
) {
|
|
100
|
-
temp.axiosList?.forEach((_ax) => {
|
|
101
|
-
_ax.interceptors.request.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 响应拦截器
|
|
107
|
-
* @param cb 成功回调
|
|
108
|
-
* @param errCb 错误回调
|
|
109
|
-
* @param opt 选项
|
|
110
|
-
*/
|
|
111
|
-
export function setAxResponse(
|
|
112
|
-
cb: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>,
|
|
113
|
-
errCb?: (error: any) => Promise<any>,
|
|
114
|
-
opt?: HandleErrCbOptions,
|
|
115
|
-
) {
|
|
116
|
-
temp.axiosList?.forEach((_ax) => {
|
|
117
|
-
_ax.interceptors.response.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 处理错误回调函数
|
|
123
|
-
* @param errCb 错误回调
|
|
124
|
-
* @param opt 选项
|
|
125
|
-
* @returns 处理后的错误回调
|
|
126
|
-
*/
|
|
127
|
-
export function handleErrCb(errCb?: (error: any) => Promise<any>, opt?: HandleErrCbOptions) {
|
|
128
|
-
const { replaceErrCb, logPrefix } = opt || {};
|
|
129
|
-
const isFn = typeof errCb === "function";
|
|
130
|
-
|
|
131
|
-
if (replaceErrCb && isFn) {
|
|
132
|
-
return errCb;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return (error: any) => {
|
|
136
|
-
if (axiosDef.isCancel(error)) {
|
|
137
|
-
console.info("axios 请求已取消:", error);
|
|
138
|
-
return Promise.reject(error);
|
|
139
|
-
}
|
|
140
|
-
console.error(logPrefix, error);
|
|
141
|
-
return isFn ? errCb(error) : Promise.reject(error);
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 设置 axios.defaults
|
|
147
|
-
* @param key 配置键
|
|
148
|
-
* @param val 配置值
|
|
149
|
-
*/
|
|
150
|
-
export function setAxDefaults(key: string, val: any) {
|
|
151
|
-
temp.axiosList?.forEach((_ax) => {
|
|
152
|
-
(_ax.defaults as any)[key] = val;
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 设置 headers
|
|
158
|
-
* @param key header 键
|
|
159
|
-
* @param val header 值
|
|
160
|
-
*/
|
|
161
|
-
export function setAxHeaders(key: string, val: any) {
|
|
162
|
-
temp.axiosList?.forEach((_ax) => {
|
|
163
|
-
(_ax.defaults.headers as any)[key] = val;
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* 设置 timeout
|
|
169
|
-
* @param timeout 超时时间
|
|
170
|
-
*/
|
|
171
|
-
export function setAxTimeout(timeout = 0) {
|
|
172
|
-
setAxDefaults("timeout", timeout);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 设置 Authorization token
|
|
177
|
-
* @param token token 值
|
|
178
|
-
*/
|
|
179
|
-
export function setAxAuthorization(token: string) {
|
|
180
|
-
setAxHeaders("Authorization", token);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 设置 baseUrl
|
|
185
|
-
* @param url 基础 URL
|
|
186
|
-
*/
|
|
187
|
-
export function setAxBaseUrl(url: string) {
|
|
188
|
-
setAxDefaults("baseURL", url);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* 获取 axios.CancelToken.source,用于取消请求
|
|
193
|
-
* @returns CancelToken source
|
|
194
|
-
*/
|
|
195
|
-
export function getCancelTokenSource() {
|
|
196
|
-
return axiosDef.CancelToken.source();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 检查是否是 cancel 的结果
|
|
201
|
-
* @param res
|
|
202
|
-
* @returns
|
|
203
|
-
*/
|
|
204
|
-
export function isCancel(res) {
|
|
205
|
-
return axiosDef.isCancel(res);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export { axios, axiosDef };
|
|
209
|
-
|
|
210
|
-
export default axios;
|
|
1
|
+
import axiosDef, { AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
2
|
+
import Cookies from "js-cookie";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
|
|
5
|
+
export const TOKEN = "token";
|
|
6
|
+
|
|
7
|
+
const axios = axiosDef.create();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* setToken 选项接口
|
|
11
|
+
*/
|
|
12
|
+
export interface SetTokenOptions {
|
|
13
|
+
/** 是否同时设置 cookie */
|
|
14
|
+
hasCookie?: boolean;
|
|
15
|
+
/** cookie 属性配置 */
|
|
16
|
+
cookieAttrs?: Cookies.CookieAttributes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* getToken 选项接口
|
|
21
|
+
*/
|
|
22
|
+
export interface GetTokenOptions {
|
|
23
|
+
/** 是否从 cookie 获取 */
|
|
24
|
+
hasCookie?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 处理错误回调函数的选项接口
|
|
29
|
+
*/
|
|
30
|
+
export interface HandleErrCbOptions {
|
|
31
|
+
/** 直接替换回调函数 */
|
|
32
|
+
replaceErrCb?: boolean;
|
|
33
|
+
/** 日志前缀 */
|
|
34
|
+
logPrefix?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 设置 token
|
|
39
|
+
* @param token token 值
|
|
40
|
+
* @param opt 选项
|
|
41
|
+
*/
|
|
42
|
+
export function setToken(token = "", opt?: SetTokenOptions) {
|
|
43
|
+
const { hasCookie = false } = opt || {};
|
|
44
|
+
const _t = token || "";
|
|
45
|
+
if (hasCookie) {
|
|
46
|
+
const { cookieAttrs } = opt || {};
|
|
47
|
+
const cookieOpt = _.merge(
|
|
48
|
+
{
|
|
49
|
+
expires: 365,
|
|
50
|
+
},
|
|
51
|
+
cookieAttrs,
|
|
52
|
+
);
|
|
53
|
+
Cookies.set(TOKEN, _t, cookieOpt);
|
|
54
|
+
}
|
|
55
|
+
localStorage.setItem(TOKEN, _t);
|
|
56
|
+
setAxAuthorization(_t);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 获取 token
|
|
61
|
+
* @param opt 选项
|
|
62
|
+
* @returns token 值
|
|
63
|
+
*/
|
|
64
|
+
export function getToken(opt?: GetTokenOptions): string {
|
|
65
|
+
const { hasCookie = false } = opt || {};
|
|
66
|
+
let token = "";
|
|
67
|
+
if (hasCookie) {
|
|
68
|
+
token = Cookies.get(TOKEN) || "";
|
|
69
|
+
if (token) {
|
|
70
|
+
return token;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
token = localStorage.getItem(TOKEN);
|
|
74
|
+
return token;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const temp = { axiosList: [axios, axiosDef] };
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 设置 axios 列表,用于批量设置 axios 相关配置
|
|
81
|
+
* @param axiosList axios 实例列表
|
|
82
|
+
* @returns 更新后的 axios 实例列表
|
|
83
|
+
*/
|
|
84
|
+
export function setAxios(axiosList: (typeof axios | typeof axiosDef)[]) {
|
|
85
|
+
temp.axiosList = [...temp.axiosList, ...axiosList];
|
|
86
|
+
return temp.axiosList;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* axios 请求拦截器
|
|
91
|
+
* @param cb 成功回调
|
|
92
|
+
* @param errCb 错误回调
|
|
93
|
+
* @param opt 选项
|
|
94
|
+
*/
|
|
95
|
+
export function setAxRequest(
|
|
96
|
+
cb: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>,
|
|
97
|
+
errCb?: (error: any) => Promise<any>,
|
|
98
|
+
opt?: HandleErrCbOptions,
|
|
99
|
+
) {
|
|
100
|
+
temp.axiosList?.forEach((_ax) => {
|
|
101
|
+
_ax.interceptors.request.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 响应拦截器
|
|
107
|
+
* @param cb 成功回调
|
|
108
|
+
* @param errCb 错误回调
|
|
109
|
+
* @param opt 选项
|
|
110
|
+
*/
|
|
111
|
+
export function setAxResponse(
|
|
112
|
+
cb: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>,
|
|
113
|
+
errCb?: (error: any) => Promise<any>,
|
|
114
|
+
opt?: HandleErrCbOptions,
|
|
115
|
+
) {
|
|
116
|
+
temp.axiosList?.forEach((_ax) => {
|
|
117
|
+
_ax.interceptors.response.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 处理错误回调函数
|
|
123
|
+
* @param errCb 错误回调
|
|
124
|
+
* @param opt 选项
|
|
125
|
+
* @returns 处理后的错误回调
|
|
126
|
+
*/
|
|
127
|
+
export function handleErrCb(errCb?: (error: any) => Promise<any>, opt?: HandleErrCbOptions) {
|
|
128
|
+
const { replaceErrCb, logPrefix } = opt || {};
|
|
129
|
+
const isFn = typeof errCb === "function";
|
|
130
|
+
|
|
131
|
+
if (replaceErrCb && isFn) {
|
|
132
|
+
return errCb;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return (error: any) => {
|
|
136
|
+
if (axiosDef.isCancel(error)) {
|
|
137
|
+
console.info("axios 请求已取消:", error);
|
|
138
|
+
return Promise.reject(error);
|
|
139
|
+
}
|
|
140
|
+
console.error(logPrefix, error);
|
|
141
|
+
return isFn ? errCb(error) : Promise.reject(error);
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 设置 axios.defaults
|
|
147
|
+
* @param key 配置键
|
|
148
|
+
* @param val 配置值
|
|
149
|
+
*/
|
|
150
|
+
export function setAxDefaults(key: string, val: any) {
|
|
151
|
+
temp.axiosList?.forEach((_ax) => {
|
|
152
|
+
(_ax.defaults as any)[key] = val;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 设置 headers
|
|
158
|
+
* @param key header 键
|
|
159
|
+
* @param val header 值
|
|
160
|
+
*/
|
|
161
|
+
export function setAxHeaders(key: string, val: any) {
|
|
162
|
+
temp.axiosList?.forEach((_ax) => {
|
|
163
|
+
(_ax.defaults.headers as any)[key] = val;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 设置 timeout
|
|
169
|
+
* @param timeout 超时时间
|
|
170
|
+
*/
|
|
171
|
+
export function setAxTimeout(timeout = 0) {
|
|
172
|
+
setAxDefaults("timeout", timeout);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 设置 Authorization token
|
|
177
|
+
* @param token token 值
|
|
178
|
+
*/
|
|
179
|
+
export function setAxAuthorization(token: string) {
|
|
180
|
+
setAxHeaders("Authorization", token);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 设置 baseUrl
|
|
185
|
+
* @param url 基础 URL
|
|
186
|
+
*/
|
|
187
|
+
export function setAxBaseUrl(url: string) {
|
|
188
|
+
setAxDefaults("baseURL", url);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 获取 axios.CancelToken.source,用于取消请求
|
|
193
|
+
* @returns CancelToken source
|
|
194
|
+
*/
|
|
195
|
+
export function getCancelTokenSource() {
|
|
196
|
+
return axiosDef.CancelToken.source();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 检查是否是 cancel 的结果
|
|
201
|
+
* @param res
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
export function isCancel(res) {
|
|
205
|
+
return axiosDef.isCancel(res);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { axios, axiosDef };
|
|
209
|
+
|
|
210
|
+
export default axios;
|