import { isAxiosError } from "axios"; import { API_ENDPOINTS } from "../config/api"; import { apiClient, withAuth } from "./client"; export interface Gym { id: string; name: string; location?: string; } export const gymsApi = { getGyms: async (token: string | null): Promise => { try { const response = await apiClient.get( API_ENDPOINTS.GYMS, withAuth(token), ); const payload = response.data; if (Array.isArray(payload)) { return payload; } if (payload && Array.isArray(payload.data)) { return payload.data; } return []; } catch (error) { if (isAxiosError(error) && error.response) { throw new Error(`Failed to fetch gyms: ${error.response.status}`); } throw new Error("Failed to fetch gyms"); } }, updateUserGym: async ( gymId: string | null, token: string | null, ): Promise => { try { await apiClient.patch( API_ENDPOINTS.USERS.GYM, { gymId }, withAuth(token), ); } catch (error) { if (isAxiosError(error) && error.response) { throw new Error( `Failed to update gym selection: ${error.response.status}`, ); } throw new Error("Failed to update gym selection"); } }, };