51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import type { TrumpPost, Candle, BotTrade, BotPerformance } from '@/types'
|
|
|
|
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'
|
|
|
|
async function fetchJson<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const res = await fetch(`${BASE_URL}/api${path}`, {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
...init,
|
|
})
|
|
if (!res.ok) throw new Error(`API error ${res.status}: ${path}`)
|
|
return res.json() as Promise<T>
|
|
}
|
|
|
|
export async function getPosts(limit = 20, page = 1): Promise<TrumpPost[]> {
|
|
return fetchJson<TrumpPost[]>(`/posts?limit=${limit}&page=${page}`)
|
|
}
|
|
|
|
export async function getPost(id: number): Promise<TrumpPost> {
|
|
return fetchJson<TrumpPost>(`/posts/${id}`)
|
|
}
|
|
|
|
export async function getPrices(asset: 'BTC' | 'ETH', tf: string): Promise<Candle[]> {
|
|
return fetchJson<Candle[]>(`/prices/${asset}?tf=${tf}`)
|
|
}
|
|
|
|
export async function getTrades(limit = 20, page = 1): Promise<BotTrade[]> {
|
|
return fetchJson<BotTrade[]>(`/trades?limit=${limit}&page=${page}`)
|
|
}
|
|
|
|
export async function getPerformance(): Promise<BotPerformance> {
|
|
return fetchJson<BotPerformance>(`/performance`)
|
|
}
|
|
|
|
export async function subscribe(
|
|
wallet: string,
|
|
signature: string,
|
|
): Promise<{ success: boolean; message: string }> {
|
|
return fetchJson<{ success: boolean; message: string }>(`/subscribe`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ wallet, signature }),
|
|
})
|
|
}
|
|
|
|
export async function getUser(
|
|
wallet: string,
|
|
): Promise<{ wallet_address: string; active: boolean; subscribed_at: string | null; hl_api_key_set: boolean; trades: BotTrade[] }> {
|
|
return fetchJson<{ wallet_address: string; active: boolean; subscribed_at: string | null; hl_api_key_set: boolean; trades: BotTrade[] }>(
|
|
`/user/${wallet}`,
|
|
)
|
|
}
|