standardize mobile api transport for gyms and core services
This commit is contained in:
1 parent
c36cad9c54
commit
80110acbf7
8 files changed
+322
-386
No files matched your search
@@ -0,0 +1,56 @@
|
||||
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<Gym[]> => {
|
||||
try {
|
||||
const response = await apiClient.get<Gym[] | { data?: Gym[] }>(
|
||||
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<void> => {
|
||||
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");
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user