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({
symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
}: GetAssetProfileParams): Promise<Partial<SymbolProfile> | 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<number> {
}): Promise<number | undefined> {
let locale = scraperConfiguration.locale;
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<
Partial<SymbolProfile>
Partial<SymbolProfile> | 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`, {

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 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);
});
}
}

Loading…
Cancel
Save