From 9b560a9a3e95e6a6a67bf51306cb9b7566052143 Mon Sep 17 00:00:00 2001 From: Kenrick Tandrian <60643640+KenTandrian@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:32:33 +0700 Subject: [PATCH] Task/improve type safety in data provider integrations (#8450) Improve type safety --- .../data-provider/manual/manual.service.ts | 20 +++++++++++++------ .../rapid-api/rapid-api.service.ts | 19 ++++++++++-------- .../yahoo-finance/yahoo-finance.service.ts | 10 +++++----- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/apps/api/src/services/data-provider/manual/manual.service.ts b/apps/api/src/services/data-provider/manual/manual.service.ts index 30168cdef8..15009ae04a 100644 --- a/apps/api/src/services/data-provider/manual/manual.service.ts +++ b/apps/api/src/services/data-provider/manual/manual.service.ts @@ -47,7 +47,7 @@ export class ManualService implements DataProviderInterface { public async getAssetProfile({ symbol - }: GetAssetProfileParams): Promise> { + }: GetAssetProfileParams): Promise | undefined> { const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles([ { symbol, dataSource: this.getName() } ]); @@ -86,8 +86,8 @@ export class ManualService implements DataProviderInterface { const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles( [{ symbol, dataSource: this.getName() }] ); - const { defaultMarketPrice, selector, url } = - symbolProfile?.scraperConfiguration ?? {}; + const scraperConfiguration = symbolProfile?.scraperConfiguration; + const { defaultMarketPrice, selector, url } = scraperConfiguration ?? {}; if (defaultMarketPrice) { const historical: { @@ -105,15 +105,19 @@ export class ManualService implements DataProviderInterface { } return historical; - } else if (!selector || !url) { + } else if (!scraperConfiguration || !selector || !url) { return {}; } const value = await this.scrape({ symbol, - scraperConfiguration: symbolProfile.scraperConfiguration + scraperConfiguration }); + if (value === undefined) { + return {}; + } + return { [format(getStartOfUtcDateOfYesterday(), DATE_FORMAT, { in: utc })]: { marketPrice: value @@ -178,6 +182,10 @@ export class ManualService implements DataProviderInterface { const scraperResultPromises = symbolProfilesToScrape.map( async ({ scraperConfiguration, symbol }) => { + if (!scraperConfiguration) { + return { symbol, marketPrice: undefined }; + } + try { const marketPrice = await this.scrape({ scraperConfiguration, @@ -294,7 +302,7 @@ export class ManualService implements DataProviderInterface { }: { scraperConfiguration: ScraperConfiguration; symbol: string; - }): Promise { + }): Promise { let locale = scraperConfiguration.locale; const response = await this.fetchService.fetch(scraperConfiguration.url, { diff --git a/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts b/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts index 07dc62393a..34e5081e64 100644 --- a/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts +++ b/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts @@ -39,7 +39,7 @@ export class RapidApiService implements DataProviderInterface { } public async getAssetProfile({}: GetAssetProfileParams): Promise< - Partial + Partial | undefined > { return undefined; } @@ -133,13 +133,16 @@ export class RapidApiService implements DataProviderInterface { return { items: [] }; } - private async getFearAndGreedIndex(): Promise<{ - now: { value: number; valueText: string }; - previousClose: { value: number; valueText: string }; - oneWeekAgo: { value: number; valueText: string }; - oneMonthAgo: { value: number; valueText: string }; - oneYearAgo: { value: number; valueText: string }; - }> { + private async getFearAndGreedIndex(): Promise< + | { + now: { value: number; valueText: string }; + previousClose: { value: number; valueText: string }; + oneWeekAgo: { value: number; valueText: string }; + oneMonthAgo: { value: number; valueText: string }; + oneYearAgo: { value: number; valueText: string }; + } + | undefined + > { try { const { fgi } = await this.fetchService .fetch(`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`, { diff --git a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts index aef498cbd6..098d888a71 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts @@ -27,10 +27,7 @@ import { ReasonPhrases, StatusCodes } from 'http-status-codes'; import { uniqBy } from 'lodash-es'; import YahooFinance from 'yahoo-finance2'; import type { ChartResultArray } from 'yahoo-finance2/modules/chart'; -import type { - HistoricalDividendsResult, - HistoricalHistoryResult -} from 'yahoo-finance2/modules/historical'; +import type { HistoricalDividendsResult } from 'yahoo-finance2/modules/historical'; import type { Quote, QuoteResponseArray } from 'yahoo-finance2/modules/quote'; import type { Price, @@ -380,7 +377,7 @@ export class YahooFinanceService implements DataProviderInterface { private convertToHistoricalResult( result: ChartResultArray - ): HistoricalHistoryResult { + ): ChartResultArray['quotes'] { return result.quotes; } @@ -405,6 +402,9 @@ export class YahooFinanceService implements DataProviderInterface { ) .map(({ value }) => { return value.price; + }) + .filter((price): price is Price => { + return Boolean(price); }); } }