-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
61 lines (51 loc) · 1.3 KB
/
api.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const api_link = 'https://thinkful-list-api.herokuapp.com/keithFreitag/bookmarks';
//hoping spread syntax is correct...
//anyway, this uses fetch() to grab items from the api
function fetchDatBoi(...some_param){
let error;
return fetch(...some_param)
.then(res => {
if (!res.ok) error.code = res.status;
if (!res.headers.get('content-type').includes('json')) {
error.message = res.statusText;
return Promise.reject(error);
}
return res.json();
})
.then(stuff => {
if (error) {
error.message = stuff.message;
}
return stuff;
});
}
function getItems(){
return fetchDatBoi(api_link);
}
function addItem(newItem){
return fetchDatBoi(api_link, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: newItem
});
}
function editItem(id, stuff){
return fetchDatBoi(api_link + '/' + id, {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: stuff
});
}
function deleteItem(id){
return fetchDatBoi(api_link + '/' + id, {
method: 'DELETE'
});
}
//I don't know why but you need to refresh the page for the delete to go through.
export default {
getItems,
addItem,
editItem,
deleteItem
};