File size: 4,116 Bytes
c63c36c
 
 
 
faf9095
c63c36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c58940d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c63c36c
 
f65bb7f
 
c63c36c
 
 
6e0c0e7
f65bb7f
 
6e0c0e7
 
 
 
f65bb7f
 
6e0c0e7
 
 
 
f65bb7f
 
6e0c0e7
 
 
f65bb7f
 
 
 
 
 
6e0c0e7
f65bb7f
 
6e0c0e7
 
 
 
f65bb7f
 
6e0c0e7
 
c63c36c
faf9095
 
 
c58940d
 
 
c63c36c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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),
  },
};