Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: request plugin type and usage #5992

Merged
merged 6 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nasty-lemons-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ice/plugin-request': patch
---

fix: request and useRequest type
fix: useRequest usage
1 change: 1 addition & 0 deletions packages/plugin-request/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './esm/hooks';
1 change: 1 addition & 0 deletions packages/plugin-request/request.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './esm/request';
19 changes: 12 additions & 7 deletions packages/plugin-request/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { useRequest as useAhooksRequest } from 'ahooks';
import type { Options, Result, Service, Plugin } from 'ahooks/lib/useRequest/src/types';
import type { AxiosRequestConfig } from 'axios';
import request from './request.js';
import { request } from './request.js';

interface RequestResult<R, P extends any[]> extends Result<R, P> {
request: (...args: P) => Promise<R>;
}

function useRequest<TData, TParams extends any[]>(
service: AxiosRequestConfig | Service<TData, TParams>,
export function useRequest<TData, TParams extends any[]>(
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) => {
const response = await request({ ...options, ...extraOptions });
return response.data as TData;
return request({ ...options, ...extraOptions });
};
}
const req = useAhooksRequest(s, {
// Note
// Note:
// ahooks/useRequest manual default to true.
// ICE3/useRequest Default to manual request.
manual: true,
Expand All @@ -35,7 +38,9 @@ function useRequest<TData, TParams extends any[]>(
} as RequestResult<TData, TParams>;
}

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

function isFunction(fn: any): fn is Function {
return typeof fn === 'function';
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const plugin: Plugin<PluginRequestOptions | void> = () => ({
// Add useRequest export for 'ice'.
// import { useRequest } from 'ice';
generator.addExport({
specifier: 'useRequest',
specifier: ['useRequest'],
source: '@ice/plugin-request/hooks',
type: false,
});
// import { request } from 'ice';
generator.addExport({
specifier: 'request',
specifier: ['request'],
source: '@ice/plugin-request/request',
type: false,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const request = async function <T = any, D = any>(options: RequestConfig<D>): Pr
console.error(error);
throw error;
}
};
} as Request;

// Provide aliases for supported request methods
['delete', 'get', 'head', 'options'].forEach((method) => {
Expand All @@ -121,4 +121,4 @@ const request = async function <T = any, D = any>(options: RequestConfig<D>): Pr
request.CancelToken = axios.CancelToken;
request.isCancel = axios.isCancel;

export default request as Request;
export { request };
18 changes: 12 additions & 6 deletions website/docs/guide/advanced/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,21 +341,27 @@ const {

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

// 用法 2:传入配置对象
const { data, error, loading } = useRequest({
request();

// 用法 2:传入 Axios 配置对象
const { data, error, loading, request } = useRequest({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useRequest('/api/repo'); 的方式不提供了吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像理论上可以支持,我顺手支持掉吧

url: '/api/repo',
method: 'get',
});

request();

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

request();
```

更多使用方式详见 [ahooks/useRequest](https://ahooks.js.org/zh-CN/hooks/use-request/index)
Expand Down