-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello.js
57 lines (52 loc) · 1.32 KB
/
trello.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
const alfy = require('alfy')
const { userPermissionToken, developerKey, boardId } = require('./secrets')
const Trello = require('node-trello')
const t = new Trello(developerKey, userPermissionToken)
const iconFor = require('./icon-for')
function assignUid (resultType) {
return function (element) {
element.uid = `${resultType}/${element.id}`
return element
}
}
function get (path, resultsType) {
const refreshCache = new Promise((resolve, reject) => {
t.get(path, (err, results) => {
if (err) {
reject(err)
} else {
results = results.map(assignUid(resultsType))
alfy.cache.set(path, results)
resolve(results)
}
})
})
const cached = alfy.cache.get(path)
if (cached) return Promise.resolve(cached)
return refreshCache
}
function post (path, args) {
return new Promise((resolve, reject) => {
t.post(path, args, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}
module.exports = {
getCards (listId) {
return get(`/1/lists/${listId}/cards`, 'card')
},
getLists () {
return get(`/1/boards/${boardId}/lists`, 'list').then((lists) => {
lists.forEach(list => list.icon = iconFor(list.name))
return lists
})
},
postCard (card) {
return post(`/1/cards`, card)
}
}