Files
PolyWeather/frontend/app/api/scan/terminal/route.ts
T
2026-05-28 09:23:01 +08:00

66 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { proxyBackendJsonGet } from "@/lib/api-proxy";
import { buildForceRefreshProxyCachePolicy } from "@/lib/proxy-cache-policy";
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
const SCAN_TERMINAL_PROXY_TIMEOUT_MS = Number(
process.env.POLYWEATHER_SCAN_TERMINAL_PROXY_TIMEOUT_MS || "40000",
);
export const maxDuration = 45;
export async function GET(req: NextRequest) {
if (!API_BASE) {
return NextResponse.json(
{ error: "POLYWEATHER_API_BASE_URL is not configured" },
{ status: 500 },
);
}
const params = new URLSearchParams();
const forceRefresh = req.nextUrl.searchParams.get("force_refresh") ?? "false";
for (const key of [
"scan_mode",
"min_price",
"max_price",
"min_edge_pct",
"min_liquidity",
"high_liquidity_only",
"market_type",
"time_range",
"limit",
"force_refresh",
"timezone_offset_seconds",
]) {
const value = req.nextUrl.searchParams.get(key);
if (value != null && value !== "") {
params.set(key, value);
}
}
const tradingRegion = req.nextUrl.searchParams.get("trading_region");
if (tradingRegion != null && tradingRegion !== "") {
params.set("region", tradingRegion);
}
const cachePolicy = buildForceRefreshProxyCachePolicy(forceRefresh, 10);
const url = `${API_BASE}/api/scan/terminal?${params.toString()}`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), SCAN_TERMINAL_PROXY_TIMEOUT_MS);
try {
return await proxyBackendJsonGet(req, {
cacheControl: cachePolicy.responseCacheControl,
fetchCache:
cachePolicy.fetchMode === "no-store" ? "no-store" : undefined,
publicMessage: "Failed to fetch scan terminal data",
revalidateSeconds: cachePolicy.revalidateSeconds,
signal: controller.signal,
timeoutPublicMessage: "Scan terminal request timed out",
url,
});
} finally {
clearTimeout(timeoutId);
}
}