initial commit
This commit is contained in:
82
apps/frontend/src/lib/api.ts
Normal file
82
apps/frontend/src/lib/api.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
export type Quote = {
|
||||
id: number;
|
||||
timeStamp: string;
|
||||
buy: string;
|
||||
sell: string;
|
||||
type: "BELO" | "BLUE" | "BNA";
|
||||
difference: string;
|
||||
percentage: string;
|
||||
previousDayDifference: string;
|
||||
previousDayPercentage: string;
|
||||
};
|
||||
|
||||
export type FetchResult = {
|
||||
type: string;
|
||||
status: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export type HistoryQuote = {
|
||||
id: number;
|
||||
timeStamp: string;
|
||||
buy: string;
|
||||
sell: string;
|
||||
type: "BELO" | "BLUE" | "BNA";
|
||||
};
|
||||
|
||||
export type DailyMinMax = {
|
||||
date: string;
|
||||
minBuy: number;
|
||||
maxBuy: number;
|
||||
minSell: number;
|
||||
maxSell: number;
|
||||
};
|
||||
|
||||
export type DailyQuote = {
|
||||
date: string;
|
||||
buy: number;
|
||||
sell: number;
|
||||
};
|
||||
|
||||
async function fetcher<T>(url: string): Promise<T> {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(`API error: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export function getQuotes(): Promise<Quote[]> {
|
||||
return fetcher<Quote[]>("/api/quotes");
|
||||
}
|
||||
|
||||
export function fetchQuotes(): Promise<FetchResult[]> {
|
||||
return fetcher<FetchResult[]>("/api/quotes/fetch");
|
||||
}
|
||||
|
||||
export function getQuoteHistory(
|
||||
type: string,
|
||||
startDate: string,
|
||||
endDate: string,
|
||||
): Promise<HistoryQuote[]> {
|
||||
const params = new URLSearchParams({ startDate, endDate });
|
||||
return fetcher<HistoryQuote[]>(`/api/quotes/${type}/history?${params}`);
|
||||
}
|
||||
|
||||
export function getDailyMinMax(
|
||||
type: string,
|
||||
startDate: string,
|
||||
endDate: string,
|
||||
): Promise<DailyMinMax[]> {
|
||||
const params = new URLSearchParams({ startDate, endDate });
|
||||
return fetcher<DailyMinMax[]>(`/api/quotes/${type}/min-max?${params}`);
|
||||
}
|
||||
|
||||
export function getDailyQuotes(
|
||||
type: string,
|
||||
startDate: string,
|
||||
endDate: string,
|
||||
): Promise<DailyQuote[]> {
|
||||
const params = new URLSearchParams({ startDate, endDate });
|
||||
return fetcher<DailyQuote[]>(`/api/quotes/${type}/daily?${params}`);
|
||||
}
|
||||
Reference in New Issue
Block a user