import type { TapdConfig } from "../types.js"; import { DEFAULT_TAPD_API_BASE } from "./config.js"; export function apiUrl( config: TapdConfig, path: string, query?: Record, ): string { const base = (config.baseUrl ?? DEFAULT_TAPD_API_BASE).replace(/\/$/, ""); return base + path + (query ? `?${new URLSearchParams(query)}` : ""); } async function tapdRequest( method: "GET" | "POST" | "PUT", url: string, config: TapdConfig, body?: Record, signal?: AbortSignal, ): Promise { try { const response = await fetch(url, { method, headers: { Authorization: `Bearer ${config.token}`, "Content-Type": "application/json", }, body: body ? JSON.stringify(body) : undefined, signal, }); if (!response.ok) return null; const json = (await response.json()) as { status?: number }; return json.status === undefined || json.status === 1 ? (json as T) : null; } catch (error) { if (error instanceof Error && error.name !== "AbortError") { console.error(`TAPD ${method} error:`, error.message); } return null; } } export function tapdGet( url: string, config: TapdConfig, signal?: AbortSignal, ) { return tapdRequest("GET", url, config, undefined, signal); } export function tapdPost( url: string, config: TapdConfig, body: Record, ) { return tapdRequest("POST", url, config, body); } export function tapdPut( url: string, config: TapdConfig, body: Record, ) { return tapdRequest("PUT", url, config, body); }