diff options
author | 2025-04-24 09:13:07 +0530 | |
---|---|---|
committer | 2025-04-24 09:13:07 +0530 | |
commit | caace928ac81c284629ee50942d72179d4da9784 (patch) | |
tree | b2f4e87b7a53e30ac5ac9af94cdc70c2da5bbfb9 /frontend/src/lib/api.ts | |
parent | 50d5e6534f5e593297a09323e683c7c8b850117b (diff) | |
download | finance-caace928ac81c284629ee50942d72179d4da9784.tar.gz finance-caace928ac81c284629ee50942d72179d4da9784.tar.bz2 finance-caace928ac81c284629ee50942d72179d4da9784.zip |
feat: Fix loan API type assertion and complete core loan features
- Resolve interface conversion panic in loan handlers by correcting
user type assertions from *models.User to models.User
- Finalize loan management API integration with frontend components
- Implement remaining loan calculation logic and CRUD operations
- Connect loan display components to backend APIs as per Phase 3
- Update project status in README.md to reflect completed loan features
- Add CORS middleware configuration for frontend-backend communication
This commit completes core loan management functionality and fixes critical
type safety issues in the API handlers, enabling proper user context handling.
Diffstat (limited to 'frontend/src/lib/api.ts')
-rw-r--r-- | frontend/src/lib/api.ts | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts new file mode 100644 index 0000000..35ef8d6 --- /dev/null +++ b/frontend/src/lib/api.ts @@ -0,0 +1,123 @@ +// API base URL +const API_BASE_URL = 'http://localhost:8080/api/v1'; + +// Helper function for fetching data with authorization +async function fetchWithAuth(url: string, options: RequestInit = {}) { + // Get token from local storage + const token = localStorage.getItem('token'); + + // Set up headers with authorization + const headers = { + 'Content-Type': 'application/json', + ...(token ? { 'Authorization': `Bearer ${token}` } : {}), + ...options.headers + }; + + // Perform fetch + const response = await fetch(`${API_BASE_URL}${url}`, { + ...options, + headers + }); + + // Handle unauthorized responses + if (response.status === 401) { + localStorage.removeItem('token'); + window.location.href = '/login'; + throw new Error('Unauthorized'); + } + + // Parse response + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.error || `API Error: ${response.status}`); + } + + return response.json(); +} + +// Auth API +export const authApi = { + login: async (email: string, password: string) => { + const response = await fetch(`${API_BASE_URL}/auth/login`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }) + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.error || 'Login failed'); + } + + const data = await response.json(); + localStorage.setItem('token', data.token); + return data; + }, + + signup: async (name: string, email: string, password: string) => { + const response = await fetch(`${API_BASE_URL}/auth/signup`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ name, email, password }) + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.error || 'Signup failed'); + } + + return response.json(); + }, + + logout: () => { + localStorage.removeItem('token'); + window.location.href = '/login'; + } +}; + +// Loan API +export interface Loan { + ID: number; + CreatedAt: string; + UpdatedAt: string; + DeletedAt: string | null; + UserID: number; + AccountID: number | null; + Name: string; + OriginalAmount: number; + CurrentBalance: number; + InterestRate: number; + StartDate: string; + EndDate: string; +} + +export interface LoanInput { + name: string; + originalAmount: number; + currentBalance: number; + interestRate: number; + startDate: string; + endDate: string; + accountId?: number; +} + +export const loanApi = { + getLoans: () => fetchWithAuth('/loans'), + getLoan: (id: number) => fetchWithAuth(`/loans/${id}`), + createLoan: (loan: LoanInput) => fetchWithAuth('/loans', { + method: 'POST', + body: JSON.stringify(loan) + }), + updateLoan: (id: number, loan: Partial<LoanInput>) => fetchWithAuth(`/loans/${id}`, { + method: 'PUT', + body: JSON.stringify(loan) + }), + deleteLoan: (id: number) => fetchWithAuth(`/loans/${id}`, { + method: 'DELETE' + }) +}; + +// User API +export const userApi = { + getProfile: () => fetchWithAuth('/users/me') +};
\ No newline at end of file |