Spaces:
Running
Running
| const API_BASE_URL = 'https://ios-ioe-website-backend.hf.space/api'; | |
| export async function fetchWithAuth(endpoint: string, options: RequestInit = {}) { | |
| const adminKey = typeof window !== 'undefined' ? localStorage.getItem('admin_key') : null; | |
| const headers = { | |
| 'Content-Type': 'application/json', | |
| ...(adminKey ? { 'X-Admin-Key': adminKey } : {}), | |
| ...options.headers, | |
| }; | |
| const response = await fetch(`${API_BASE_URL}${endpoint}`, { | |
| ...options, | |
| headers, | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json().catch(() => ({ detail: 'An error occurred' })); | |
| throw new Error(error.detail || 'Request failed'); | |
| } | |
| return response.json(); | |
| } | |
| export async function uploadFile(file: File): Promise<{ url: string }> { | |
| const adminKey = typeof window !== 'undefined' ? localStorage.getItem('admin_key') : null; | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| const response = await fetch(`${API_BASE_URL}/v1/upload/image`, { | |
| method: 'POST', | |
| headers: { | |
| ...(adminKey ? { 'X-Admin-Key': adminKey } : {}), | |
| }, | |
| body: formData, | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json().catch(() => ({ detail: 'Upload failed' })); | |
| throw new Error(error.detail || 'Upload failed'); | |
| } | |
| return response.json(); | |
| } | |
| export const api = { | |
| projects: { | |
| list: () => fetchWithAuth('/v1/projects'), | |
| create: (data: any) => fetchWithAuth('/v1/projects', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/projects/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/projects/${id}`, { method: 'DELETE' }), | |
| }, | |
| research: { | |
| list: () => fetchWithAuth('/v1/research'), | |
| create: (data: any) => fetchWithAuth('/v1/research', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/research/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/research/${id}`, { method: 'DELETE' }), | |
| }, | |
| events: { | |
| list: () => fetchWithAuth('/v1/events'), | |
| create: (data: any) => fetchWithAuth('/v1/events', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/events/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/events/${id}`, { method: 'DELETE' }), | |
| }, | |
| edu: { | |
| list: () => fetchWithAuth('/v1/edu'), | |
| create: (data: any) => fetchWithAuth('/v1/edu', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/edu/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/edu/${id}`, { method: 'DELETE' }), | |
| }, | |
| blogs: { | |
| list: () => fetchWithAuth('/v1/blogs'), | |
| create: (data: any) => fetchWithAuth('/v1/blogs', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/blogs/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/blogs/${id}`, { method: 'DELETE' }), | |
| }, | |
| teams: { | |
| list: () => fetchWithAuth('/v1/teams'), | |
| create: (data: any) => fetchWithAuth('/v1/teams', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/teams/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/teams/${id}`, { method: 'DELETE' }), | |
| }, | |
| reviews: { | |
| list: () => fetchWithAuth('/v1/reviews'), | |
| create: (data: any) => fetchWithAuth('/v1/reviews', { method: 'POST', body: JSON.stringify(data) }), | |
| update: (id: string, data: any) => fetchWithAuth(`/v1/reviews/${id}`, { method: 'PUT', body: JSON.stringify(data) }), | |
| delete: (id: string) => fetchWithAuth(`/v1/reviews/${id}`, { method: 'DELETE' }), | |
| }, | |
| auth: { | |
| verify: (key: string) => fetchWithAuth('/v1/verify-key', { headers: { 'X-Admin-Key': key } }), | |
| }, | |
| upload: { | |
| image: (file: File) => uploadFile(file), | |
| }, | |
| }; | |