client: auth context and API layer
Add JWT-based AuthContext (login/logout, token persistence), typed HTTP client with auth header injection, REST API functions for all game endpoints, and shared TypeScript types. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { request } from './http';
|
||||
import type {
|
||||
ActiveMission,
|
||||
AuthResponse,
|
||||
City,
|
||||
ClaimMissionResponse,
|
||||
InventoryResponse,
|
||||
LeaderboardResponse,
|
||||
MarketResponse,
|
||||
Mission,
|
||||
PlayerMeResponse,
|
||||
StartMissionResponse,
|
||||
TradeResponse,
|
||||
TravelResponse,
|
||||
WorldEvent,
|
||||
} from './types';
|
||||
|
||||
export const api = {
|
||||
// Auth
|
||||
register: (email: string, password: string, displayName: string) =>
|
||||
request<AuthResponse>('/auth/register', {
|
||||
method: 'POST',
|
||||
body: { email, password, displayName },
|
||||
}),
|
||||
login: (email: string, password: string) =>
|
||||
request<AuthResponse>('/auth/login', { method: 'POST', body: { email, password } }),
|
||||
|
||||
// Player
|
||||
playerMe: () => request<PlayerMeResponse>('/player/me'),
|
||||
travel: (cityId: string) =>
|
||||
request<TravelResponse>('/player/travel', { method: 'POST', body: { cityId } }),
|
||||
|
||||
// Cities
|
||||
cities: () => request<{ cities: City[] }>('/cities'),
|
||||
|
||||
// Market
|
||||
market: () => request<MarketResponse>('/market/current'),
|
||||
buy: (itemId: string, quantity: number) =>
|
||||
request<TradeResponse>('/market/buy', { method: 'POST', body: { itemId, quantity } }),
|
||||
sell: (itemId: string, quantity: number) =>
|
||||
request<TradeResponse>('/market/sell', { method: 'POST', body: { itemId, quantity } }),
|
||||
|
||||
// Inventory
|
||||
inventory: () => request<InventoryResponse>('/inventory'),
|
||||
|
||||
// Missions
|
||||
missionsAvailable: () => request<{ missions: Mission[] }>('/missions/available'),
|
||||
missionsActive: () => request<{ missions: ActiveMission[] }>('/missions/active'),
|
||||
startMission: (missionId: string) =>
|
||||
request<StartMissionResponse>(`/missions/${missionId}/start`, { method: 'POST' }),
|
||||
claimMission: (playerMissionId: string) =>
|
||||
request<ClaimMissionResponse>(`/missions/${playerMissionId}/claim`, { method: 'POST' }),
|
||||
|
||||
// Events
|
||||
events: () => request<{ events: WorldEvent[] }>('/events/current'),
|
||||
|
||||
// Leaderboard
|
||||
leaderboard: (metric: 'money' | 'reputation', limit = 20) =>
|
||||
request<LeaderboardResponse>(`/leaderboard/${metric}?limit=${limit}`),
|
||||
};
|
||||
Reference in New Issue
Block a user