diff options
Diffstat (limited to 'src/services/api.ts')
| -rw-r--r-- | src/services/api.ts | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/src/services/api.ts b/src/services/api.ts new file mode 100644 index 0000000..edd376e --- /dev/null +++ b/src/services/api.ts @@ -0,0 +1,165 @@ +import { Platform } from 'react-native'; +import type { DashboardData, OneTimeExpense, RecurringExpense, User } from '@/types'; + +export type LoginPayload = any; +export type RegisterPayload = any; + +const BASE_URL = 'http://64.181.172.51:3333/api/v1' + +let currentToken: string | null = null; + +export function setApiToken(token: string | null) { + currentToken = token; +} + +async function fetcher<T>(endpoint: string, options?: RequestInit): Promise<T> { + const url = `${BASE_URL}${endpoint}`; + try { + const headers: Record<string, string> = {}; + if (options?.body) { + headers['Content-Type'] = 'application/json'; + } + if (currentToken) { + headers['Authorization'] = `Bearer ${currentToken}`; + } + + const response = await fetch(url, { + ...options, + headers: { + ...headers, + ...options?.headers, + }, + }); + + if (!response.ok) { + const errorText = await response.text(); + console.error(`API Error (${response.status}):`, errorText); + throw new Error(`API Error: ${response.status}`); + } + + const data = await response.json(); + return data; + } catch (error) { + console.error(`Fetch Error on ${endpoint}:`, error); + throw error; + } +} + +export async function apiLogin(data: any): Promise<{ user: User; token: string }> { + const result = await fetcher<{ data: { user: User; token: string } }>('/auth/login', { + method: 'POST', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function apiRegister(data: any): Promise<{ user: User; token: string }> { + const result = await fetcher<{ data: { user: User; token: string } }>('/auth/register', { + method: 'POST', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function getDashboard(userId: number, month?: number, year?: number): Promise<DashboardData> { + const params = new URLSearchParams(); + if (month) params.append('month', month.toString()); + if (year) params.append('year', year.toString()); + const qs = params.toString() ? `?${params.toString()}` : ''; + + const result = await fetcher<{ data: DashboardData }>(`/users/${userId}/dashboard${qs}`); + return result.data; +} + +export async function updateUserSalary( + userId: number, + data: { gross_salary: number; net_salary_override: number | null }, +): Promise<User> { + const result = await fetcher<{ data: User }>(`/users/${userId}`, { + method: 'PUT', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function getRecurringExpenses(userId: number): Promise<RecurringExpense[]> { + const result = await fetcher<{ data: RecurringExpense[] }>(`/users/${userId}/recurring-expenses`); + return result.data; +} + +export async function createRecurringExpense( + userId: number, + data: Omit<RecurringExpense, 'id' | 'user_id' | 'created_at' | 'updated_at'>, +): Promise<RecurringExpense> { + const result = await fetcher<{ data: RecurringExpense }>(`/users/${userId}/recurring-expenses`, { + method: 'POST', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function toggleRecurringExpense( + userId: number, + expenseId: number, +): Promise<RecurringExpense> { + const result = await fetcher<{ data: RecurringExpense }>(`/users/${userId}/recurring-expenses/${expenseId}/toggle`, { + method: 'PATCH', + }); + return result.data; +} + +export async function updateRecurringExpense( + userId: number, + expenseId: number, + data: Partial<Omit<RecurringExpense, 'id' | 'user_id' | 'created_at' | 'updated_at'>>, +): Promise<RecurringExpense> { + const result = await fetcher<{ data: RecurringExpense }>(`/users/${userId}/recurring-expenses/${expenseId}`, { + method: 'PUT', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function deleteRecurringExpense(userId: number, expenseId: number): Promise<void> { + await fetcher(`/users/${userId}/recurring-expenses/${expenseId}`, { + method: 'DELETE', + }); +} + +export async function getOneTimeExpenses(userId: number, month?: number, year?: number): Promise<OneTimeExpense[]> { + const params = new URLSearchParams(); + if (month) params.append('month', month.toString()); + if (year) params.append('year', year.toString()); + const qs = params.toString() ? `?${params.toString()}` : ''; + const result = await fetcher<{ data: OneTimeExpense[] }>(`/users/${userId}/one-time-expenses${qs}`); + return result.data; +} + +export async function createOneTimeExpense( + userId: number, + data: Omit<OneTimeExpense, 'id' | 'user_id' | 'created_at' | 'updated_at'>, +): Promise<OneTimeExpense> { + const result = await fetcher<{ data: OneTimeExpense }>(`/users/${userId}/one-time-expenses`, { + method: 'POST', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function updateOneTimeExpense( + userId: number, + expenseId: number, + data: Partial<Omit<OneTimeExpense, 'id' | 'user_id' | 'created_at' | 'updated_at'>>, +): Promise<OneTimeExpense> { + const result = await fetcher<{ data: OneTimeExpense }>(`/users/${userId}/one-time-expenses/${expenseId}`, { + method: 'PUT', + body: JSON.stringify(data), + }); + return result.data; +} + +export async function deleteOneTimeExpense(userId: number, expenseId: number): Promise<void> { + await fetcher(`/users/${userId}/one-time-expenses/${expenseId}`, { + method: 'DELETE', + }); +} |