Browse Source

Task/improve type safety in data provider integrations (#8450)

Improve type safety
pull/8453/merge
Kenrick Tandrian 5 days ago
committed by GitHub
parent
commit
9b560a9a3e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 20
      apps/api/src/services/data-provider/manual/manual.service.ts
  2. 19
      apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts
  3. 10
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

20
apps/api/src/services/data-provider/manual/manual.service.ts

@ -47,7 +47,7 @@ export class ManualService implements DataProviderInterface {
public async getAssetProfile({ public async getAssetProfile({
symbol symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> { }: GetAssetProfileParams): Promise<Partial<SymbolProfile> | undefined> {
const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles([ const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles([
{ symbol, dataSource: this.getName() } { symbol, dataSource: this.getName() }
]); ]);
@ -86,8 +86,8 @@ export class ManualService implements DataProviderInterface {
const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles( const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles(
[{ symbol, dataSource: this.getName() }] [{ symbol, dataSource: this.getName() }]
); );
const { defaultMarketPrice, selector, url } = const scraperConfiguration = symbolProfile?.scraperConfiguration;
symbolProfile?.scraperConfiguration ?? {}; const { defaultMarketPrice, selector, url } = scraperConfiguration ?? {};
if (defaultMarketPrice) { if (defaultMarketPrice) {
const historical: { const historical: {
@ -105,15 +105,19 @@ export class ManualService implements DataProviderInterface {
} }
return historical; return historical;
} else if (!selector || !url) { } else if (!scraperConfiguration || !selector || !url) {
return {}; return {};
} }
const value = await this.scrape({ const value = await this.scrape({
symbol, symbol,
scraperConfiguration: symbolProfile.scraperConfiguration scraperConfiguration
}); });
if (value === undefined) {
return {};
}
return { return {
[format(getStartOfUtcDateOfYesterday(), DATE_FORMAT, { in: utc })]: { [format(getStartOfUtcDateOfYesterday(), DATE_FORMAT, { in: utc })]: {
marketPrice: value marketPrice: value
@ -178,6 +182,10 @@ export class ManualService implements DataProviderInterface {
const scraperResultPromises = symbolProfilesToScrape.map( const scraperResultPromises = symbolProfilesToScrape.map(
async ({ scraperConfiguration, symbol }) => { async ({ scraperConfiguration, symbol }) => {
if (!scraperConfiguration) {
return { symbol, marketPrice: undefined };
}
try { try {
const marketPrice = await this.scrape({ const marketPrice = await this.scrape({
scraperConfiguration, scraperConfiguration,
@ -294,7 +302,7 @@ export class ManualService implements DataProviderInterface {
}: { }: {
scraperConfiguration: ScraperConfiguration; scraperConfiguration: ScraperConfiguration;
symbol: string; symbol: string;
}): Promise<number> { }): Promise<number | undefined> {
let locale = scraperConfiguration.locale; let locale = scraperConfiguration.locale;
const response = await this.fetchService.fetch(scraperConfiguration.url, { const response = await this.fetchService.fetch(scraperConfiguration.url, {

19
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< public async getAssetProfile({}: GetAssetProfileParams): Promise<
Partial<SymbolProfile> Partial<SymbolProfile> | undefined
> { > {
return undefined; return undefined;
} }
@ -133,13 +133,16 @@ export class RapidApiService implements DataProviderInterface {
return { items: [] }; return { items: [] };
} }
private async getFearAndGreedIndex(): Promise<{ private async getFearAndGreedIndex(): Promise<
now: { value: number; valueText: string }; | {
previousClose: { value: number; valueText: string }; now: { value: number; valueText: string };
oneWeekAgo: { value: number; valueText: string }; previousClose: { value: number; valueText: string };
oneMonthAgo: { value: number; valueText: string }; oneWeekAgo: { value: number; valueText: string };
oneYearAgo: { value: number; valueText: string }; oneMonthAgo: { value: number; valueText: string };
}> { oneYearAgo: { value: number; valueText: string };
}
| undefined
> {
try { try {
const { fgi } = await this.fetchService const { fgi } = await this.fetchService
.fetch(`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`, { .fetch(`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`, {

10
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 { uniqBy } from 'lodash-es';
import YahooFinance from 'yahoo-finance2'; import YahooFinance from 'yahoo-finance2';
import type { ChartResultArray } from 'yahoo-finance2/modules/chart'; import type { ChartResultArray } from 'yahoo-finance2/modules/chart';
import type { import type { HistoricalDividendsResult } from 'yahoo-finance2/modules/historical';
HistoricalDividendsResult,
HistoricalHistoryResult
} from 'yahoo-finance2/modules/historical';
import type { Quote, QuoteResponseArray } from 'yahoo-finance2/modules/quote'; import type { Quote, QuoteResponseArray } from 'yahoo-finance2/modules/quote';
import type { import type {
Price, Price,
@ -380,7 +377,7 @@ export class YahooFinanceService implements DataProviderInterface {
private convertToHistoricalResult( private convertToHistoricalResult(
result: ChartResultArray result: ChartResultArray
): HistoricalHistoryResult { ): ChartResultArray['quotes'] {
return result.quotes; return result.quotes;
} }
@ -405,6 +402,9 @@ export class YahooFinanceService implements DataProviderInterface {
) )
.map(({ value }) => { .map(({ value }) => {
return value.price; return value.price;
})
.filter((price): price is Price => {
return Boolean(price);
}); });
} }
} }

Loading…
Cancel
Save