Skip to content

Commit

Permalink
feature: 增加 timeoutMessage 配置 (#267)
Browse files Browse the repository at this point in the history
Co-authored-by: shiluo.dwt <shiluo.dwt@antgroup.com>
  • Loading branch information
WynterDing and shiluo.dwt committed Sep 14, 2021
1 parent d0f3a45 commit 99c3281
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
27 changes: 14 additions & 13 deletions README.md
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
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
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
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
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
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

0 comments on commit 99c3281

Please sign in to comment.