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:
counterweight 2025-12-25 20:32:11 +01:00
parent 6d0f125536
commit a6fa6a8012
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
24 changed files with 529 additions and 255 deletions

View file

@ -2,7 +2,7 @@
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from "react";
import { api } from "./api";
import { authApi } from "./api";
import { components } from "./generated/api";
import { extractApiErrorMessage } from "./utils/error-handling";
@ -54,7 +54,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const checkAuth = async () => {
try {
const userData = await api.get<User>("/api/auth/me");
const userData = await authApi.getMe();
setUser(userData);
} catch {
// Not authenticated
@ -65,7 +65,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const login = async (email: string, password: string) => {
try {
const userData = await api.post<User>("/api/auth/login", { email, password });
const userData = await authApi.login(email, password);
setUser(userData);
} catch (err) {
throw new Error(extractApiErrorMessage(err, "Login failed"));
@ -74,11 +74,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const register = async (email: string, password: string, inviteIdentifier: string) => {
try {
const userData = await api.post<User>("/api/auth/register", {
email,
password,
invite_identifier: inviteIdentifier,
});
const userData = await authApi.register(email, password, inviteIdentifier);
setUser(userData);
} catch (err) {
throw new Error(extractApiErrorMessage(err, "Registration failed"));
@ -87,7 +83,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const logout = async () => {
try {
await api.post("/api/auth/logout");
await authApi.logout();
} catch {
// Ignore errors on logout
}