@hzab/data-model 2.0.3-alpha.0 → 2.0.3-alpha.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/CHANGELOG.md +6 -1
- package/package.json +2 -3
- package/src/ArrayUtils.ts +712 -712
- package/src/RequestCache.ts +202 -202
- package/src/array-data-model.ts +1 -1
- package/src/axios.ts +2 -2
- package/src/data-model.ts +16 -16
- package/src/hooks.ts +38 -38
package/src/RequestCache.ts
CHANGED
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
import { InternalAxiosRequestConfig } from "axios";
|
|
2
|
-
import { axiosDef } from "./axios";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 缓存配置
|
|
6
|
-
*/
|
|
7
|
-
export interface CacheOpt {
|
|
8
|
-
/** 缓存时间 毫秒 */
|
|
9
|
-
cacheTTL?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 缓存方法入参配置
|
|
14
|
-
*/
|
|
15
|
-
export interface RequestCacheParams {
|
|
16
|
-
options?: CacheOpt;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 接口入参(用于生成缓存 key)
|
|
21
|
-
*/
|
|
22
|
-
export interface Req {
|
|
23
|
-
method?: string;
|
|
24
|
-
baseURL?: string;
|
|
25
|
-
url?: string;
|
|
26
|
-
params?: unknown;
|
|
27
|
-
data?: unknown;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 缓存对象
|
|
32
|
-
*/
|
|
33
|
-
export interface CacheItem {
|
|
34
|
-
key: string;
|
|
35
|
-
/** 过期时间戳 */
|
|
36
|
-
expires?: number;
|
|
37
|
-
/** 缓存的 promise */
|
|
38
|
-
promise?: Promise<unknown>;
|
|
39
|
-
/** 缓存的 axios config 配置 */
|
|
40
|
-
config?: InternalAxiosRequestConfig;
|
|
41
|
-
resolve: (d) => void;
|
|
42
|
-
reject: (err) => void;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 接口缓存类
|
|
47
|
-
*/
|
|
48
|
-
export class RequestCache {
|
|
49
|
-
options: CacheOpt;
|
|
50
|
-
cacheTTL = 3000;
|
|
51
|
-
/** 存储为 CacheItem(类型为 unknown,返回时断言) */
|
|
52
|
-
_cacheMap: Map<string, CacheItem> = new Map();
|
|
53
|
-
|
|
54
|
-
constructor(params: RequestCacheParams) {
|
|
55
|
-
const { options = {} } = params || {};
|
|
56
|
-
this.cacheTTL = options?.cacheTTL ?? 3000;
|
|
57
|
-
this.options = options;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 处理 axios request 拦截逻辑
|
|
62
|
-
* @param config
|
|
63
|
-
* @returns
|
|
64
|
-
*/
|
|
65
|
-
handleAxRequest(config) {
|
|
66
|
-
if (this.hasCache(config)) {
|
|
67
|
-
const cache = this.getCache(config);
|
|
68
|
-
// 存在缓存,
|
|
69
|
-
return Promise.reject({
|
|
70
|
-
code: 200,
|
|
71
|
-
key: this.getCacheKey(config),
|
|
72
|
-
config,
|
|
73
|
-
from: "cache",
|
|
74
|
-
cache,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
this.rmCache(config);
|
|
78
|
-
|
|
79
|
-
const cacheData = {
|
|
80
|
-
code: 200,
|
|
81
|
-
key: this.getCacheKey(config),
|
|
82
|
-
config,
|
|
83
|
-
from: "cache",
|
|
84
|
-
promise: undefined,
|
|
85
|
-
resolve: undefined,
|
|
86
|
-
reject: undefined,
|
|
87
|
-
};
|
|
88
|
-
// 设置 promise 保持请求挂起状态
|
|
89
|
-
cacheData.promise = new Promise((resolve, reject) => {
|
|
90
|
-
cacheData.resolve = resolve;
|
|
91
|
-
cacheData.reject = reject;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
config.cache = this.addCache(cacheData);
|
|
95
|
-
return config;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 判断是否存在缓存
|
|
100
|
-
* @param axConf
|
|
101
|
-
* @returns
|
|
102
|
-
*/
|
|
103
|
-
hasCache(axConf) {
|
|
104
|
-
const key = this.getCacheKey(axConf);
|
|
105
|
-
const cache = this._cacheMap.get(key);
|
|
106
|
-
if (cache && cache.expires >= Date.now()) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
// 过期清除
|
|
110
|
-
this._cacheMap.delete(key);
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 获取缓存
|
|
116
|
-
* @param axios config
|
|
117
|
-
* @param opt 自定义缓存配置
|
|
118
|
-
* @returns Promise<T>
|
|
119
|
-
*/
|
|
120
|
-
getCache(axConf, opt?: CacheOpt): CacheItem {
|
|
121
|
-
const key = this.getCacheKey(axConf);
|
|
122
|
-
|
|
123
|
-
return this.getCacheByKey(key, opt);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 获取缓存
|
|
128
|
-
* @param key string
|
|
129
|
-
* @param opt 自定义缓存配置
|
|
130
|
-
* @returns Promise<T>
|
|
131
|
-
*/
|
|
132
|
-
getCacheByKey(key, opt?: CacheOpt) {
|
|
133
|
-
// TODO: 过期处理? 结果和请求分开。只有请求才进行过期判断?
|
|
134
|
-
// 存在未过期的缓存
|
|
135
|
-
const cached = this._cacheMap.get(key);
|
|
136
|
-
if (cached && cached.expires > Date.now()) {
|
|
137
|
-
return cached;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 过期删除
|
|
141
|
-
if (cached) {
|
|
142
|
-
this._cacheMap.delete(key);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 添加缓存
|
|
148
|
-
* @param promise 要缓存的 Promise
|
|
149
|
-
* @param req 请求描述对象
|
|
150
|
-
* @param config 缓存配置
|
|
151
|
-
*/
|
|
152
|
-
addCache(data: CacheItem, opt: CacheOpt = this.options): CacheItem {
|
|
153
|
-
const { config } = data || {};
|
|
154
|
-
const key = this.getCacheKey(config);
|
|
155
|
-
if (this._cacheMap.has(key)) {
|
|
156
|
-
return this._cacheMap.get(key);
|
|
157
|
-
}
|
|
158
|
-
const ttl = opt?.cacheTTL ?? this.cacheTTL;
|
|
159
|
-
const cache = {
|
|
160
|
-
...data,
|
|
161
|
-
key,
|
|
162
|
-
expires: Date.now() + ttl,
|
|
163
|
-
config: config,
|
|
164
|
-
promise: data.promise,
|
|
165
|
-
};
|
|
166
|
-
this._cacheMap.set(key, cache);
|
|
167
|
-
return cache;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
rmCache(config: Req) {
|
|
171
|
-
this._cacheMap.delete(this.getCacheKey(config));
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* 获取缓存的 key
|
|
176
|
-
* @param config 请求入参
|
|
177
|
-
* @returns 缓存键字符串
|
|
178
|
-
*/
|
|
179
|
-
getCacheKey(config: Req): string {
|
|
180
|
-
const { baseURL, method, url, params, data } = config;
|
|
181
|
-
// 注意:params 和 data 需要序列化,并保证对象属性顺序稳定性
|
|
182
|
-
return `${method}:${baseURL}/${url}:${this.stableStringify(params)}:${this.stableStringify(data)}`;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* 序列化对象,保证属性顺序一致
|
|
187
|
-
* @param obj 任意值
|
|
188
|
-
* @returns JSON 字符串
|
|
189
|
-
*/
|
|
190
|
-
private stableStringify(obj: unknown): string {
|
|
191
|
-
if (!obj || typeof obj !== "object") return JSON.stringify(obj);
|
|
192
|
-
const sorted = Object.keys(obj)
|
|
193
|
-
.sort()
|
|
194
|
-
.reduce((acc, key) => {
|
|
195
|
-
acc[key] = (obj as Record<string, unknown>)[key];
|
|
196
|
-
return acc;
|
|
197
|
-
}, {} as Record<string, unknown>);
|
|
198
|
-
return JSON.stringify(sorted);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export default RequestCache;
|
|
1
|
+
import { InternalAxiosRequestConfig } from "axios";
|
|
2
|
+
import { axiosDef } from "./axios";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 缓存配置
|
|
6
|
+
*/
|
|
7
|
+
export interface CacheOpt {
|
|
8
|
+
/** 缓存时间 毫秒 */
|
|
9
|
+
cacheTTL?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 缓存方法入参配置
|
|
14
|
+
*/
|
|
15
|
+
export interface RequestCacheParams {
|
|
16
|
+
options?: CacheOpt;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 接口入参(用于生成缓存 key)
|
|
21
|
+
*/
|
|
22
|
+
export interface Req {
|
|
23
|
+
method?: string;
|
|
24
|
+
baseURL?: string;
|
|
25
|
+
url?: string;
|
|
26
|
+
params?: unknown;
|
|
27
|
+
data?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 缓存对象
|
|
32
|
+
*/
|
|
33
|
+
export interface CacheItem {
|
|
34
|
+
key: string;
|
|
35
|
+
/** 过期时间戳 */
|
|
36
|
+
expires?: number;
|
|
37
|
+
/** 缓存的 promise */
|
|
38
|
+
promise?: Promise<unknown>;
|
|
39
|
+
/** 缓存的 axios config 配置 */
|
|
40
|
+
config?: InternalAxiosRequestConfig;
|
|
41
|
+
resolve: (d) => void;
|
|
42
|
+
reject: (err) => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 接口缓存类
|
|
47
|
+
*/
|
|
48
|
+
export class RequestCache {
|
|
49
|
+
options: CacheOpt;
|
|
50
|
+
cacheTTL = 3000;
|
|
51
|
+
/** 存储为 CacheItem(类型为 unknown,返回时断言) */
|
|
52
|
+
_cacheMap: Map<string, CacheItem> = new Map();
|
|
53
|
+
|
|
54
|
+
constructor(params: RequestCacheParams) {
|
|
55
|
+
const { options = {} } = params || {};
|
|
56
|
+
this.cacheTTL = options?.cacheTTL ?? 3000;
|
|
57
|
+
this.options = options;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 处理 axios request 拦截逻辑
|
|
62
|
+
* @param config
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
handleAxRequest(config) {
|
|
66
|
+
if (this.hasCache(config)) {
|
|
67
|
+
const cache = this.getCache(config);
|
|
68
|
+
// 存在缓存,
|
|
69
|
+
return Promise.reject({
|
|
70
|
+
code: 200,
|
|
71
|
+
key: this.getCacheKey(config),
|
|
72
|
+
config,
|
|
73
|
+
from: "cache",
|
|
74
|
+
cache,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
this.rmCache(config);
|
|
78
|
+
|
|
79
|
+
const cacheData = {
|
|
80
|
+
code: 200,
|
|
81
|
+
key: this.getCacheKey(config),
|
|
82
|
+
config,
|
|
83
|
+
from: "cache",
|
|
84
|
+
promise: undefined,
|
|
85
|
+
resolve: undefined,
|
|
86
|
+
reject: undefined,
|
|
87
|
+
};
|
|
88
|
+
// 设置 promise 保持请求挂起状态
|
|
89
|
+
cacheData.promise = new Promise((resolve, reject) => {
|
|
90
|
+
cacheData.resolve = resolve;
|
|
91
|
+
cacheData.reject = reject;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
config.cache = this.addCache(cacheData);
|
|
95
|
+
return config;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 判断是否存在缓存
|
|
100
|
+
* @param axConf
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
hasCache(axConf) {
|
|
104
|
+
const key = this.getCacheKey(axConf);
|
|
105
|
+
const cache = this._cacheMap.get(key);
|
|
106
|
+
if (cache && cache.expires >= Date.now()) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
// 过期清除
|
|
110
|
+
this._cacheMap.delete(key);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 获取缓存
|
|
116
|
+
* @param axios config
|
|
117
|
+
* @param opt 自定义缓存配置
|
|
118
|
+
* @returns Promise<T>
|
|
119
|
+
*/
|
|
120
|
+
getCache(axConf, opt?: CacheOpt): CacheItem {
|
|
121
|
+
const key = this.getCacheKey(axConf);
|
|
122
|
+
|
|
123
|
+
return this.getCacheByKey(key, opt);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 获取缓存
|
|
128
|
+
* @param key string
|
|
129
|
+
* @param opt 自定义缓存配置
|
|
130
|
+
* @returns Promise<T>
|
|
131
|
+
*/
|
|
132
|
+
getCacheByKey(key, opt?: CacheOpt) {
|
|
133
|
+
// TODO: 过期处理? 结果和请求分开。只有请求才进行过期判断?
|
|
134
|
+
// 存在未过期的缓存
|
|
135
|
+
const cached = this._cacheMap.get(key);
|
|
136
|
+
if (cached && cached.expires > Date.now()) {
|
|
137
|
+
return cached;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 过期删除
|
|
141
|
+
if (cached) {
|
|
142
|
+
this._cacheMap.delete(key);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 添加缓存
|
|
148
|
+
* @param promise 要缓存的 Promise
|
|
149
|
+
* @param req 请求描述对象
|
|
150
|
+
* @param config 缓存配置
|
|
151
|
+
*/
|
|
152
|
+
addCache(data: CacheItem, opt: CacheOpt = this.options): CacheItem {
|
|
153
|
+
const { config } = data || {};
|
|
154
|
+
const key = this.getCacheKey(config);
|
|
155
|
+
if (this._cacheMap.has(key)) {
|
|
156
|
+
return this._cacheMap.get(key);
|
|
157
|
+
}
|
|
158
|
+
const ttl = opt?.cacheTTL ?? this.cacheTTL;
|
|
159
|
+
const cache = {
|
|
160
|
+
...data,
|
|
161
|
+
key,
|
|
162
|
+
expires: Date.now() + ttl,
|
|
163
|
+
config: config,
|
|
164
|
+
promise: data.promise,
|
|
165
|
+
};
|
|
166
|
+
this._cacheMap.set(key, cache);
|
|
167
|
+
return cache;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
rmCache(config: Req) {
|
|
171
|
+
this._cacheMap.delete(this.getCacheKey(config));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 获取缓存的 key
|
|
176
|
+
* @param config 请求入参
|
|
177
|
+
* @returns 缓存键字符串
|
|
178
|
+
*/
|
|
179
|
+
getCacheKey(config: Req): string {
|
|
180
|
+
const { baseURL, method, url, params, data } = config;
|
|
181
|
+
// 注意:params 和 data 需要序列化,并保证对象属性顺序稳定性
|
|
182
|
+
return `${method}:${baseURL}/${url}:${this.stableStringify(params)}:${this.stableStringify(data)}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 序列化对象,保证属性顺序一致
|
|
187
|
+
* @param obj 任意值
|
|
188
|
+
* @returns JSON 字符串
|
|
189
|
+
*/
|
|
190
|
+
private stableStringify(obj: unknown): string {
|
|
191
|
+
if (!obj || typeof obj !== "object") return JSON.stringify(obj);
|
|
192
|
+
const sorted = Object.keys(obj)
|
|
193
|
+
.sort()
|
|
194
|
+
.reduce((acc, key) => {
|
|
195
|
+
acc[key] = (obj as Record<string, unknown>)[key];
|
|
196
|
+
return acc;
|
|
197
|
+
}, {} as Record<string, unknown>);
|
|
198
|
+
return JSON.stringify(sorted);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export default RequestCache;
|
package/src/array-data-model.ts
CHANGED
package/src/axios.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axiosDef, { AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
2
2
|
import Cookies from "js-cookie";
|
|
3
|
-
import
|
|
3
|
+
import { merge } from "lodash-es";
|
|
4
4
|
|
|
5
5
|
import { CacheItem } from "./RequestCache";
|
|
6
6
|
|
|
@@ -55,7 +55,7 @@ export function setToken(token = "", opt?: SetTokenOptions) {
|
|
|
55
55
|
const _t = token || "";
|
|
56
56
|
if (hasCookie) {
|
|
57
57
|
const { cookieAttrs } = opt || {};
|
|
58
|
-
const cookieOpt =
|
|
58
|
+
const cookieOpt = merge(
|
|
59
59
|
{
|
|
60
60
|
expires: 365,
|
|
61
61
|
},
|
package/src/data-model.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { merge, each, isString, isNumber, isBoolean, pickBy, isNil, cloneDeep, isObject } from "lodash-es";
|
|
2
2
|
|
|
3
3
|
import { axios, isCancel } from "./axios";
|
|
4
4
|
|
|
@@ -407,9 +407,9 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
407
407
|
throw new Error(errMsg);
|
|
408
408
|
}
|
|
409
409
|
let apiUrl = api;
|
|
410
|
-
const params =
|
|
411
|
-
|
|
412
|
-
if (!
|
|
410
|
+
const params = merge({}, record, ctx);
|
|
411
|
+
each(params, (value, key) => {
|
|
412
|
+
if (!isString(value) || !isNumber(value) || isBoolean(value)) {
|
|
413
413
|
apiUrl = apiUrl.replace(new RegExp(`:${key}$|:${key}(?=/)`), value);
|
|
414
414
|
}
|
|
415
415
|
});
|
|
@@ -424,8 +424,8 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
424
424
|
* @returns Promise<any>
|
|
425
425
|
*/
|
|
426
426
|
async get(q: QueryParams = {}, ctx: JSONObject = {}, axiosConf?: AxiosConfig): Promise<any> {
|
|
427
|
-
let query =
|
|
428
|
-
query =
|
|
427
|
+
let query = merge({}, this.query, q);
|
|
428
|
+
query = pickBy(query, (val) => !isNil(val) && val !== "");
|
|
429
429
|
|
|
430
430
|
if (this.getReqMap) {
|
|
431
431
|
query = await this.getReqMap(query);
|
|
@@ -467,8 +467,8 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
467
467
|
* @returns Promise<GetListResult<T>>
|
|
468
468
|
*/
|
|
469
469
|
async getList(q: QueryParams = {}, ctx: JSONObject = {}, axiosConf?: AxiosConfig): Promise<GetListResult<T>> {
|
|
470
|
-
let query =
|
|
471
|
-
query =
|
|
470
|
+
let query = merge({}, this.query, q);
|
|
471
|
+
query = pickBy(query, (val) => !isNil(val) && val !== "");
|
|
472
472
|
|
|
473
473
|
if (this.getListReqMap) {
|
|
474
474
|
query = await this.getListReqMap(query);
|
|
@@ -525,7 +525,7 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
525
525
|
...this.createAxiosConf,
|
|
526
526
|
...axiosConf,
|
|
527
527
|
};
|
|
528
|
-
let _params =
|
|
528
|
+
let _params = cloneDeep(formDataToObj(params));
|
|
529
529
|
if (this.createReqMap) {
|
|
530
530
|
_params = await this.createReqMap(_params, params);
|
|
531
531
|
}
|
|
@@ -562,7 +562,7 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
562
562
|
update(params?: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<any> {
|
|
563
563
|
return new Promise(async (resolve, reject) => {
|
|
564
564
|
const opt: AxiosConfig = { ...this.axiosConf, ...this.updateAxiosConf, ...axiosConf };
|
|
565
|
-
let _params =
|
|
565
|
+
let _params = cloneDeep(formDataToObj(params));
|
|
566
566
|
if (this.updateReqMap) {
|
|
567
567
|
_params = await this.updateReqMap(_params, params);
|
|
568
568
|
}
|
|
@@ -599,7 +599,7 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
599
599
|
patch(params?: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<any> {
|
|
600
600
|
return new Promise(async (resolve, reject) => {
|
|
601
601
|
const opt: AxiosConfig = { ...this.axiosConf, ...this.patchAxiosConf, ...axiosConf };
|
|
602
|
-
let _params =
|
|
602
|
+
let _params = cloneDeep(formDataToObj(params));
|
|
603
603
|
if (this.patchReqMap) {
|
|
604
604
|
_params = await this.patchReqMap(_params, params);
|
|
605
605
|
}
|
|
@@ -635,14 +635,14 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
635
635
|
* @returns Promise<any>
|
|
636
636
|
*/
|
|
637
637
|
async delete(config?: DeleteConfig, ctx?: JSONObject): Promise<any> {
|
|
638
|
-
let _config =
|
|
638
|
+
let _config = cloneDeep(config) || {};
|
|
639
639
|
if (this.deleteReqMap) {
|
|
640
640
|
_config = await this.deleteReqMap(_config);
|
|
641
641
|
}
|
|
642
642
|
return new Promise((resolve, reject) => {
|
|
643
643
|
const apiUrl = this.getApiUrl(
|
|
644
644
|
this.deleteApi,
|
|
645
|
-
Object.assign(
|
|
645
|
+
Object.assign(cloneDeep(_config), _config.params, _config.data),
|
|
646
646
|
ctx,
|
|
647
647
|
{ from: "delete" },
|
|
648
648
|
);
|
|
@@ -673,14 +673,14 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
673
673
|
* @returns Promise<any>
|
|
674
674
|
*/
|
|
675
675
|
async multipleDelete(config?: DeleteConfig, ctx?: JSONObject): Promise<any> {
|
|
676
|
-
let _config =
|
|
676
|
+
let _config = cloneDeep(config) || {};
|
|
677
677
|
if (this.multipleDeleteReqMap) {
|
|
678
678
|
_config = await this.multipleDeleteReqMap(_config);
|
|
679
679
|
}
|
|
680
680
|
return new Promise((resolve, reject) => {
|
|
681
681
|
const apiUrl = this.getApiUrl(
|
|
682
682
|
this.multipleDeleteApi,
|
|
683
|
-
Object.assign(
|
|
683
|
+
Object.assign(cloneDeep(_config), _config.params, _config.data),
|
|
684
684
|
ctx,
|
|
685
685
|
{ from: "multipleDelete" },
|
|
686
686
|
);
|
|
@@ -741,7 +741,7 @@ class DataModel<T = any, R = Record<string, any>> {
|
|
|
741
741
|
const message = this.handleMsg(response);
|
|
742
742
|
if (code == 200) {
|
|
743
743
|
const _data = data ?? {};
|
|
744
|
-
if (
|
|
744
|
+
if (isObject(_data)) {
|
|
745
745
|
if (Array.isArray(_data.content) && _data.pageNumber) {
|
|
746
746
|
_data.list = _data.content;
|
|
747
747
|
_data.pagination = { current: _data.pageNumber, total: _data.total };
|
package/src/hooks.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { useMemo, useRef } from "react";
|
|
2
|
-
import { merge } from "lodash";
|
|
3
|
-
import DataModel, { DataModelOptions } from "./data-model";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* useDataModel 选项接口
|
|
7
|
-
*/
|
|
8
|
-
export interface UseDataModelOptions<T = any> {
|
|
9
|
-
/** 动态数据监听的目标 */
|
|
10
|
-
effectTargets?: any[];
|
|
11
|
-
/** 动态的 params 数据,包含了 query */
|
|
12
|
-
effectParams?: Partial<DataModelOptions<T>>;
|
|
13
|
-
/** 动态的 query 数据 */
|
|
14
|
-
effectQuery?: DataModelOptions<T>["query"];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 解决 hooks 重复实例化导致 query 丢失的问题
|
|
19
|
-
* @param {Object} initParams 初始参数
|
|
20
|
-
* @param {Object} opt
|
|
21
|
-
* @param {Object} opt.effectTargets 动态数据监听的目标
|
|
22
|
-
* @param {Object} opt.effectParams 动态的 params 数据,包含了 query
|
|
23
|
-
* @param {Object} opt.effectQuery 动态的 query 数据
|
|
24
|
-
* @returns DataModel 实例
|
|
25
|
-
*/
|
|
26
|
-
export const useDataModel = function <T = any>(initParams: DataModelOptions<T> = {}, opt: UseDataModelOptions<T> = {}) {
|
|
27
|
-
const model = useRef<DataModel<T>>(new DataModel(initParams));
|
|
28
|
-
return useMemo(() => {
|
|
29
|
-
const { effectParams, effectQuery } = opt || {};
|
|
30
|
-
if (effectParams) {
|
|
31
|
-
merge(model.current, effectParams);
|
|
32
|
-
}
|
|
33
|
-
if (effectQuery) {
|
|
34
|
-
merge(model.current.query, effectQuery);
|
|
35
|
-
}
|
|
36
|
-
return model.current;
|
|
37
|
-
}, opt.effectTargets || []);
|
|
38
|
-
};
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
import { merge } from "lodash-es";
|
|
3
|
+
import DataModel, { DataModelOptions } from "./data-model";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* useDataModel 选项接口
|
|
7
|
+
*/
|
|
8
|
+
export interface UseDataModelOptions<T = any> {
|
|
9
|
+
/** 动态数据监听的目标 */
|
|
10
|
+
effectTargets?: any[];
|
|
11
|
+
/** 动态的 params 数据,包含了 query */
|
|
12
|
+
effectParams?: Partial<DataModelOptions<T>>;
|
|
13
|
+
/** 动态的 query 数据 */
|
|
14
|
+
effectQuery?: DataModelOptions<T>["query"];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 解决 hooks 重复实例化导致 query 丢失的问题
|
|
19
|
+
* @param {Object} initParams 初始参数
|
|
20
|
+
* @param {Object} opt
|
|
21
|
+
* @param {Object} opt.effectTargets 动态数据监听的目标
|
|
22
|
+
* @param {Object} opt.effectParams 动态的 params 数据,包含了 query
|
|
23
|
+
* @param {Object} opt.effectQuery 动态的 query 数据
|
|
24
|
+
* @returns DataModel 实例
|
|
25
|
+
*/
|
|
26
|
+
export const useDataModel = function <T = any>(initParams: DataModelOptions<T> = {}, opt: UseDataModelOptions<T> = {}) {
|
|
27
|
+
const model = useRef<DataModel<T>>(new DataModel(initParams));
|
|
28
|
+
return useMemo(() => {
|
|
29
|
+
const { effectParams, effectQuery } = opt || {};
|
|
30
|
+
if (effectParams) {
|
|
31
|
+
merge(model.current, effectParams);
|
|
32
|
+
}
|
|
33
|
+
if (effectQuery) {
|
|
34
|
+
merge(model.current.query, effectQuery);
|
|
35
|
+
}
|
|
36
|
+
return model.current;
|
|
37
|
+
}, opt.effectTargets || []);
|
|
38
|
+
};
|