Refactor API layer into structured domain-specific modules
- Created new api/ directory with domain-specific API modules: - api/client.ts: Base API client with error handling - api/auth.ts: Authentication endpoints - api/exchange.ts: Exchange/price endpoints - api/trades.ts: User trade endpoints - api/profile.ts: Profile management endpoints - api/invites.ts: Invite endpoints - api/admin.ts: Admin endpoints - api/index.ts: Centralized exports - Migrated all API calls from ad-hoc api.get/post/put to typed domain APIs - Updated all imports across codebase - Fixed test mocks to use new API structure - Fixed type issues in validation utilities - Removed old api.ts file Benefits: - Type-safe endpoints (no more string typos) - Centralized API surface (easy to discover endpoints) - Better organization (domain-specific modules) - Uses generated OpenAPI types automatically
This commit is contained in:
parent
6d0f125536
commit
a6fa6a8012
24 changed files with 529 additions and 255 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { useEffect, useState, useCallback, CSSProperties } from "react";
|
||||
import { Permission } from "../../auth-context";
|
||||
import { api } from "../../api";
|
||||
import { adminApi } from "../../api";
|
||||
import { Header } from "../../components/Header";
|
||||
import { SatsDisplay } from "../../components/SatsDisplay";
|
||||
import { useRequireAuth } from "../../hooks/useRequireAuth";
|
||||
|
|
@ -47,7 +47,7 @@ export default function AdminTradesPage() {
|
|||
|
||||
const fetchUpcomingTrades = useCallback(async (): Promise<string | null> => {
|
||||
try {
|
||||
const data = await api.get<AdminExchangeResponse[]>("/api/admin/trades/upcoming");
|
||||
const data = await adminApi.getUpcomingTrades();
|
||||
setUpcomingTrades(data);
|
||||
return null;
|
||||
} catch (err) {
|
||||
|
|
@ -58,21 +58,17 @@ export default function AdminTradesPage() {
|
|||
|
||||
const fetchPastTrades = useCallback(async (): Promise<string | null> => {
|
||||
try {
|
||||
let url = "/api/admin/trades/past";
|
||||
const params = new URLSearchParams();
|
||||
|
||||
const params: { status?: string; user_search?: string } = {};
|
||||
if (statusFilter !== "all") {
|
||||
params.append("status", statusFilter);
|
||||
params.status = statusFilter;
|
||||
}
|
||||
if (userSearch.trim()) {
|
||||
params.append("user_search", userSearch.trim());
|
||||
params.user_search = userSearch.trim();
|
||||
}
|
||||
|
||||
if (params.toString()) {
|
||||
url += `?${params.toString()}`;
|
||||
}
|
||||
|
||||
const data = await api.get<AdminExchangeResponse[]>(url);
|
||||
const data = await adminApi.getPastTrades(
|
||||
Object.keys(params).length > 0 ? params : undefined
|
||||
);
|
||||
setPastTrades(data);
|
||||
return null;
|
||||
} catch (err) {
|
||||
|
|
@ -105,12 +101,13 @@ export default function AdminTradesPage() {
|
|||
setError(null);
|
||||
|
||||
try {
|
||||
const endpoint =
|
||||
action === "no_show"
|
||||
? `/api/admin/trades/${publicId}/no-show`
|
||||
: `/api/admin/trades/${publicId}/${action}`;
|
||||
|
||||
await api.post<AdminExchangeResponse>(endpoint, {});
|
||||
if (action === "complete") {
|
||||
await adminApi.completeTrade(publicId);
|
||||
} else if (action === "no_show") {
|
||||
await adminApi.noShowTrade(publicId);
|
||||
} else if (action === "cancel") {
|
||||
await adminApi.cancelTrade(publicId);
|
||||
}
|
||||
// Refetch trades - errors from fetch are informational, not critical
|
||||
const [upcomingErr, pastErr] = await Promise.all([fetchUpcomingTrades(), fetchPastTrades()]);
|
||||
const fetchErrors = [upcomingErr, pastErr].filter(Boolean);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue