Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
import { UserService } from '@ghostfolio/client/services/user/user.service';
import {
HEADER_KEY_SKIP_INTERCEPTOR,
HEADER_KEY_TOKEN
} from '@ghostfolio/common/config';
import { DATE_FORMAT } from '@ghostfolio/common/helper';
import {
AiServiceHealthResponse,
DataProviderGhostfolioAssetProfileResponse,
DataProviderGhostfolioStatusResponse,
DividendsResponse,
HistoricalResponse,
LookupResponse,
MarketDataOfMarketsResponse,
QuotesResponse
} from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import { GfFearAndGreedIndexComponent } from '@ghostfolio/ui/fear-and-greed-index';
import { CommonModule } from '@angular/common';
import {
HttpClient,
HttpErrorResponse,
HttpHeaders,
HttpParams
} from '@angular/common/http';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
inject,
OnInit
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatCardModule } from '@angular/material/card';
import { format, startOfYear } from 'date-fns';
import { isObject } from 'lodash-es';
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import { catchError, map, Observable, of, OperatorFunction } from 'rxjs';
import { FetchFailure, FetchResult } from './interfaces/interfaces';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'page' },
imports: [
CommonModule,
GfFearAndGreedIndexComponent,
MatCardModule,
NgxSkeletonLoaderModule
],
selector: 'gf-api-page',
styleUrls: ['./api-page.scss'],
templateUrl: './api-page.html'
})
export class GfApiPageComponent implements OnInit {
protected aiServiceHealth$: Observable<FetchResult<AiServiceHealthResponse>>;
protected assetProfile$: Observable<
FetchResult<DataProviderGhostfolioAssetProfileResponse>
>;
protected dividends$: Observable<FetchResult<DividendsResponse['dividends']>>;
protected hasPermissionToAccessAdminControl: boolean;
protected historicalData$: Observable<
FetchResult<HistoricalResponse['historicalData']>
>;
protected isinLookupItems$: Observable<FetchResult<LookupResponse['items']>>;
protected lookupItems$: Observable<FetchResult<LookupResponse['items']>>;
protected marketDataOfMarkets$: Observable<
FetchResult<MarketDataOfMarketsResponse>
>;
protected quotes$: Observable<FetchResult<QuotesResponse['quotes']>>;
protected status$: Observable<
FetchResult<DataProviderGhostfolioStatusResponse>
>;
private apiKey: string;
private readonly changeDetectorRef = inject(ChangeDetectorRef);
private readonly destroyRef = inject(DestroyRef);
private readonly http = inject(HttpClient);
private readonly userService = inject(UserService);
public ngOnInit() {
this.apiKey =
prompt($localize`Please enter your Ghostfolio API key:`) ?? '';
this.userService.stateChanged
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((state) => {
this.hasPermissionToAccessAdminControl = hasPermission(
state?.user?.permissions,
permissions.accessAdminControl
);
this.changeDetectorRef.markForCheck();
});
this.aiServiceHealth$ = this.fetchAiServiceHealth();
this.assetProfile$ = this.fetchAssetProfile({ symbol: 'AAPL' });
this.dividends$ = this.fetchDividends({ symbol: 'KO' });
this.historicalData$ = this.fetchHistoricalData({ symbol: 'AAPL' });
this.isinLookupItems$ = this.fetchLookupItems({ query: 'US0378331005' });
this.lookupItems$ = this.fetchLookupItems({ query: 'apple' });
this.marketDataOfMarkets$ = this.fetchMarketDataOfMarkets();
this.quotes$ = this.fetchQuotes({ symbols: ['AAPL', 'VOO'] });
this.status$ = this.fetchStatus();
}
protected isFetchFailure(value: unknown): value is FetchFailure {
return isObject(value) && value !== null && 'fetchError' in value;
}
private catchFetchFailure<T>(): OperatorFunction<T, T | FetchFailure> {
return catchError(({ error }: HttpErrorResponse) => {
const body = error as { message?: string; status?: string };
const status = body?.status ?? 'Error';
const fetchError = body?.message ? `${status}: ${body.message}` : status;
return of<FetchFailure>({ fetchError });
}) as OperatorFunction<T, T | FetchFailure>;
}
private fetchAiServiceHealth() {
return this.http
.get<AiServiceHealthResponse>('/api/v1/health/ai')
.pipe(this.catchFetchFailure(), takeUntilDestroyed(this.destroyRef));
}
private fetchAssetProfile({ symbol }: { symbol: string }) {
return this.http
.get<DataProviderGhostfolioAssetProfileResponse>(
`/api/v1/data-providers/ghostfolio/asset-profile/${encodeURIComponent(symbol)}`,
{ headers: this.getHeaders() }
)
.pipe(this.catchFetchFailure(), takeUntilDestroyed(this.destroyRef));
}
private fetchDividends({ symbol }: { symbol: string }) {
const params = new HttpParams()
.set('from', format(startOfYear(new Date()), DATE_FORMAT))
.set('to', format(new Date(), DATE_FORMAT));
return this.http
.get<DividendsResponse>(
`/api/v2/data-providers/ghostfolio/dividends/${encodeURIComponent(symbol)}`,
{
params,
headers: this.getHeaders()
}
)
.pipe(
map(({ dividends }) => {
return dividends;
}),
this.catchFetchFailure(),
takeUntilDestroyed(this.destroyRef)
);
}
private fetchHistoricalData({ symbol }: { symbol: string }) {
const params = new HttpParams()
.set('from', format(startOfYear(new Date()), DATE_FORMAT))
.set('to', format(new Date(), DATE_FORMAT));
return this.http
.get<HistoricalResponse>(
`/api/v2/data-providers/ghostfolio/historical/${encodeURIComponent(symbol)}`,
{
params,
headers: this.getHeaders()
}
)
.pipe(
map(({ historicalData }) => {
return historicalData;
}),
this.catchFetchFailure(),
takeUntilDestroyed(this.destroyRef)
);
}
private fetchLookupItems({
includeIndices = false,
query
}: {
includeIndices?: boolean;
query: string;
}) {
let params = new HttpParams().set('query', query);
if (includeIndices) {
params = params.append('includeIndices', includeIndices);
}
return this.http
.get<LookupResponse>('/api/v2/data-providers/ghostfolio/lookup', {
params,
headers: this.getHeaders()
})
.pipe(
map(({ items }) => {
return items;
}),
this.catchFetchFailure(),
takeUntilDestroyed(this.destroyRef)
);
}
private fetchMarketDataOfMarkets() {
return this.http
.get<MarketDataOfMarketsResponse>(
'/api/v1/data-providers/ghostfolio/markets',
{ headers: this.getHeaders() }
)
.pipe(this.catchFetchFailure(), takeUntilDestroyed(this.destroyRef));
}
private fetchQuotes({ symbols }: { symbols: string[] }) {
const params = new HttpParams().set('symbols', symbols.join(','));
return this.http
.get<QuotesResponse>('/api/v2/data-providers/ghostfolio/quotes', {
params,
headers: this.getHeaders()
})
.pipe(
map(({ quotes }) => {
return quotes;
}),
this.catchFetchFailure(),
takeUntilDestroyed(this.destroyRef)
);
}
private fetchStatus() {
return this.http
.get<DataProviderGhostfolioStatusResponse>(
'/api/v2/data-providers/ghostfolio/status',
{ headers: this.getHeaders() }
)
.pipe(this.catchFetchFailure(), takeUntilDestroyed(this.destroyRef));
}
private getHeaders() {
return new HttpHeaders({
[HEADER_KEY_SKIP_INTERCEPTOR]: 'true',
[HEADER_KEY_TOKEN]: `Api-Key ${this.apiKey}`
});
}
}