-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIs.js
30 lines (30 loc) · 798 Bytes
/
APIs.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
const apis = {
localStorage: {
read: function(name) {
return JSON.parse(localStorage.getItem(name));
},
write: function(name, data) {
localStorage.setItem(name, JSON.stringify(data));
}
},
remoteApi: {
getData: function(name) {
return fetch(`https://jsonplaceholder.typicode.com/${name}`)
},
read: async function(name) {
const fetched = await this.getData(name)
const data = await fetched.json();
return data;
},
write: function(name, data) {
fetch(`https://jsonplaceholder.typicode.com/${name}`, {
method: 'POST',
body: JSON.stringify(data),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.json())
}
}
}