Skip to content

Commit

Permalink
improvement(userequest): userequest complete
Browse files Browse the repository at this point in the history
  • Loading branch information
innocces committed Jul 8, 2021
1 parent cc4dcdb commit 2bd618f
Show file tree
Hide file tree
Showing 18 changed files with 1,623 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@tarojs/runtime": "3.2.15",
"@tarojs/taro": "3.2.15",
"lodash": "4.17.15",
"mockjs": "^1.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"taro-hooks": "^0.0.0",
Expand Down
10 changes: 6 additions & 4 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
"README.md"
],
"dependencies": {
"@babel/runtime": "^7.14.6"
"@babel/runtime": "^7.14.6",
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
"@tarojs/taro": "3.2.15",
"react": "^17.0.2"
},
"peerDependencies": {
"@tarojs/taro": ">= 3.x",
"react": ">=16.9",
"react-dom": ">=16.9"
"react": ">=16.9"
},
"devDependencies": {},
"engines": {
"node": ">=12.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useBase64ToArrayBuffer from './useBase64ToArrayBuffer';
import useArrayBufferToBase64 from './useArrayBufferToBase64';
import useSystemInfo from './useSystemInfo';
import useEvent from './useEvent';
import useVisible from './useVisible';

// wechat
import useAPICheck from './useAPICheck';
Expand All @@ -22,6 +23,7 @@ export {
useArrayBufferToBase64,
useSystemInfo,
useEvent,
useVisible,
useAPICheck,
useUpdateManager,
useNetworkType,
Expand Down
37 changes: 37 additions & 0 deletions packages/hooks/src/useRequest/antdTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface PaginationConfig {
total?: number;
defaultCurrent?: number;
disabled?: boolean;
current?: number;
defaultPageSize?: number;
pageSize?: number;
onChange?: (page: number, pageSize?: number) => void;
hideOnSinglePage?: boolean;
showSizeChanger?: boolean;
pageSizeOptions?: string[];
onShowSizeChange?: (current: number, size: number) => void;
showQuickJumper?:
| boolean
| {
goButton?: React.ReactNode;
};
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
simple?: boolean;
style?: React.CSSProperties;
locale?: Object;
className?: string;
prefixCls?: string;
selectPrefixCls?: string;
itemRender?: (
page: number,
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
originalElement: React.ReactElement<HTMLElement>,
) => React.ReactNode;
role?: string;
showLessItems?: boolean;
[key: string]: any;
}

export type Sorter = any;

export type Filter = any;
1 change: 0 additions & 1 deletion packages/hooks/src/useRequest/helper/document.ts

This file was deleted.

248 changes: 240 additions & 8 deletions packages/hooks/src/useRequest/index.md

Large diffs are not rendered by default.

125 changes: 121 additions & 4 deletions packages/hooks/src/useRequest/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,125 @@
import { getEnv, ENV_TYPE } from '@tarojs/taro';
import { useEffect, useState } from 'react';
/* eslint-disable react-hooks/rules-of-hooks */
import { useRef } from 'react';
import { request } from '@tarojs/taro';
import {
BaseOptions,
BasePaginatedOptions,
BaseResult,
CombineService,
LoadMoreFormatReturn,
LoadMoreOptions,
LoadMoreOptionsWithFormat,
LoadMoreParams,
LoadMoreResult,
OptionsWithFormat,
PaginatedFormatReturn,
PaginatedOptionsWithFormat,
PaginatedParams,
PaginatedResult,
} from './types';
import useAsync from './useAsync';
import useLoadMore from './useLoadMore';
import usePaginated from './usePaginated';

export type Result = ENV_TYPE | '';
function useRequest<
R = any,
P extends any[] = any,
U = any,
UU extends U = any,
>(
service: CombineService<R, P>,
options: OptionsWithFormat<R, P, U, UU>,
): BaseResult<U, P>;
function useRequest<R = any, P extends any[] = any>(
service: CombineService<R, P>,
options?: BaseOptions<R, P>,
): BaseResult<R, P>;

function useRequest(service: any, options: any = {}) {}
function useRequest<R extends LoadMoreFormatReturn, RR>(
service: CombineService<RR, LoadMoreParams<R>>,
options: LoadMoreOptionsWithFormat<R, RR>,
): LoadMoreResult<R>;
function useRequest<R extends LoadMoreFormatReturn, RR extends R>(
service: CombineService<R, LoadMoreParams<R>>,
options: LoadMoreOptions<RR>,
): LoadMoreResult<R>;

function useRequest<R = any, Item = any, U extends Item = any>(
service: CombineService<R, PaginatedParams>,
options: PaginatedOptionsWithFormat<R, Item, U>,
): PaginatedResult<Item>;
function useRequest<R = any, Item = any, U extends Item = any>(
service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams>,
options: BasePaginatedOptions<U>,
): PaginatedResult<Item>;

function useRequest(service: any, options: any = {}) {
const finalOptions = { ...options };

const { paginated, loadMore, requestMethod } = finalOptions;

const paginatedRef = useRef(paginated);
const loadMoreRef = useRef(loadMore);

if (paginatedRef.current !== paginated) {
throw Error('You should not modify the paginated of options');
}

if (loadMoreRef.current !== loadMore) {
throw Error('You should not modify the loadMore of options');
}

paginatedRef.current = paginated;
loadMoreRef.current = loadMore;

// @ts-ignore
const fetchProxy = (url, config = {}) =>
// @ts-ignore
request({ url, ...config }).then((res: request.SuccessCallbackResult) => {
return res;
});

const finalRequestMethod = requestMethod || fetchProxy;

let promiseService: () => Promise<any>;
switch (typeof service) {
case 'string':
promiseService = () => finalRequestMethod(service);
break;
case 'object':
const { url, ...rest } = service;
promiseService = () =>
requestMethod ? requestMethod(service) : fetchProxy(url, rest);
break;
default:
promiseService = (...args: any[]) =>
new Promise((resolve, reject) => {
const s = service(...args);
let fn = s;
if (!s.then) {
switch (typeof s) {
case 'string':
fn = finalRequestMethod(s);
break;
case 'object':
const { url, ...rest } = s;
fn = requestMethod ? requestMethod(s) : fetchProxy(url, rest);
break;
}
}
fn.then(resolve).catch(reject);
});
}

if (loadMore) {
return useLoadMore(promiseService, finalOptions);
}
if (paginated) {
return usePaginated(promiseService, finalOptions);
}
return useAsync(promiseService, finalOptions);
}

export { useAsync, usePaginated, useLoadMore };

export default useRequest;
Loading

0 comments on commit 2bd618f

Please sign in to comment.