From 2c193bfacbc6afbf5effa41dd4eb1a1dd20ea149 Mon Sep 17 00:00:00 2001 From: keshav-005 Date: Wed, 22 Apr 2026 23:51:04 +0530 Subject: [PATCH] Tighten search exchange suffix fallback --- lib/actions/finnhub.actions.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/actions/finnhub.actions.ts b/lib/actions/finnhub.actions.ts index 18753b8..9262448 100644 --- a/lib/actions/finnhub.actions.ts +++ b/lib/actions/finnhub.actions.ts @@ -26,6 +26,13 @@ type SearchStockCandidate = FinnhubSearchResult & { __exchange?: string; }; +const FINNHUB_EXCHANGE_SUFFIXES = new Set([ + 'AS', 'AT', 'AX', 'BA', 'BK', 'BO', 'BR', 'CO', 'DE', 'F', 'HE', 'HK', + 'IL', 'IS', 'JK', 'JO', 'KL', 'KQ', 'KS', 'L', 'LS', 'MC', 'MI', 'MX', + 'NS', 'NZ', 'OL', 'PA', 'PR', 'SA', 'SI', 'SS', 'ST', 'SW', 'SZ', 'T', + 'TA', 'TO', 'TW', 'TWO', 'V', 'VI', 'WA', +]); + async function fetchJSON(url: string, revalidateSeconds?: number): Promise { const options: RequestInit & { next?: { revalidate?: number } } = revalidateSeconds ? { cache: 'force-cache', next: { revalidate: revalidateSeconds } } @@ -47,9 +54,17 @@ function getExchangeLabel(symbol: string, exchange?: string) { } const parts = symbol.split('.'); - const suffix = parts.length > 1 ? parts[parts.length - 1] : ''; + const suffix = parts.length > 1 ? parts[parts.length - 1].toUpperCase() : ''; - return suffix || 'US'; + if (!suffix) { + return 'US'; + } + + if (FINNHUB_EXCHANGE_SUFFIXES.has(suffix) || suffix.length >= 2) { + return suffix; + } + + return 'US'; } export async function getQuote(symbol: string) {