mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 13:17:32 +00:00
🐛 retry
This commit is contained in:
parent
7d9f33fa49
commit
92bb7993b4
1 changed files with 12 additions and 8 deletions
|
|
@ -23,11 +23,11 @@ const _request = async (url, options) => {
|
||||||
* @param {number} retry 重试次数
|
* @param {number} retry 重试次数
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
const request = (url, options, retry = 0, timeout = 15000) => {
|
const request = (url, options = {}, retry = 0, timeout = 15000) => {
|
||||||
let err;
|
let err;
|
||||||
const controller = new AbortController()
|
const controller = new AbortController();
|
||||||
const { signal } = controller
|
const { signal } = controller;
|
||||||
const timeoutId = setTimeout(() => controller.abort(), timeout)
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
||||||
|
|
||||||
const _fetch = async (url, options, retryCount = 0) => {
|
const _fetch = async (url, options, retryCount = 0) => {
|
||||||
if (retryCount > retry) {
|
if (retryCount > retry) {
|
||||||
|
|
@ -36,11 +36,15 @@ const request = (url, options, retry = 0, timeout = 15000) => {
|
||||||
try {
|
try {
|
||||||
return await _request(url, { signal, ...options });
|
return await _request(url, { signal, ...options });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.debug(error.name === 'AbortError' ? 'Request timed out' : `Fetch error: ${error.message}`);
|
logger.debug(
|
||||||
|
error.name === 'AbortError'
|
||||||
|
? 'Request timed out'
|
||||||
|
: `Fetch error: ${error.message}`
|
||||||
|
);
|
||||||
err = error;
|
err = error;
|
||||||
return await _fetch(url, options, retryCount + 1);
|
return await _fetch(url, options, retryCount + 1);
|
||||||
} finally {
|
} finally {
|
||||||
clearTimeout(timeoutId)
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return _fetch(url, options);
|
return _fetch(url, options);
|
||||||
|
|
@ -53,7 +57,7 @@ const request = (url, options, retry = 0, timeout = 15000) => {
|
||||||
* @param {object} options 请求配置
|
* @param {object} options 请求配置
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
request.get = async (url, data, options) => {
|
request.get = async (url, data, options = {}) => {
|
||||||
const params = new URLSearchParams(data);
|
const params = new URLSearchParams(data);
|
||||||
const { retry, timeout, ...restOptions } = options;
|
const { retry, timeout, ...restOptions } = options;
|
||||||
return request(`${url}?${params}`, restOptions, retry, timeout);
|
return request(`${url}?${params}`, restOptions, retry, timeout);
|
||||||
|
|
@ -66,7 +70,7 @@ request.get = async (url, data, options) => {
|
||||||
* @param {object} options 请求配置
|
* @param {object} options 请求配置
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
request.post = async (url, data, options) => {
|
request.post = async (url, data, options = {}) => {
|
||||||
const body = JSON.stringify(data);
|
const body = JSON.stringify(data);
|
||||||
const { retry, timeout, ...restOptions } = options;
|
const { retry, timeout, ...restOptions } = options;
|
||||||
return request(url, { ...restOptions, method: 'POST', body }, retry, timeout);
|
return request(url, { ...restOptions, method: 'POST', body }, retry, timeout);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue