fix: add timeout for fetch

This commit is contained in:
Gabe Yuan
2024-03-19 14:25:37 +08:00
parent 9ee4c20250
commit bda83ce76e

View File

@@ -12,6 +12,8 @@ import {
import { isBg } from "./browser";
import { newCacheReq, newTransReq } from "./req";
const TIMEOUT = 8000;
/**
* 油猴脚本的请求封装
* @param {*} input
@@ -26,6 +28,7 @@ export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
headers,
data: body,
// withCredentials: true,
timeout: TIMEOUT,
onload: ({ response, responseHeaders, status, statusText, ...opts }) => {
const headers = {};
responseHeaders.split("\n").forEach((line) => {
@@ -86,6 +89,10 @@ export const fetchApi = async ({ input, init, transOpts, apiSetting }) => {
}
}
if (AbortSignal?.timeout) {
Object.assign(init, { signal: AbortSignal.timeout(TIMEOUT) });
}
return fetch(input, init);
};