@hzab/data-model 2.0.2 → 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/README.md +17 -0
- package/package.json +2 -3
- package/src/ArrayUtils.ts +712 -712
- package/src/RequestCache.ts +202 -0
- package/src/array-data-model.ts +1 -1
- package/src/axios.ts +245 -210
- package/src/data-model.ts +16 -16
- package/src/hooks.ts +38 -38
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
|
+
};
|