/** * User service for API calls related to user management */ import api from './api-wrapper.ts'; import type { UserRegisterRequest, UserRegisterResponse, UserUpdateNameRequest, UserUpdateNameResponse, User, } from '../types/user.types.ts'; import { API_ENDPOINTS } from '../constants/index.ts'; import { getUserId } from '../utils/index.ts'; /** * Register a new user or get existing user by unique_id */ export async function registerUser( request: UserRegisterRequest ): Promise { return api.post( API_ENDPOINTS.USER_REGISTER, request ); } /** * Get current user by user_id (requires X-User-ID header) */ export async function getCurrentUser(): Promise { const userId = getUserId(); if (!userId) { throw new Error('User ID not found in localStorage'); } return api.get(API_ENDPOINTS.USER_ME, { headers: { 'X-User-ID': userId, }, }); } /** * Get user by unique_id */ export async function getUserByUniqueId( uniqueId: string ): Promise { return api.get( `${API_ENDPOINTS.USER_BY_UNIQUE_ID}?unique_id=${encodeURIComponent(uniqueId)}` ); } /** * Update user's display name */ export async function updateUserName( request: UserUpdateNameRequest ): Promise { const userId = getUserId(); if (!userId) { throw new Error('User ID not found in localStorage'); } return api.patch( API_ENDPOINTS.USER_UPDATE_NAME, request, { headers: { 'X-User-ID': userId, }, } ); }