Consolidate server utilities into shared module
- Add lib/server/utils/normalize.ts with normalizeTicker, normalizeTagsOrNull, nowIso, todayIso - Add lib/server/utils/validation.ts with asRecord, asBoolean, asStringArray, asEnum - Add lib/server/utils/index.ts re-exporting all utilities - Remove duplicate lib/server/utils.ts (old file) - Update all repos and files to use shared utilities - Remove redundant ?? '' from normalizeTicker calls - Update watchlist.ts to use normalizeTagsOrNull for null-return tags
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { normalizeTicker } from '@/lib/server/utils';
|
||||
|
||||
const YAHOO_BASE = 'https://query1.finance.yahoo.com/v8/finance/chart';
|
||||
const QUOTE_CACHE_TTL_MS = 1000 * 60;
|
||||
const PRICE_HISTORY_CACHE_TTL_MS = 1000 * 60 * 15;
|
||||
@@ -27,11 +29,11 @@ const quoteCache = new Map<string, QuoteCacheEntry>();
|
||||
const priceHistoryCache = new Map<string, PriceHistoryCacheEntry>();
|
||||
|
||||
function buildYahooChartUrl(ticker: string, params: string) {
|
||||
return `${YAHOO_BASE}/${encodeURIComponent(ticker.trim().toUpperCase())}?${params}`;
|
||||
return `${YAHOO_BASE}/${encodeURIComponent(normalizeTicker(ticker))}?${params}`;
|
||||
}
|
||||
|
||||
export async function getQuote(ticker: string): Promise<QuoteResult> {
|
||||
const normalizedTicker = ticker.trim().toUpperCase();
|
||||
const normalizedTicker = normalizeTicker(ticker);
|
||||
const cached = quoteCache.get(normalizedTicker);
|
||||
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
@@ -101,7 +103,7 @@ export async function getQuoteOrNull(ticker: string): Promise<number | null> {
|
||||
}
|
||||
|
||||
export async function getHistoricalClosingPrices(ticker: string, dates: string[]) {
|
||||
const normalizedTicker = ticker.trim().toUpperCase();
|
||||
const normalizedTicker = normalizeTicker(ticker);
|
||||
const normalizedDates = dates
|
||||
.map((value) => {
|
||||
const parsed = Date.parse(value);
|
||||
@@ -169,7 +171,7 @@ export async function getHistoricalClosingPrices(ticker: string, dates: string[]
|
||||
}
|
||||
|
||||
export async function getPriceHistory(ticker: string): Promise<PriceHistoryResult> {
|
||||
const normalizedTicker = ticker.trim().toUpperCase();
|
||||
const normalizedTicker = normalizeTicker(ticker);
|
||||
const cached = priceHistoryCache.get(normalizedTicker);
|
||||
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
|
||||
Reference in New Issue
Block a user