@nova-kits/hooks 1.0.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/LICENSE +21 -0
- package/dist/config.d.ts +59 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +102 -0
- package/dist/index.umd.cjs +1 -0
- package/dist/useRequest/index.d.ts +79 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 floral-rice
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type InjectionKey, type App, type Component } from 'vue';
|
|
2
|
+
export interface HooksConfig {
|
|
3
|
+
/**
|
|
4
|
+
* 显示加载中的 toast
|
|
5
|
+
* @returns toast 实例,用于取消显示
|
|
6
|
+
*/
|
|
7
|
+
showToast: () => any;
|
|
8
|
+
/**
|
|
9
|
+
* 取消 toast 显示
|
|
10
|
+
* @param toast toast 实例
|
|
11
|
+
*/
|
|
12
|
+
cancelToast: (toast: any) => void;
|
|
13
|
+
/**
|
|
14
|
+
* 显示错误信息
|
|
15
|
+
* @param err 错误对象
|
|
16
|
+
*/
|
|
17
|
+
showError: (err: Error) => void;
|
|
18
|
+
/**
|
|
19
|
+
* 显示加载中状态
|
|
20
|
+
* @returns 加载中组件或 null
|
|
21
|
+
*/
|
|
22
|
+
showLoading: () => Component | null;
|
|
23
|
+
/**
|
|
24
|
+
* 错误结果展示
|
|
25
|
+
* @param err 错误对象
|
|
26
|
+
* @param refresh 刷新函数
|
|
27
|
+
* @returns 错误展示组件或 null
|
|
28
|
+
*/
|
|
29
|
+
errorResult: (err: Error, refresh: () => void) => Component | null;
|
|
30
|
+
/**
|
|
31
|
+
* 设置主题色
|
|
32
|
+
* @param val 颜色值
|
|
33
|
+
*/
|
|
34
|
+
setPrimaryColor: (val: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
* 默认 throttle 时间,便于移动端使用
|
|
37
|
+
*/
|
|
38
|
+
requestThrottleInterval: number;
|
|
39
|
+
/**
|
|
40
|
+
* request lock 防止 useRequest 重复调用
|
|
41
|
+
*/
|
|
42
|
+
requestLock?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare const defaultHooksConfig: HooksConfig;
|
|
45
|
+
export declare const HOOKS_CONFIG_KEY: InjectionKey<HooksConfig>;
|
|
46
|
+
/**
|
|
47
|
+
* 获取 hooks 配置
|
|
48
|
+
*/
|
|
49
|
+
export declare function useHooksConfig(): HooksConfig;
|
|
50
|
+
/**
|
|
51
|
+
* 提供 hooks 配置
|
|
52
|
+
*/
|
|
53
|
+
export declare function provideHooksConfig(config: Partial<HooksConfig>): void;
|
|
54
|
+
/**
|
|
55
|
+
* 创建 hooks 插件,用于全局配置
|
|
56
|
+
*/
|
|
57
|
+
export declare function createHooksPlugin(config: Partial<HooksConfig>): {
|
|
58
|
+
install(app: App): void;
|
|
59
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { inject as F, provide as K, ref as t, onMounted as _, watch as j, onUnmounted as x } from "vue";
|
|
2
|
+
import { debounce as G, throttle as M } from "lodash-es";
|
|
3
|
+
const a = (e) => {
|
|
4
|
+
console.warn(`please implement: ${e}`);
|
|
5
|
+
}, I = {
|
|
6
|
+
showToast() {
|
|
7
|
+
return a("showToast"), null;
|
|
8
|
+
},
|
|
9
|
+
cancelToast() {
|
|
10
|
+
a("cancelToast");
|
|
11
|
+
},
|
|
12
|
+
showError(e) {
|
|
13
|
+
console.error(e);
|
|
14
|
+
},
|
|
15
|
+
errorResult() {
|
|
16
|
+
return a("errorResult"), null;
|
|
17
|
+
},
|
|
18
|
+
setPrimaryColor() {
|
|
19
|
+
a("setPrimaryColor");
|
|
20
|
+
},
|
|
21
|
+
showLoading() {
|
|
22
|
+
return a("showLoading"), null;
|
|
23
|
+
},
|
|
24
|
+
requestThrottleInterval: 0,
|
|
25
|
+
requestLock: !1
|
|
26
|
+
}, R = /* @__PURE__ */ Symbol("hooks-config");
|
|
27
|
+
function N() {
|
|
28
|
+
return F(R, I);
|
|
29
|
+
}
|
|
30
|
+
function U(e) {
|
|
31
|
+
K(R, { ...I, ...e });
|
|
32
|
+
}
|
|
33
|
+
function z(e) {
|
|
34
|
+
return {
|
|
35
|
+
install(w) {
|
|
36
|
+
U(e);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function A(e, w = {}) {
|
|
41
|
+
const r = N(), {
|
|
42
|
+
onSuccess: q,
|
|
43
|
+
onError: g,
|
|
44
|
+
throttleInterval: k = r.requestThrottleInterval || 0,
|
|
45
|
+
debounceInterval: p = 0,
|
|
46
|
+
loadingState: T = !0,
|
|
47
|
+
onLoading: u,
|
|
48
|
+
toast: H = !1,
|
|
49
|
+
manual: i = !0,
|
|
50
|
+
defaultData: L = null,
|
|
51
|
+
refreshDeps: y = [],
|
|
52
|
+
onFinally: S,
|
|
53
|
+
lock: b = r.requestLock || !1,
|
|
54
|
+
showError: O
|
|
55
|
+
} = w, C = O || r.showError, c = t(!1), E = t(L), n = t(!1), v = t(0), h = t(!1), l = t(null), P = () => {
|
|
56
|
+
H && !l.value && (l.value = r.showToast() || !0), T && (c.value = !0), u && u(!0);
|
|
57
|
+
}, d = () => {
|
|
58
|
+
l.value && (r.cancelToast(l.value), l.value = null), n.value && (T && (c.value = !1), u && u(!1));
|
|
59
|
+
}, s = async (...m) => {
|
|
60
|
+
if (h.value)
|
|
61
|
+
return;
|
|
62
|
+
b && (h.value = !0), v.value += 1, P();
|
|
63
|
+
const D = v.value;
|
|
64
|
+
try {
|
|
65
|
+
const o = await e(...m);
|
|
66
|
+
n.value && D === v.value ? (d(), q?.(o, m), i || (E.value = o)) : n.value || d();
|
|
67
|
+
} catch (o) {
|
|
68
|
+
d(), g ? g(o, m, () => {
|
|
69
|
+
C(o);
|
|
70
|
+
}) : C(o);
|
|
71
|
+
} finally {
|
|
72
|
+
S?.(), h.value = !1;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
let f;
|
|
76
|
+
return p ? f = G(s, p) : k ? f = M(s, k, { trailing: !1 }) : f = s, _(() => {
|
|
77
|
+
n.value = !0, i || s();
|
|
78
|
+
}), y.length > 0 && !i && j(y, () => {
|
|
79
|
+
s();
|
|
80
|
+
}), x(() => {
|
|
81
|
+
n.value = !1;
|
|
82
|
+
}), {
|
|
83
|
+
/**
|
|
84
|
+
* 手动执行函数
|
|
85
|
+
*/
|
|
86
|
+
run: f,
|
|
87
|
+
/**
|
|
88
|
+
* loading状态
|
|
89
|
+
*/
|
|
90
|
+
loading: c,
|
|
91
|
+
/**
|
|
92
|
+
* 数据
|
|
93
|
+
*/
|
|
94
|
+
data: E
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
z as createHooksPlugin,
|
|
99
|
+
U as provideHooksConfig,
|
|
100
|
+
N as useHooksConfig,
|
|
101
|
+
A as useRequest
|
|
102
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue"),require("lodash-es")):typeof define=="function"&&define.amd?define(["exports","vue","lodash-es"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o.NovaKits={},o.Vue,o.lodashEs))})(this,(function(o,e,k){"use strict";const r=t=>{console.warn(`please implement: ${t}`)},m={showToast(){return r("showToast"),null},cancelToast(){r("cancelToast")},showError(t){console.error(t)},errorResult(){return r("errorResult"),null},setPrimaryColor(){r("setPrimaryColor")},showLoading(){return r("showLoading"),null},requestThrottleInterval:0,requestLock:!1},p=Symbol("hooks-config");function y(){return e.inject(p,m)}function T(t){e.provide(p,{...m,...t})}function P(t){return{install(C){T(t)}}}function b(t,C={}){const s=y(),{onSuccess:O,onError:q,throttleInterval:H=s.requestThrottleInterval||0,debounceInterval:E=0,loadingState:R=!0,onLoading:i,toast:j=!1,manual:c=!0,defaultData:K=null,refreshDeps:S=[],onFinally:D,lock:F=s.requestLock||!1,showError:M}=C,I=M||s.showError,d=e.ref(!1),L=e.ref(K),l=e.ref(!1),h=e.ref(0),v=e.ref(!1),a=e.ref(null),N=()=>{j&&!a.value&&(a.value=s.showToast()||!0),R&&(d.value=!0),i&&i(!0)},g=()=>{a.value&&(s.cancelToast(a.value),a.value=null),l.value&&(R&&(d.value=!1),i&&i(!1))},u=async(...w)=>{if(v.value)return;F&&(v.value=!0),h.value+=1,N();const _=h.value;try{const n=await t(...w);l.value&&_===h.value?(g(),O?.(n,w),c||(L.value=n)):l.value||g()}catch(n){g(),q?q(n,w,()=>{I(n)}):I(n)}finally{D?.(),v.value=!1}};let f;return E?f=k.debounce(u,E):H?f=k.throttle(u,H,{trailing:!1}):f=u,e.onMounted(()=>{l.value=!0,c||u()}),S.length>0&&!c&&e.watch(S,()=>{u()}),e.onUnmounted(()=>{l.value=!1}),{run:f,loading:d,data:L}}o.createHooksPlugin=P,o.provideHooksConfig=T,o.useHooksConfig=y,o.useRequest=b,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
4
|
+
* @param request 网络请求方法
|
|
5
|
+
* @param options 参数
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export default function useRequest<T extends (...args: any[]) => Promise<any> | any, K = T extends (...args: any) => Promise<infer U> | infer U ? U : never>(request: T, options?: {
|
|
9
|
+
/**
|
|
10
|
+
* loading状态变化监听函数
|
|
11
|
+
*/
|
|
12
|
+
onLoading?: (loading: boolean) => void;
|
|
13
|
+
/**
|
|
14
|
+
* 成功回调
|
|
15
|
+
* @param res request返回的数据
|
|
16
|
+
* @param params request的参数
|
|
17
|
+
*/
|
|
18
|
+
onSuccess?: (res: K, params: Parameters<T>) => void;
|
|
19
|
+
/**
|
|
20
|
+
* 错误回调
|
|
21
|
+
* @param err 异常,抛出异常的时候务必是Error的实例
|
|
22
|
+
* @param params request的参数
|
|
23
|
+
* @param showError 调用全局的错误信息提示
|
|
24
|
+
*/
|
|
25
|
+
onError?: (err: Error, params: Parameters<T>, showError: () => void) => void;
|
|
26
|
+
/**
|
|
27
|
+
* 最终回调
|
|
28
|
+
*/
|
|
29
|
+
onFinally?: () => void;
|
|
30
|
+
/**
|
|
31
|
+
* 节流模式的时间
|
|
32
|
+
*/
|
|
33
|
+
throttleInterval?: number;
|
|
34
|
+
/**
|
|
35
|
+
* 防抖模式的时间
|
|
36
|
+
*/
|
|
37
|
+
debounceInterval?: number;
|
|
38
|
+
/**
|
|
39
|
+
* 启用loading状态
|
|
40
|
+
*/
|
|
41
|
+
loadingState?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* 是否展示加载中的toast
|
|
44
|
+
*/
|
|
45
|
+
toast?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* 是否需要手动执行 默认 true,非手动模式默认会返回request返回的数据
|
|
48
|
+
*/
|
|
49
|
+
manual?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 自动模式data的默认值,否则就是null
|
|
52
|
+
*/
|
|
53
|
+
defaultData?: K;
|
|
54
|
+
/**
|
|
55
|
+
* 非手动模式自动刷新的依赖项
|
|
56
|
+
*/
|
|
57
|
+
refreshDeps?: Ref<any>[];
|
|
58
|
+
/**
|
|
59
|
+
* 在上一次未完成之前,防止重复调用
|
|
60
|
+
*/
|
|
61
|
+
lock?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* 自定义错误处理,覆盖全局配置
|
|
64
|
+
*/
|
|
65
|
+
showError?: (err: Error) => void;
|
|
66
|
+
}): {
|
|
67
|
+
/**
|
|
68
|
+
* 手动执行函数
|
|
69
|
+
*/
|
|
70
|
+
run: (...args: Parameters<T>) => void;
|
|
71
|
+
/**
|
|
72
|
+
* loading状态
|
|
73
|
+
*/
|
|
74
|
+
loading: Ref<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* 数据
|
|
77
|
+
*/
|
|
78
|
+
data: Ref<K>;
|
|
79
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nova-kits/hooks",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Nova Kits Vue hooks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./src/*": "./src/*"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"lodash-es": "^4.17.21"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"vue": "^3.5.24"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/lodash-es": "^4.17.12",
|
|
27
|
+
"@vitejs/plugin-vue": "^6.0.0",
|
|
28
|
+
"typescript": "^6.0.3",
|
|
29
|
+
"vite": "^7.2.4",
|
|
30
|
+
"vue-tsc": "^3.1.4"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "vite build && vue-tsc --declaration --emitDeclarationOnly --outDir dist"
|
|
34
|
+
}
|
|
35
|
+
}
|