-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
41 lines (31 loc) · 785 Bytes
/
bin.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
const axios = require('axios')
const create = async () => {
const bin = await axios.post(
'https://api.jsonbin.io/b',
JSON.stringify({ items: [] }),
{
headers: {
'content-type': 'application/json'
}
}
)
return bin.data
}
const get = async id => {
const bin = await axios.get(`https://api.jsonbin.io/b/${id}/latest`)
return bin.data
}
const update = async (id, data) => {
const bin = await get(id)
const { items } = bin
items.unshift(data)
items.length = items.length > 100 ? 100 : items.length
const updatedBin = {
items
}
axios.put(`https://api.jsonbin.io/b/${id}`, JSON.stringify(updatedBin), {
headers: { 'Content-Type': 'application/json' }
})
return true
}
module.exports = { create, get, update }