@hzab/data-model 2.0.0 → 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/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";
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
+ };
package/src/index.ts CHANGED
@@ -1,11 +1,11 @@
1
- import axios from "./axios";
2
- import DataModel from "./data-model";
3
-
4
- export * from "./axios";
5
- export * from "./data-model";
6
- export * from "./hooks";
7
- export * from "./type";
8
-
9
- export { axios, DataModel };
10
-
11
- export default DataModel;
1
+ import axios from "./axios";
2
+ import DataModel from "./data-model";
3
+
4
+ export * from "./axios";
5
+ export * from "./data-model";
6
+ export * from "./hooks";
7
+ export * from "./type";
8
+
9
+ export { axios, DataModel };
10
+
11
+ export default DataModel;
@@ -1,26 +1,26 @@
1
- /**
2
- * getPublicUrl 选项接口
3
- */
4
- export interface GetPublicUrlOptions {
5
- /** 是否返回完整路径 */
6
- isFullPath?: boolean;
7
- }
8
-
9
- /**
10
- * 获取 public 文件路径(自动拼接 hash 数据)
11
- * @param url 文件路径
12
- * @param opt 选项
13
- * @returns 完整的 public 文件路径
14
- */
15
- export function getPublicUrl(url: string | any, opt?: GetPublicUrlOptions): string | any {
16
- if (typeof url !== "string") {
17
- return url;
18
- }
19
- const { isFullPath } = opt || {};
20
- const baseURL = (process.env.WEBPACK_PUBLIC_PATH || ".")?.replace(/^\//, "")?.replace(/\/$/, "");
21
- if (isFullPath) {
22
- const fullPathname = (location.origin + location.pathname).replace(/[^\/]+.html\/?$/, "").replace(/\/$/, "");
23
- return `${fullPathname}/${baseURL?.replace(/^\./, "")}/${url?.replace(/^\//, "")}`;
24
- }
25
- return `${baseURL}/${url?.replace(/^\//, "")}`;
26
- }
1
+ /**
2
+ * getPublicUrl 选项接口
3
+ */
4
+ export interface GetPublicUrlOptions {
5
+ /** 是否返回完整路径 */
6
+ isFullPath?: boolean;
7
+ }
8
+
9
+ /**
10
+ * 获取 public 文件路径(自动拼接 hash 数据)
11
+ * @param url 文件路径
12
+ * @param opt 选项
13
+ * @returns 完整的 public 文件路径
14
+ */
15
+ export function getPublicUrl(url: string | any, opt?: GetPublicUrlOptions): string | any {
16
+ if (typeof url !== "string") {
17
+ return url;
18
+ }
19
+ const { isFullPath } = opt || {};
20
+ const baseURL = (process.env.WEBPACK_PUBLIC_PATH || ".")?.replace(/^\//, "")?.replace(/\/$/, "");
21
+ if (isFullPath) {
22
+ const fullPathname = (location.origin + location.pathname).replace(/[^\/]+.html\/?$/, "").replace(/\/$/, "");
23
+ return `${fullPathname}/${baseURL?.replace(/^\./, "")}/${url?.replace(/^\//, "")}`;
24
+ }
25
+ return `${baseURL}/${url?.replace(/^\//, "")}`;
26
+ }
package/src/utils.ts CHANGED
@@ -1,98 +1,98 @@
1
- /** 默认错误提示文案 */
2
- const DEFAULT_MSG = "未知错误";
3
- /** 网络异常提示文案 */
4
- const NETWORK_MSG = "网络异常,请检查网络是否开启";
5
-
6
- /** 暂存相关变量 */
7
- export interface TempStore {
8
- defaultErrMsg: string;
9
- networkErrMsg: string;
10
- axios?: any;
11
- }
12
-
13
- export const _$Temp: TempStore = {
14
- defaultErrMsg: DEFAULT_MSG,
15
- networkErrMsg: NETWORK_MSG,
16
- };
17
-
18
- /**
19
- * 设置默认的 axios
20
- * @param ax axios 实例
21
- */
22
- export function setDefaultAxios(ax: any) {
23
- if (ax) {
24
- _$Temp.axios = ax;
25
- }
26
- }
27
-
28
- /**
29
- * 设置默认的错误提示文案
30
- * @param msg 错误提示文案
31
- */
32
- export function setDefaultErrMsg(msg: string) {
33
- if (msg) {
34
- _$Temp.defaultErrMsg = msg;
35
- }
36
- }
37
-
38
- /**
39
- * 设置默认的网络异常提示文案
40
- * @param msg 网络异常提示文案
41
- */
42
- export function setNetworkErrMsg(msg: string) {
43
- if (msg) {
44
- _$Temp.networkErrMsg = msg;
45
- }
46
- }
47
-
48
- /**
49
- * 检测网络状态
50
- * @returns 网络是否可用
51
- */
52
- export function checkNetwork(): boolean {
53
- if (typeof navigator === "undefined") {
54
- return true;
55
- }
56
- if (navigator.onLine === false) {
57
- return false;
58
- }
59
- const _navigator = navigator as any;
60
- const connection = _navigator.connection || _navigator.mozConnection || _navigator.webkitConnection;
61
- if (!connection) {
62
- return true;
63
- }
64
- const noNetwork = connection?.type === "none" || connection.downlink === 0 || connection.effectiveType === null;
65
- return !noNetwork;
66
- }
67
-
68
- /**
69
- * 对象转 FormData 格式
70
- * @param data 对象数据
71
- * @returns FormData 对象
72
- */
73
- export function objToFormData(data: Record<string, any>): FormData {
74
- if (data instanceof FormData) {
75
- return data;
76
- }
77
- const formData = new FormData();
78
- Object.keys(data)?.forEach((key) => {
79
- formData.set(key, data[key]);
80
- });
81
- return formData;
82
- }
83
-
84
- /**
85
- * FormData 转对象
86
- * @param formData FormData 对象
87
- * @returns 普通对象
88
- */
89
- export function formDataToObj(formData: FormData | any): Record<string, any> {
90
- if (!(formData instanceof FormData)) {
91
- return formData;
92
- }
93
- const tempData: Record<string, any> = {};
94
- for (const [key, value] of formData.entries()) {
95
- tempData[key] = value;
96
- }
97
- return tempData;
98
- }
1
+ /** 默认错误提示文案 */
2
+ const DEFAULT_MSG = "未知错误";
3
+ /** 网络异常提示文案 */
4
+ const NETWORK_MSG = "网络异常,请检查网络是否开启";
5
+
6
+ /** 暂存相关变量 */
7
+ export interface TempStore {
8
+ defaultErrMsg: string;
9
+ networkErrMsg: string;
10
+ axios?: any;
11
+ }
12
+
13
+ export const _$Temp: TempStore = {
14
+ defaultErrMsg: DEFAULT_MSG,
15
+ networkErrMsg: NETWORK_MSG,
16
+ };
17
+
18
+ /**
19
+ * 设置默认的 axios
20
+ * @param ax axios 实例
21
+ */
22
+ export function setDefaultAxios(ax: any) {
23
+ if (ax) {
24
+ _$Temp.axios = ax;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * 设置默认的错误提示文案
30
+ * @param msg 错误提示文案
31
+ */
32
+ export function setDefaultErrMsg(msg: string) {
33
+ if (msg) {
34
+ _$Temp.defaultErrMsg = msg;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * 设置默认的网络异常提示文案
40
+ * @param msg 网络异常提示文案
41
+ */
42
+ export function setNetworkErrMsg(msg: string) {
43
+ if (msg) {
44
+ _$Temp.networkErrMsg = msg;
45
+ }
46
+ }
47
+
48
+ /**
49
+ * 检测网络状态
50
+ * @returns 网络是否可用
51
+ */
52
+ export function checkNetwork(): boolean {
53
+ if (typeof navigator === "undefined") {
54
+ return true;
55
+ }
56
+ if (navigator.onLine === false) {
57
+ return false;
58
+ }
59
+ const _navigator = navigator as any;
60
+ const connection = _navigator.connection || _navigator.mozConnection || _navigator.webkitConnection;
61
+ if (!connection) {
62
+ return true;
63
+ }
64
+ const noNetwork = connection?.type === "none" || connection.downlink === 0 || connection.effectiveType === null;
65
+ return !noNetwork;
66
+ }
67
+
68
+ /**
69
+ * 对象转 FormData 格式
70
+ * @param data 对象数据
71
+ * @returns FormData 对象
72
+ */
73
+ export function objToFormData(data: Record<string, any>): FormData {
74
+ if (data instanceof FormData) {
75
+ return data;
76
+ }
77
+ const formData = new FormData();
78
+ Object.keys(data)?.forEach((key) => {
79
+ formData.set(key, data[key]);
80
+ });
81
+ return formData;
82
+ }
83
+
84
+ /**
85
+ * FormData 转对象
86
+ * @param formData FormData 对象
87
+ * @returns 普通对象
88
+ */
89
+ export function formDataToObj(formData: FormData | any): Record<string, any> {
90
+ if (!(formData instanceof FormData)) {
91
+ return formData;
92
+ }
93
+ const tempData: Record<string, any> = {};
94
+ for (const [key, value] of formData.entries()) {
95
+ tempData[key] = value;
96
+ }
97
+ return tempData;
98
+ }