Skip to content

Commit

Permalink
feat: support useRequest param is string
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Mar 1, 2023
1 parent f3ebe8a commit 8ef438b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/plugin-request/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ interface RequestResult<R, P extends any[]> extends Result<R, P> {
}

export function useRequest<TData, TParams extends any[]>(
service: AxiosRequestConfig | Service<TData, TParams>,
service: string | AxiosRequestConfig | Service<TData, TParams>,
options?: Options<TData, TParams>,
plugins?: Plugin<TData, TParams>[]) {
let s: Service<TData, TParams>;
if (isFunction(service)) {
s = service as Service<TData, TParams>;
} else if (isString(service)) {
s = async (...extraOptions: TParams) => {
return request({ url: service, ...extraOptions });
};
} else {
const options = service as AxiosRequestConfig;
s = async (...extraOptions: TParams) => {
Expand All @@ -34,6 +38,10 @@ export function useRequest<TData, TParams extends any[]>(
} as RequestResult<TData, TParams>;
}

function isString(str: any): str is string {
return typeof str === 'string';
}

function isFunction(fn: any): fn is Function {
return typeof fn === 'function';
}
9 changes: 7 additions & 2 deletions website/docs/guide/advanced/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,20 @@ const {

```ts
import { useRequest } from 'ice';
// 用法 1:传入 Axios 配置对象
// 用法 1:传入请求地址
const { data, error, loading, request } = useRequest('/api/repo');

request();

// 用法 2:传入 Axios 配置对象
const { data, error, loading, request } = useRequest({
url: '/api/repo',
method: 'get',
});

request();

// 用法 2:传入 service 函数
// 用法 3:传入 service 函数
const { data, error, loading, request } = useRequest((id) => Promise.resolve({
url: '/api/repo',
method: 'get',
Expand Down

0 comments on commit 8ef438b

Please sign in to comment.