Skip to content

Commit f9dfdaa

Browse files
authored
Update api.js
1 parent a441495 commit f9dfdaa

1 file changed

Lines changed: 33 additions & 35 deletions

File tree

  • scode-cms-frontend/src/api

‎scode-cms-frontend/src/api/api.js‎

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
// src/api/api.js
2-
const BASE_URL = "http://localhost:5000/api"; // backend URL
1+
import { API_BASE_URL } from "../config.js";
32

43
// Helper function for GET requests
54
export const getRequest = async (endpoint, token) => {
6-
const res = await fetch(`${BASE_URL}${endpoint}`, {
7-
headers: {
8-
"Content-Type": "application/json",
9-
...(token && { Authorization: `Bearer ${token}` })
10-
}
11-
});
12-
return res.json();
5+
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
6+
headers: {
7+
"Content-Type": "application/json",
8+
...(token && { Authorization: `Bearer ${token}` })
9+
}
10+
});
11+
return res.json();
1312
};
1413

1514
// Helper function for POST requests (JSON)
1615
export const postRequest = async (endpoint, data, token) => {
17-
const res = await fetch(`${BASE_URL}${endpoint}`, {
18-
method: "POST",
19-
headers: {
20-
"Content-Type": "application/json",
21-
...(token && { Authorization: `Bearer ${token}` })
22-
},
23-
body: JSON.stringify(data)
24-
});
25-
return res.json();
16+
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
17+
method: "POST",
18+
headers: {
19+
"Content-Type": "application/json",
20+
...(token && { Authorization: `Bearer ${token}` })
21+
},
22+
body: JSON.stringify(data)
23+
});
24+
return res.json();
2625
};
2726

2827
// Helper function for POST requests (FormData / file uploads)
2928
export const postFormRequest = async (endpoint, formData, token) => {
30-
const res = await fetch(`${BASE_URL}${endpoint}`, {
31-
method: "POST",
32-
headers: {
33-
...(token && { Authorization: `Bearer ${token}` })
34-
},
35-
body: formData
36-
});
37-
return res.json();
29+
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
30+
method: "POST",
31+
headers: {
32+
...(token && { Authorization: `Bearer ${token}` }) // no content-type, browser sets it automatically
33+
},
34+
body: formData
35+
});
36+
return res.json();
3837
};
3938

4039
// ===== AUTH =====
@@ -47,14 +46,13 @@ export const getPost = (id, token) => getRequest(`/posts/${id}`, token);
4746

4847
// Create post (supports file upload)
4948
export const createPost = (data, token) => {
50-
// If data includes a file, use FormData
51-
if (data.file) {
52-
const formData = new FormData();
53-
for (const key in data) {
54-
formData.append(key, data[key]);
55-
}
56-
return postFormRequest("/posts", formData, token);
57-
} else {
58-
return postRequest("/posts", data, token);
49+
if (data.file) {
50+
const formData = new FormData();
51+
for (const key in data) {
52+
formData.append(key, data[key]);
5953
}
54+
return postFormRequest("/posts", formData, token);
55+
} else {
56+
return postRequest("/posts", data, token);
57+
}
6058
};

0 commit comments

Comments
 (0)