-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (44 loc) · 1.05 KB
/
app.js
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
const appId = 'ZijPZapn481OKArMG39M';
const baseUrl = `https://us-central1-bookstore-api-e63c8.cloudfunctions.net/bookstoreApi/apps/${appId}/books`;
const bookString = (book) => JSON.stringify({
item_id: book.id,
title: book.title,
author: book.author,
category: book.category,
});
export const getBooksAPI = async () => {
try {
const result = await fetch(baseUrl);
const data = await result.json();
return data;
} catch (error) {
return error;
}
};
export const addBookAPI = async (book) => {
try {
await fetch(baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: bookString(book),
});
return true;
} catch (error) {
return false;
}
};
export const removeBookAPI = async (id) => {
const url = `${baseUrl}/${id}`;
try {
const result = await fetch(url, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ item_id: id }),
});
return result;
} catch (error) {
return error;
}
};