Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

Tighten search exchange suffix fallback

keshav-0052c193bfacbc6
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<T>(url: string, revalidateSeconds?: number): Promise<T> {
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() : '';
+
+ if (!suffix) {
+ return 'US';
+ }
+
+ if (FINNHUB_EXCHANGE_SUFFIXES.has(suffix) || suffix.length >= 2) {
+ return suffix;
+ }
- return suffix || 'US';
+ return 'US';
}
export async function getQuote(symbol: string) {