Skip to content

Commit 2bd618f

Browse files
committed
improvement(userequest): userequest complete
1 parent cc4dcdb commit 2bd618f

18 files changed

+1623
-18
lines changed

packages/app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@tarojs/runtime": "3.2.15",
4343
"@tarojs/taro": "3.2.15",
4444
"lodash": "4.17.15",
45+
"mockjs": "^1.1.0",
4546
"react": "^17.0.2",
4647
"react-dom": "^17.0.2",
4748
"taro-hooks": "^0.0.0",

packages/hooks/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
"README.md"
3636
],
3737
"dependencies": {
38-
"@babel/runtime": "^7.14.6"
38+
"@babel/runtime": "^7.14.6",
39+
"lodash.debounce": "^4.0.8",
40+
"lodash.throttle": "^4.1.1",
41+
"@tarojs/taro": "3.2.15",
42+
"react": "^17.0.2"
3943
},
4044
"peerDependencies": {
4145
"@tarojs/taro": ">= 3.x",
42-
"react": ">=16.9",
43-
"react-dom": ">=16.9"
46+
"react": ">=16.9"
4447
},
45-
"devDependencies": {},
4648
"engines": {
4749
"node": ">=12.0.0"
4850
},

packages/hooks/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import useBase64ToArrayBuffer from './useBase64ToArrayBuffer';
66
import useArrayBufferToBase64 from './useArrayBufferToBase64';
77
import useSystemInfo from './useSystemInfo';
88
import useEvent from './useEvent';
9+
import useVisible from './useVisible';
910

1011
// wechat
1112
import useAPICheck from './useAPICheck';
@@ -22,6 +23,7 @@ export {
2223
useArrayBufferToBase64,
2324
useSystemInfo,
2425
useEvent,
26+
useVisible,
2527
useAPICheck,
2628
useUpdateManager,
2729
useNetworkType,
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export interface PaginationConfig {
2+
total?: number;
3+
defaultCurrent?: number;
4+
disabled?: boolean;
5+
current?: number;
6+
defaultPageSize?: number;
7+
pageSize?: number;
8+
onChange?: (page: number, pageSize?: number) => void;
9+
hideOnSinglePage?: boolean;
10+
showSizeChanger?: boolean;
11+
pageSizeOptions?: string[];
12+
onShowSizeChange?: (current: number, size: number) => void;
13+
showQuickJumper?:
14+
| boolean
15+
| {
16+
goButton?: React.ReactNode;
17+
};
18+
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
19+
simple?: boolean;
20+
style?: React.CSSProperties;
21+
locale?: Object;
22+
className?: string;
23+
prefixCls?: string;
24+
selectPrefixCls?: string;
25+
itemRender?: (
26+
page: number,
27+
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
28+
originalElement: React.ReactElement<HTMLElement>,
29+
) => React.ReactNode;
30+
role?: string;
31+
showLessItems?: boolean;
32+
[key: string]: any;
33+
}
34+
35+
export type Sorter = any;
36+
37+
export type Filter = any;

packages/hooks/src/useRequest/helper/document.ts

-1
This file was deleted.

packages/hooks/src/useRequest/index.md

+240-8
Large diffs are not rendered by default.
+121-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,125 @@
1-
import { getEnv, ENV_TYPE } from '@tarojs/taro';
2-
import { useEffect, useState } from 'react';
1+
/* eslint-disable react-hooks/rules-of-hooks */
2+
import { useRef } from 'react';
3+
import { request } from '@tarojs/taro';
4+
import {
5+
BaseOptions,
6+
BasePaginatedOptions,
7+
BaseResult,
8+
CombineService,
9+
LoadMoreFormatReturn,
10+
LoadMoreOptions,
11+
LoadMoreOptionsWithFormat,
12+
LoadMoreParams,
13+
LoadMoreResult,
14+
OptionsWithFormat,
15+
PaginatedFormatReturn,
16+
PaginatedOptionsWithFormat,
17+
PaginatedParams,
18+
PaginatedResult,
19+
} from './types';
20+
import useAsync from './useAsync';
21+
import useLoadMore from './useLoadMore';
22+
import usePaginated from './usePaginated';
323

4-
export type Result = ENV_TYPE | '';
24+
function useRequest<
25+
R = any,
26+
P extends any[] = any,
27+
U = any,
28+
UU extends U = any,
29+
>(
30+
service: CombineService<R, P>,
31+
options: OptionsWithFormat<R, P, U, UU>,
32+
): BaseResult<U, P>;
33+
function useRequest<R = any, P extends any[] = any>(
34+
service: CombineService<R, P>,
35+
options?: BaseOptions<R, P>,
36+
): BaseResult<R, P>;
537

6-
function useRequest(service: any, options: any = {}) {}
38+
function useRequest<R extends LoadMoreFormatReturn, RR>(
39+
service: CombineService<RR, LoadMoreParams<R>>,
40+
options: LoadMoreOptionsWithFormat<R, RR>,
41+
): LoadMoreResult<R>;
42+
function useRequest<R extends LoadMoreFormatReturn, RR extends R>(
43+
service: CombineService<R, LoadMoreParams<R>>,
44+
options: LoadMoreOptions<RR>,
45+
): LoadMoreResult<R>;
46+
47+
function useRequest<R = any, Item = any, U extends Item = any>(
48+
service: CombineService<R, PaginatedParams>,
49+
options: PaginatedOptionsWithFormat<R, Item, U>,
50+
): PaginatedResult<Item>;
51+
function useRequest<R = any, Item = any, U extends Item = any>(
52+
service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams>,
53+
options: BasePaginatedOptions<U>,
54+
): PaginatedResult<Item>;
55+
56+
function useRequest(service: any, options: any = {}) {
57+
const finalOptions = { ...options };
58+
59+
const { paginated, loadMore, requestMethod } = finalOptions;
60+
61+
const paginatedRef = useRef(paginated);
62+
const loadMoreRef = useRef(loadMore);
63+
64+
if (paginatedRef.current !== paginated) {
65+
throw Error('You should not modify the paginated of options');
66+
}
67+
68+
if (loadMoreRef.current !== loadMore) {
69+
throw Error('You should not modify the loadMore of options');
70+
}
71+
72+
paginatedRef.current = paginated;
73+
loadMoreRef.current = loadMore;
74+
75+
// @ts-ignore
76+
const fetchProxy = (url, config = {}) =>
77+
// @ts-ignore
78+
request({ url, ...config }).then((res: request.SuccessCallbackResult) => {
79+
return res;
80+
});
81+
82+
const finalRequestMethod = requestMethod || fetchProxy;
83+
84+
let promiseService: () => Promise<any>;
85+
switch (typeof service) {
86+
case 'string':
87+
promiseService = () => finalRequestMethod(service);
88+
break;
89+
case 'object':
90+
const { url, ...rest } = service;
91+
promiseService = () =>
92+
requestMethod ? requestMethod(service) : fetchProxy(url, rest);
93+
break;
94+
default:
95+
promiseService = (...args: any[]) =>
96+
new Promise((resolve, reject) => {
97+
const s = service(...args);
98+
let fn = s;
99+
if (!s.then) {
100+
switch (typeof s) {
101+
case 'string':
102+
fn = finalRequestMethod(s);
103+
break;
104+
case 'object':
105+
const { url, ...rest } = s;
106+
fn = requestMethod ? requestMethod(s) : fetchProxy(url, rest);
107+
break;
108+
}
109+
}
110+
fn.then(resolve).catch(reject);
111+
});
112+
}
113+
114+
if (loadMore) {
115+
return useLoadMore(promiseService, finalOptions);
116+
}
117+
if (paginated) {
118+
return usePaginated(promiseService, finalOptions);
119+
}
120+
return useAsync(promiseService, finalOptions);
121+
}
122+
123+
export { useAsync, usePaginated, useLoadMore };
7124

8125
export default useRequest;

0 commit comments

Comments
 (0)