Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 363260f

Browse files
committed
feat: Update frontend code to match modified serverless API
1 parent 87f2506 commit 363260f

File tree

1 file changed

+69
-95
lines changed

1 file changed

+69
-95
lines changed

frontend/src/background.js

Lines changed: 69 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Constants = {
22
BACKEND_URL:
3-
"https://3bxsddbb222i3zghh77frbcyoa0pbwan.lambda-url.ap-northeast-2.on.aws",
3+
"https://3t4g2w8kcf.execute-api.ap-northeast-2.amazonaws.com/prod",
44
};
55

66
globalThis.browser = (function () {
@@ -20,7 +20,7 @@ function registUser() {
2020
"Content-Type": "application/json",
2121
},
2222
body: JSON.stringify({
23-
id: data.user_id,
23+
user_id: data.user_id,
2424
email: data.user_email,
2525
}),
2626
});
@@ -127,20 +127,16 @@ async function onUpdated(tabId) {
127127
.catch((err) => console.log(err));
128128
}
129129
// 검색 페이지에 쿼리가 들어가 있다면
130-
/*
131130
if (
132131
tab.status == "complete" &&
133132
/^http/.test(tab.url) &&
134-
tab.url != "https://velog.io/search" &&
135-
tab.url.slice(0, 26) === "https://velog.io/search?q="
133+
tab.url.slice(0, 23) === "https://velog.io/search"
136134
) {
137-
console.log("stert");
138135
browser.scripting.executeScript({
139136
target: { tabId: tab.id },
140137
files: ["./src/util.js", "./src/search.js"],
141138
});
142139
}
143-
*/
144140
}
145141

146142
browser.tabs.onUpdated.addListener(async (tabID, changeInfo, tab) => {
@@ -156,58 +152,60 @@ browser.tabs.onReplaced.addListener(async (addedTabId, removedTabId) => {
156152
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
157153
if (request.message === "add_bookmark") {
158154
browser.storage.local.get(["user_id", "user_email"], (data) => {
159-
fetch(
160-
`${Constants.BACKEND_URL}/${data.user_id}/blog?blog_id=${request.payload}`,
161-
{
162-
method: "POST",
163-
headers: {
164-
"Content-Type": "application/json",
165-
},
166-
}
167-
)
155+
fetch(`${Constants.BACKEND_URL}/user/${data.user_id}/blog`, {
156+
method: "POST",
157+
headers: {
158+
"Content-Type": "application/json",
159+
Accept: "application/json",
160+
},
161+
body: JSON.stringify({
162+
blog_id: request.payload,
163+
}),
164+
})
168165
.then((response) => {
169-
return response.json();
170-
})
171-
.then((data) => {
172-
sendResponse({ message: "success" });
173-
return;
166+
if (response.status === 204) {
167+
sendResponse({ message: "success" });
168+
return;
169+
}
174170
})
175171
.catch((err) => {
176172
console.log(err);
177173
});
178174
});
179-
return true;
180175
} else if (request.message === "delete_bookmark") {
181176
browser.storage.local.get(["user_id", "user_email"], (data) => {
182-
fetch(
183-
`${Constants.BACKEND_URL}/${data.user_id}/blog?blog_id=${request.payload}`,
184-
{
185-
method: "DELETE",
186-
headers: {
187-
"Content-Type": "application/json",
188-
},
189-
}
190-
)
177+
fetch(`${Constants.BACKEND_URL}/user/${data.user_id}/blog`, {
178+
method: "DELETE",
179+
headers: {
180+
"Content-Type": "application/json",
181+
Accept: "application/json",
182+
},
183+
body: JSON.stringify({
184+
blog_id: request.payload,
185+
}),
186+
})
191187
.then((response) => {
192-
return response.json();
193-
})
194-
.then((data) => {
195-
sendResponse({ message: "success" });
196-
return;
188+
if (response.status === 204) {
189+
sendResponse({ message: "success" });
190+
return;
191+
}
197192
})
198193
.catch((err) => {
199194
console.log(err);
200195
});
201196
});
202-
return true;
203197
} else if (request.message === "get_new_post") {
204198
browser.storage.local.get(["user_id", "user_email"], (data) => {
205-
fetch(`${Constants.BACKEND_URL}/${data.user_id}/archive`, {
206-
method: "GET",
207-
headers: {
208-
"Content-Type": "application/json",
209-
},
210-
})
199+
fetch(
200+
`${Constants.BACKEND_URL}/user/${data.user_id}/posts?skip=0&limit=15`,
201+
{
202+
method: "GET",
203+
headers: {
204+
"Content-Type": "application/json",
205+
Accept: "application/json",
206+
},
207+
}
208+
)
211209
.then((response) => {
212210
return response.json();
213211
})
@@ -219,39 +217,42 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
219217
console.log(err);
220218
});
221219
});
222-
return true;
223220
} else if (request.message === "is_bookmarked") {
224221
browser.storage.local.get(["user_id", "user_email"], (data) => {
225222
fetch(
226-
`${Constants.BACKEND_URL}/${data.user_id}/is_bookmarked?blog_id=${request.payload}`,
223+
`${Constants.BACKEND_URL}/user/${data.user_id}/blog/${request.payload}`,
227224
{
228225
method: "GET",
229226
headers: {
230227
"Content-Type": "application/json",
228+
Accept: "application/json",
231229
},
232230
}
233231
)
234232
.then((response) => {
235-
return response.json();
236-
})
237-
.then((data) => {
233+
if (response.status === 200) {
234+
sendResponse({
235+
message: "success",
236+
is_bookmarked: true,
237+
});
238+
return;
239+
}
238240
sendResponse({
239241
message: "success",
240-
is_bookmarked: data.is_bookmarked,
242+
is_bookmarked: false,
241243
});
242-
return;
243244
})
244245
.catch((err) => {
245246
console.log(err);
246247
});
247248
});
248-
return true;
249249
} else if (request.message === "get_blogs") {
250250
browser.storage.local.get(["user_id"], (data) => {
251-
fetch(`${Constants.BACKEND_URL}/${data.user_id}/blogs`, {
251+
fetch(`${Constants.BACKEND_URL}/user/${data.user_id}/blog/all`, {
252252
method: "GET",
253253
headers: {
254254
"Content-Type": "application/json",
255+
Accept: "application/json",
255256
},
256257
})
257258
.then((response) => {
@@ -260,30 +261,27 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
260261
.then((data) => {
261262
sendResponse({
262263
message: "success",
263-
blogs: data.blogs,
264+
blogs: data,
264265
});
265266
return;
266267
})
267268
.catch((err) => {
268269
console.log(err);
269270
});
270271
});
271-
return true;
272272
} else if (request.message === "veloghelper-change_email") {
273273
browser.storage.local.get(["user_id"], (data) => {
274-
fetch(
275-
`${Constants.BACKEND_URL}/${data.user_id}/email?email=${request.payload}`,
276-
{
277-
method: "Post",
278-
headers: {
279-
"Content-Type": "application/json",
280-
},
281-
}
282-
)
274+
fetch(`${Constants.BACKEND_URL}/user/${data.user_id}/email`, {
275+
method: "PUT",
276+
headers: {
277+
"Content-Type": "application/json",
278+
Accept: "application/json",
279+
},
280+
data: JSON.stringify({
281+
email: request.payload,
282+
}),
283+
})
283284
.then((response) => {
284-
return response.json();
285-
})
286-
.then((data) => {
287285
sendResponse({
288286
message: "success",
289287
});
@@ -293,13 +291,13 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
293291
console.log(err);
294292
});
295293
});
296-
return true;
297294
} else if (request.message === "veloghelper-get_email") {
298295
browser.storage.local.get(["user_id"], (data) => {
299-
fetch(`${Constants.BACKEND_URL}/user?user_id=${data.user_id}`, {
296+
fetch(`${Constants.BACKEND_URL}/user/${data.user_id}`, {
300297
method: "GET",
301298
headers: {
302299
"Content-Type": "application/json",
300+
Accept: "application/json",
303301
},
304302
})
305303
.then((response) => {
@@ -316,42 +314,18 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
316314
console.log(err);
317315
});
318316
});
319-
return true;
320-
} else if (request.message === "get_search_results") {
321-
fetch(`${Constants.BACKEND_URL}/search?query=${request.payload}`, {
322-
method: "GET",
323-
headers: {
324-
"Content-Type": "application/json",
325-
},
326-
})
327-
.then((response) => {
328-
return response.json();
329-
})
330-
.then((data) => {
331-
sendResponse({
332-
message: "success",
333-
total: data.total,
334-
results: data.results,
335-
});
336-
return;
337-
})
338-
.catch((err) => {
339-
console.log(err);
340-
});
341-
return true;
342317
} else if (request.message === "veloghelper-get_followers") {
343-
fetch(`${Constants.BACKEND_URL}/${request.payload}/followers`, {
318+
fetch(`${Constants.BACKEND_URL}/blog/${request.payload}/followers`, {
344319
method: "GET",
345320
headers: {
346321
"Content-Type": "application/json",
322+
Accept: "application/json",
347323
},
348324
})
349325
.then((response) => {
350-
console.log(response);
351326
return response.json();
352327
})
353328
.then((data) => {
354-
console.log(data);
355329
sendResponse({
356330
message: "success",
357331
followers: data,
@@ -361,6 +335,6 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
361335
.catch((err) => {
362336
console.log(err);
363337
});
364-
return true;
365338
}
339+
return true;
366340
});

0 commit comments

Comments
 (0)