mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 13:17:32 +00:00
尝试解决图片第一次下载失败后无法自动解决图片裂开问题&驱动盘序号错位问题
This commit is contained in:
parent
8304b8bffa
commit
839ca76855
6 changed files with 153 additions and 56 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import http from 'http';
|
||||
import https from 'https';
|
||||
import fetch from 'node-fetch';
|
||||
export async function checkLatency(url) {
|
||||
let request = http;
|
||||
if (url.startsWith('https')) {
|
||||
|
|
@ -36,20 +35,3 @@ export function getQueryVariable(url, variable) {
|
|||
const key = searchParams.get(variable);
|
||||
return key;
|
||||
}
|
||||
|
||||
export async function fetchWithRetry(url, options, retry = 3) {
|
||||
let err;
|
||||
const _fetch = async (url, options, retryCount = 0) => {
|
||||
if (retryCount > retry) {
|
||||
throw new Error('Retry limit reached', err);
|
||||
}
|
||||
try {
|
||||
return await fetch(url, options);
|
||||
} catch (error) {
|
||||
logger.debug(`Fetch error: ${error.message}`);
|
||||
err = error;
|
||||
return _fetch(url, options, retryCount + 1);
|
||||
}
|
||||
};
|
||||
return _fetch(url, options);
|
||||
}
|
||||
|
|
|
|||
69
utils/request.js
Normal file
69
utils/request.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import fetch from 'node-fetch';
|
||||
|
||||
/**
|
||||
* 请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {object} options 请求配置
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
const _request = async (url, options) => {
|
||||
const response = await fetch(url, options);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
} else if (response.status < 200 || response.status >= 300) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
/**
|
||||
* 请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {object} options 请求配置
|
||||
* @param {number} retry 重试次数
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
const request = (url, options, retry = 0) => {
|
||||
let err;
|
||||
const _fetch = async (url, options, retryCount = 0) => {
|
||||
if (retryCount > retry) {
|
||||
throw new Error('Retry limit reached', err);
|
||||
}
|
||||
try {
|
||||
return await _request(url, options);
|
||||
} catch (error) {
|
||||
logger.debug(`Fetch error: ${error.message}`);
|
||||
err = error;
|
||||
return await _fetch(url, options, retryCount + 1);
|
||||
}
|
||||
};
|
||||
return _fetch(url, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* GET 请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {object} data 请求数据
|
||||
* @param {object} options 请求配置
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
request.get = async (url, data, options) => {
|
||||
const params = new URLSearchParams(data);
|
||||
const { retry, ...restOptions } = options;
|
||||
return request(`${url}?${params}`, restOptions, retry);
|
||||
};
|
||||
|
||||
/**
|
||||
* POST 请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {object} data 请求数据
|
||||
* @param {object} options 请求配置
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
request.post = async (url, data, options) => {
|
||||
const body = JSON.stringify(data);
|
||||
const { retry, ...restOptions } = options;
|
||||
return request(url, { ...restOptions, method: 'POST', body }, retry);
|
||||
};
|
||||
|
||||
export default request;
|
||||
Loading…
Add table
Add a link
Reference in a new issue