fix(api): 改用 setGlobalDispatcher 方案,解决代理参数不兼容报错

This commit is contained in:
kennethcheng 2026-04-28 14:04:46 +08:00
parent f1d1b58ed9
commit 58db0b82ee

View File

@ -1,6 +1,6 @@
'use server'; 'use server';
import { ProxyAgent } from 'undici'; import { ProxyAgent, setGlobalDispatcher } from 'undici';
import { db } from '@/db'; import { db } from '@/db';
import { assets } from '@/db/schema'; import { assets } from '@/db/schema';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
@ -24,7 +24,10 @@ export async function syncAllMarketPrices() {
.from(assets); .from(assets);
const proxyUrl = process.env.HTTPS_PROXY; const proxyUrl = process.env.HTTPS_PROXY;
const proxyDispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined; if (proxyUrl) {
const proxyAgent = new ProxyAgent(proxyUrl);
setGlobalDispatcher(proxyAgent);
}
let successCount = 0; let successCount = 0;
@ -52,7 +55,7 @@ export async function syncAllMarketPrices() {
} }
} else if (asset.type === 'CRYPTO') { } else if (asset.type === 'CRYPTO') {
const cryptoSymbol = asset.symbol.trim().toUpperCase() + 'USDT'; const cryptoSymbol = asset.symbol.trim().toUpperCase() + 'USDT';
const response = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cryptoSymbol}`, { cache: 'no-store', dispatcher: proxyDispatcher } as RequestInit & { dispatcher?: any }); const response = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cryptoSymbol}`, { cache: 'no-store' });
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();