@vuetkit/core 0.0.1 → 0.0.3
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/README.md +6 -0
- package/dist/index.d.ts +23 -3
- package/dist/index.js +92 -4
- package/package.json +5 -2
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Ref } from "vue";
|
|
2
|
+
|
|
3
|
+
//#region src/network/useRequest/index.d.ts
|
|
4
|
+
interface RequestOptions<T> {
|
|
5
|
+
manual?: boolean;
|
|
6
|
+
defaultParams?: any;
|
|
7
|
+
initialData?: T;
|
|
8
|
+
delayLoadingTime?: number;
|
|
9
|
+
formatData?: (data: unknown) => T;
|
|
10
|
+
onSuccess?: (data: T) => void;
|
|
11
|
+
onError?: (error: any) => void;
|
|
12
|
+
onFinally?: () => void;
|
|
13
|
+
}
|
|
14
|
+
interface RequestReturn<T> {
|
|
15
|
+
loading: Ref<boolean>;
|
|
16
|
+
data: Ref<T | null>;
|
|
17
|
+
error: Ref<unknown>;
|
|
18
|
+
execute: (params?: any) => void;
|
|
19
|
+
cancel: () => void;
|
|
20
|
+
}
|
|
21
|
+
type RequestService = (params?: any) => Promise<unknown>;
|
|
22
|
+
declare function useRequest<T>(service: RequestService, options?: RequestOptions<T>): RequestReturn<T>;
|
|
3
23
|
//#endregion
|
|
4
|
-
export {
|
|
24
|
+
export { RequestOptions, RequestReturn, RequestService, useRequest };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,94 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { realObj } from "@vuetkit/shared";
|
|
2
|
+
import { onMounted, onUnmounted, ref, shallowRef, toValue } from "vue";
|
|
3
|
+
//#region src/network/useRequest/index.ts
|
|
4
|
+
function useRequest(service, options) {
|
|
5
|
+
const { initialData = null, manual, defaultParams, delayLoadingTime = 300, formatData, onSuccess, onError, onFinally } = options || {};
|
|
6
|
+
const loading = ref(false);
|
|
7
|
+
const data = shallowRef(initialData);
|
|
8
|
+
const error = ref();
|
|
9
|
+
const timer = ref(null);
|
|
10
|
+
const requestComplete = ref(false);
|
|
11
|
+
const isDiscardRequestData = ref(false);
|
|
12
|
+
const requestId = ref(0);
|
|
13
|
+
const cancel = () => {
|
|
14
|
+
loading.value = false;
|
|
15
|
+
isDiscardRequestData.value = true;
|
|
16
|
+
clearTimer();
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* get params
|
|
20
|
+
* @param oldParams old params
|
|
21
|
+
* @param newParams new params
|
|
22
|
+
* @returns request params
|
|
23
|
+
* @description get oldParams and newParams, if both are obj, merge them, otherwise newParams will be cover oldParams
|
|
24
|
+
*/
|
|
25
|
+
function getParams(oldParams, newParams) {
|
|
26
|
+
if (!oldParams && !newParams) return newParams;
|
|
27
|
+
if (oldParams) {
|
|
28
|
+
const oldParamsVal = toValue(oldParams);
|
|
29
|
+
if (newParams) {
|
|
30
|
+
const newParamsVal = toValue(newParams);
|
|
31
|
+
if (realObj(oldParamsVal) && realObj(newParamsVal)) return {
|
|
32
|
+
...oldParamsVal,
|
|
33
|
+
...newParamsVal
|
|
34
|
+
};
|
|
35
|
+
return newParamsVal;
|
|
36
|
+
}
|
|
37
|
+
return oldParamsVal;
|
|
38
|
+
}
|
|
39
|
+
return toValue(newParams);
|
|
40
|
+
}
|
|
41
|
+
function clearTimer() {
|
|
42
|
+
if (timer.value) {
|
|
43
|
+
clearTimeout(timer.value);
|
|
44
|
+
timer.value = null;
|
|
45
|
+
}
|
|
46
|
+
requestComplete.value = false;
|
|
47
|
+
}
|
|
48
|
+
function delayLoading() {
|
|
49
|
+
timer.value = setTimeout(() => {
|
|
50
|
+
if (!requestComplete.value) loading.value = true;
|
|
51
|
+
}, delayLoadingTime);
|
|
52
|
+
}
|
|
53
|
+
async function execute(params) {
|
|
54
|
+
try {
|
|
55
|
+
const currentRequestId = ++requestId.value;
|
|
56
|
+
isDiscardRequestData.value = false;
|
|
57
|
+
clearTimer();
|
|
58
|
+
const requestParams = getParams(defaultParams, params);
|
|
59
|
+
delayLoading();
|
|
60
|
+
const res = await service(requestParams);
|
|
61
|
+
error.value = null;
|
|
62
|
+
requestComplete.value = true;
|
|
63
|
+
if (currentRequestId !== requestId.value) return;
|
|
64
|
+
const formatRes = formatData ? formatData(res) : res;
|
|
65
|
+
if (isDiscardRequestData.value) return;
|
|
66
|
+
data.value = formatRes;
|
|
67
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(data.value);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
error.value = err;
|
|
70
|
+
onError === null || onError === void 0 || onError(err);
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimer();
|
|
73
|
+
loading.value = false;
|
|
74
|
+
isDiscardRequestData.value = false;
|
|
75
|
+
onFinally === null || onFinally === void 0 || onFinally();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
onMounted(() => {
|
|
79
|
+
if (!manual) execute();
|
|
80
|
+
});
|
|
81
|
+
onUnmounted(() => {
|
|
82
|
+
clearTimer();
|
|
83
|
+
requestId.value = 0;
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
loading,
|
|
87
|
+
data,
|
|
88
|
+
error,
|
|
89
|
+
execute,
|
|
90
|
+
cancel
|
|
91
|
+
};
|
|
4
92
|
}
|
|
5
93
|
//#endregion
|
|
6
|
-
export {
|
|
94
|
+
export { useRequest };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vuetkit/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "Collection of business development hooks for Vue3 projects",
|
|
6
6
|
"author": "Kalu5",
|
|
7
7
|
"license": "MIT",
|
|
@@ -10,12 +10,15 @@
|
|
|
10
10
|
"hooks"
|
|
11
11
|
],
|
|
12
12
|
"main": "dist/index.js",
|
|
13
|
-
"module": "dist/index.
|
|
13
|
+
"module": "dist/index.js",
|
|
14
14
|
"types": "dist/index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"README.md",
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"vue": "^3.5.35"
|
|
21
|
+
},
|
|
19
22
|
"scripts": {
|
|
20
23
|
"build": "tsdown --config-loader tsx"
|
|
21
24
|
}
|