40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import type { MarketContext, PortfolioHolding, PortfolioAdvice } from '$lib/types.js';
|
|
import { authFetch } from './auth.js';
|
|
|
|
const BASE = '/api';
|
|
|
|
export async function fetchPortfolio(): Promise<{
|
|
advice: PortfolioAdvice[];
|
|
holdings: PortfolioHolding[];
|
|
marketContext: MarketContext | null;
|
|
netWorth: number | null;
|
|
error?: string;
|
|
}> {
|
|
const res = await authFetch(`${BASE}/finance/portfolio`);
|
|
if (!res.ok) throw new Error(await res.text());
|
|
return res.json();
|
|
}
|
|
|
|
export async function addHolding(
|
|
holding: PortfolioHolding,
|
|
): Promise<{ holdings: PortfolioHolding[] }> {
|
|
const res = await authFetch(`${BASE}/finance/holdings`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(holding),
|
|
});
|
|
if (!res.ok) throw new Error(await res.text());
|
|
return res.json();
|
|
}
|
|
|
|
export async function removeHolding(ticker: string): Promise<{ holdings: PortfolioHolding[] }> {
|
|
const res = await authFetch(`${BASE}/finance/holdings/${ticker}`, { method: 'DELETE' });
|
|
if (!res.ok) throw new Error(await res.text());
|
|
return res.json();
|
|
}
|
|
|
|
export async function fetchMarketContext(): Promise<MarketContext> {
|
|
const res = await authFetch(`${BASE}/finance/market-context`);
|
|
if (!res.ok) throw new Error(await res.text());
|
|
return res.json();
|
|
}
|