import YahooFinance from 'yahoo-finance2'; import type { YahooNewsItem, YahooSearchOptions, YahooFinanceLib } from '../types'; import { YAHOO_MODULES } from '../config/constants'; export class YahooFinanceClient { private lib: YahooFinanceLib; constructor() { this.lib = new (YahooFinance as unknown as new (_opts: object) => YahooFinanceLib)({ suppressNotices: ['yahooSurvey'], }); } async fetchSummary(ticker: string, retries = 3, backoff = 1000): Promise { for (let attempt = 0; attempt < retries; attempt++) { try { return await this.lib.quoteSummary(ticker, { modules: YAHOO_MODULES }); } catch (error) { if (attempt === retries - 1) throw error; await new Promise((resolve) => setTimeout(resolve, backoff * (attempt + 1))); } } } async fetchCalendarEvents(ticker: string): Promise { try { const result = await this.lib.quoteSummary(ticker, { modules: ['calendarEvents'] }); return result.calendarEvents ?? null; } catch { return null; } } async search(query: string, opts: YahooSearchOptions = {}): Promise { const { news = [] } = await this.lib.search(query, opts); return news; } }