fitaiProto/apps/mobile/src/services/apiService.ts
echo 24ed0b6190 pedometar
problem with expogo, should be sorted before build
2025-11-28 18:08:52 +01:00

75 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 };
}
},
};