75 lines
3.0 KiB
TypeScript
75 lines
3.0 KiB
TypeScript
import { API_BASE_URL, API_ENDPOINTS } from '../config/api';
|
||
|
||
export const apiClient = {
|
||
async getFitnessProfile(token?: string | null) {
|
||
try {
|
||
const headers: Record<string, string> = {
|
||
'Content-Type': 'application/json',
|
||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||
};
|
||
const res = await fetch(`${API_BASE_URL}${API_ENDPOINTS.PROFILE.FITNESS}`, { headers });
|
||
if (!res.ok) {
|
||
throw new Error('Failed to fetch fitness profile');
|
||
}
|
||
return await res.json();
|
||
} catch (e) {
|
||
console.warn('Using mock fitness profile due to fetch error', e);
|
||
// Mock data shape should match backend response
|
||
return { steps: 0, calories: 0, duration: 0 };
|
||
}
|
||
},
|
||
async getRecentActivities(token?: string | null) {
|
||
// Placeholder endpoint – adjust to match backend implementation
|
||
try {
|
||
const headers: Record<string, string> = {
|
||
'Content-Type': 'application/json',
|
||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||
};
|
||
const res = await fetch(`${API_BASE_URL}/api/fitness-goals/recent`, { headers });
|
||
if (!res.ok) {
|
||
throw new Error('Failed to fetch recent activities');
|
||
}
|
||
return await res.json();
|
||
} catch (e) {
|
||
console.warn('Using mock recent activities due to fetch error', e);
|
||
// Return empty array or sample data
|
||
return [];
|
||
}
|
||
},
|
||
|
||
async syncSteps(steps: number, token?: string | null) {
|
||
try {
|
||
const headers: Record<string, string> = {
|
||
'Content-Type': 'application/json',
|
||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||
};
|
||
const res = await fetch(`${API_BASE_URL}/api/activity/steps`, {
|
||
method: 'POST',
|
||
headers,
|
||
body: JSON.stringify({ steps, timestamp: new Date().toISOString() }),
|
||
});
|
||
if (!res.ok) throw new Error('Failed to sync steps');
|
||
return await res.json();
|
||
} catch (e) {
|
||
console.warn('Failed to sync steps', e);
|
||
throw e;
|
||
}
|
||
},
|
||
|
||
async getDailyActivity(date?: string, token?: string | null) {
|
||
try {
|
||
const headers: Record<string, string> = {
|
||
'Content-Type': 'application/json',
|
||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||
};
|
||
const dateParam = date || new Date().toISOString().split('T')[0];
|
||
const res = await fetch(`${API_BASE_URL}/api/activity/daily?date=${dateParam}`, { headers });
|
||
if (!res.ok) throw new Error('Failed to fetch daily activity');
|
||
return await res.json();
|
||
} catch (e) {
|
||
console.warn('Failed to fetch daily activity', e);
|
||
return { steps: 0, calories: 0, duration: 0 };
|
||
}
|
||
},
|
||
};
|