@wzyjs/hooks 0.0.13

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.
@@ -0,0 +1,3 @@
1
+ export * from 'ahooks';
2
+ export { useCopyToClipboard, useCookie } from 'react-use';
3
+ export * from './useRequestPro';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from 'ahooks';
2
+ export { useCopyToClipboard, useCookie } from 'react-use';
3
+ export * from './useRequestPro';
@@ -0,0 +1,11 @@
1
+ import { RequestRes } from '@wzyjs/utils';
2
+ import { Options, Service, Result } from 'ahooks/lib/useRequest/src/types';
3
+ interface UserOptions<P extends any[], R> extends Options<R, P> {
4
+ alertErrorMessage?: false | string;
5
+ alertSuccessMessage?: true | string;
6
+ }
7
+ interface IResult<R extends RequestRes, P extends any[]> extends Omit<Result<R, P>, 'data'> {
8
+ data?: R['data'];
9
+ }
10
+ export declare const useRequestPro: <P extends any[], R extends RequestRes<any>>(reqPromise: Service<R, P>, userOptions: UserOptions<P, R>) => IResult<R, P>;
11
+ export {};
@@ -0,0 +1,34 @@
1
+ import { message } from 'antd';
2
+ import { omit } from '@wzyjs/utils';
3
+ import { useRequest } from 'ahooks';
4
+ export const useRequestPro = (reqPromise, userOptions) => {
5
+ const { alertErrorMessage = true, alertSuccessMessage = false } = userOptions;
6
+ const defaultOptions = {
7
+ debounceWait: 300,
8
+ throttleWait: 300,
9
+ onError: (e, params) => {
10
+ if (alertErrorMessage) {
11
+ message.error(typeof alertErrorMessage === 'string' ? alertErrorMessage : e.message || '请求失败');
12
+ }
13
+ userOptions?.onError?.(e, params);
14
+ },
15
+ onSuccess: (res, params) => {
16
+ // 如果失败,则还是调用 onError
17
+ if (!res?.success) {
18
+ defaultOptions.onError(new Error(res?.message), params);
19
+ return;
20
+ }
21
+ // 如果成功,并且需要自动弹出提示
22
+ if (alertSuccessMessage) {
23
+ message.success(typeof alertSuccessMessage === 'string' ? alertSuccessMessage : res.message || '请求成功');
24
+ }
25
+ userOptions?.onSuccess?.(res, params);
26
+ },
27
+ };
28
+ // 用户配置与默认配置合并
29
+ const options = Object.assign(defaultOptions, omit(userOptions, ['onSuccess', 'onError']));
30
+ // 取出 data 里的 data
31
+ const state = useRequest(reqPromise, options);
32
+ state.data = state.data?.data;
33
+ return state;
34
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@wzyjs/hooks",
3
+ "version": "0.0.13",
4
+ "description": "> TODO: description",
5
+ "author": "wzy",
6
+ "license": "ISC",
7
+ "main": "dist/index.js",
8
+ "typings": "dist/index.d.ts",
9
+ "scripts": {
10
+ "dev": "tsc -w",
11
+ "build": "tsc"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "dependencies": {
17
+ "@wzyjs/utils": "^0.0.13",
18
+ "ahooks": "^3.7.3",
19
+ "antd": "^5.1.0",
20
+ "react-use": "^17.4.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^18.11.17"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://gitee.com/wang-zhenyu/work.git"
31
+ },
32
+ "gitHead": "9ff9118177f6450701f5d42cd442830c8bd57bae"
33
+ }