Files
Wrangler-API/src/utils/httpClient.ts
Jurangren 37f47f981a refactor: 统一日志输出方式并移除冗余检查
*   移除 `src/utils/httpClient.ts` 中自定义的 `logToCF` 日志函数。
*   将 `src/core.ts` 中所有 `logToCF` 调用替换为 `console.log` 进行结构化日志输出。
*   删除 `src/platforms/gal/xxacg.ts` 中无匹配项但存在 HTML 的特定错误检查。
2025-08-23 00:21:14 +08:00

41 lines
1.1 KiB
TypeScript

const TIMEOUT_SECONDS = 15;
const HEADERS = {
"Connection": "close",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 (From SearchGal.homes) (https://github.com/Moe-Sakura/SearchGal)",
};
/**
* 一个封装了原生 fetch 并增加了超时功能的 HTTP 客户端。
* @param url 请求的 URL。
* @param options fetch 的请求选项。
* @returns 返回一个 Promise<Response>。
*/
export async function fetchClient(
url: string | URL,
options: RequestInit = {}
): Promise<Response> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_SECONDS * 1000);
const finalOptions: RequestInit = {
...options,
headers: {
...HEADERS,
...options.headers,
},
signal: controller.signal,
};
try {
const response = await fetch(url, finalOptions);
return response;
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
throw new Error(`资源平台 SearchAPI 请求超时`);
}
throw error;
} finally {
clearTimeout(timeoutId);
}
}