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

feature: 增加 timeoutMessage 配置 #267

Merged
merged 1 commit into from
Sep 14, 2021
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
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ The network request library, based on fetch encapsulation, combines the features
| :------------------- | :--------------------- | :--------------------- | :------------- |
| implementation | Browser native support | Browser native support | XMLHttpRequest |
| size | 9k | 4k (polyfill) | 14k |
| query simplification | ✅ | ❌ | ✅ |
| post simplification | ✅ | ❌ | ❌ |
| timeout | ✅ | ❌ | ✅ |
| cache | ✅ | ❌ | ❌ |
| error Check | ✅ | ❌ | ❌ |
| error Handling | ✅ | ❌ | ✅ |
| interceptor | ✅ | ❌ | ✅ |
| prefix | ✅ | ❌ | ❌ |
| suffix | ✅ | ❌ | ❌ |
| processing gbk | ✅ | ❌ | ❌ |
| middleware | ✅ | ❌ | ❌ |
| cancel request | ✅ | ❌ | ✅ |
| query simplification | ✅ | ❌ | ✅ |
| post simplification | ✅ | ❌ | ❌ |
| timeout | ✅ | ❌ | ✅ |
| cache | ✅ | ❌ | ❌ |
| error Check | ✅ | ❌ | ❌ |
| error Handling | ✅ | ❌ | ✅ |
| interceptor | ✅ | ❌ | ✅ |
| prefix | ✅ | ❌ | ❌ |
| suffix | ✅ | ❌ | ❌ |
| processing gbk | ✅ | ❌ | ❌ |
| middleware | ✅ | ❌ | ❌ |
| cancel request | ✅ | ❌ | ✅ |

For more discussion, refer to [Traditional Ajax is dead, Fetch eternal life](https://github.com/camsong/blog/issues/2) If you have good suggestions and needs, please mention [issue](https://github.com/umijs/umi/issues)

Expand Down Expand Up @@ -227,7 +227,8 @@ More umi-request cases can see [antd-pro](https://github.com/umijs/ant-design-pr
| params | url request parameters | object or URLSearchParams | -- | -- |
| data | Submitted data | any | -- | -- |
| headers | fetch original parameters | object | -- | {} |
| timeout | timeout, default millisecond, write with caution | number | -- | -- |
| timeout | timeout, default millisecond, write with caution | number | -- |
| timeoutMessage | customize timeout error message, please config `timeout` first | string | -- | -- |
| prefix | prefix, generally used to override the uniform settings prefix | string | -- | -- |
| suffix | suffix, such as some scenes api need to be unified .json | string | -- |
| credentials | fetch request with cookies | string | -- | credentials: 'same-origin' |
Expand Down
27 changes: 14 additions & 13 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
| :--------- | :------------- | :------------- | :------------- |
| 实现 | 浏览器原生支持 | 浏览器原生支持 | XMLHttpRequest |
| 大小 | 9k | 4k (polyfill) | 14k |
| query 简化 | ✅ | ❌ | ✅ |
| post 简化 | ✅ | ❌ | ❌ |
| 超时 | ✅ | ❌ | ✅ |
| 缓存 | ✅ | ❌ | ❌ |
| 错误检查 | ✅ | ❌ | ❌ |
| 错误处理 | ✅ | ❌ | ✅ |
| 拦截器 | ✅ | ❌ | ✅ |
| 前缀 | ✅ | ❌ | ❌ |
| 后缀 | ✅ | ❌ | ❌ |
| 处理 gbk | ✅ | ❌ | ❌ |
| 中间件 | ✅ | ❌ | ❌ |
| 取消请求 | ✅ | ❌ | ✅ |
| query 简化 | ✅ | ❌ | ✅ |
| post 简化 | ✅ | ❌ | ❌ |
| 超时 | ✅ | ❌ | ✅ |
| 缓存 | ✅ | ❌ | ❌ |
| 错误检查 | ✅ | ❌ | ❌ |
| 错误处理 | ✅ | ❌ | ✅ |
| 拦截器 | ✅ | ❌ | ✅ |
| 前缀 | ✅ | ❌ | ❌ |
| 后缀 | ✅ | ❌ | ❌ |
| 处理 gbk | ✅ | ❌ | ❌ |
| 中间件 | ✅ | ❌ | ❌ |
| 取消请求 | ✅ | ❌ | ✅ |

更多讨论参考[传统 Ajax 已死,Fetch 永生](https://github.com/camsong/blog/issues/2), 如果你有好的建议和需求, 请提 [issue](https://github.com/umijs/umi/issues)

Expand Down Expand Up @@ -233,7 +233,8 @@ umi-request 可以进行一层简单封装后再使用, 可参考 [antd-pro](htt
| params | url 请求参数 | object 或 URLSearchParams 对象 | -- | -- |
| data | 提交的数据 | any | -- | -- |
| headers | fetch 原有参数 | object | -- | {} |
| timeout | 超时时长, 默认毫秒, 写操作慎用 | number | -- | -- |
| timeout | 超时时长, 默认毫秒, 写操作慎用 | number | -- |
| timeoutMessage | 超时可自定义提示文案, 需先定义 timeout | string | -- | -- |
| prefix | 前缀, 一般用于覆盖统一设置的 prefix | string | -- | -- |
| suffix | 后缀, 比如某些场景 api 需要统一加 .json | string | -- | -- |
| credentials | fetch 请求包含 cookies 信息 | string | -- | credentials: 'same-origin' |
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function fetchMiddleware(ctx, next) {
const { req: { options = {}, url = '' } = {}, cache, responseInterceptors } = ctx;
const {
timeout = 0,
timeoutMessage,
__umiRequestCoreType__ = 'normal',
useCache = false,
method = 'get',
Expand Down Expand Up @@ -59,7 +60,7 @@ export default function fetchMiddleware(ctx, next) {
let response;
// 超时处理、取消请求处理
if (timeout > 0) {
response = Promise.race([cancel2Throw(options, ctx), adapter(url, options), timeout2Throw(timeout, ctx.req)]);
response = Promise.race([cancel2Throw(options, ctx), adapter(url, options), timeout2Throw(timeout, timeoutMessage, ctx.req)]);
} else {
response = Promise.race([cancel2Throw(options, ctx), adapter(url, options)]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export function safeJsonParse(data, throwErrIfParseFail = false, response = null
return data;
}

export function timeout2Throw(msec, request) {
export function timeout2Throw(msec, timeoutMessage, request) {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new RequestError(`timeout of ${msec}ms exceeded`, request, 'Timeout'));
reject(new RequestError(timeoutMessage || `timeout of ${msec}ms exceeded`, request, 'Timeout'));
}, msec);
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/timeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('timeout', () => {
response = await request(prefix('/test/timeout'), { timeout: 800 });
} catch (error) {
expect(error.name).toBe('RequestError');
expect(error.message).toBe('timeout of 800ms exceeded');
expect(error.message).toBe(error.request.options.timeoutMessage || 'timeout of 800ms exceeded');
expect(error.request.options.timeout).toBe(800);
done();
}
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface RequestOptionsInit extends RequestInit {
useCache?: boolean;
ttl?: number;
timeout?: number;
timeoutMessage?: string;
errorHandler?: (error: ResponseError) => void;
prefix?: string;
suffix?: string;
Expand Down