-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (152 loc) · 4.79 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const _ = require('lodash')
const got = require('got')
const routes = require('./routes')
const utils = require('./utils')
const validate = require('./validate')
function getPermissionsCard () {
return {
type: 'AskForPermissionsConsent',
permissions: [
'read::alexa:household:list',
'write::alexa:household:list'
]
}
}
function makeRequest (apiToken) {
return function _makeRequest (routeObj, body = {}) {
const options = _.assign({body}, routeObj.options, {
headers: {
Authorization: `Bearer ${apiToken}`
},
json: true
})
return got(routeObj.url, options)
}
}
function init (token) {
token = token || process.env.ALEXA_API_TOKEN
const makeRequestFn = makeRequest(token)
function createCustomList (listName) {
validate.listName(listName)
const body = {
name: listName,
state: 'active'
}
return makeRequestFn(routes.createCustomList(), body)
.then(utils.stripPropertyFromResponse())
}
function createNewListItem (listName, listItemName, status = 'active') {
validate.listItemName(listItemName)
return getList(listName, status).then(function (list) {
const body = {
value: listItemName,
status
}
return makeRequestFn(routes.createNewListItem(list.listId), body)
})
.then(utils.stripPropertyFromResponse())
}
function deleteCustomList (listName) {
validate.listName(listName)
return getListMetadata()
.then(function (lists) {
const list = utils.findSpecificList(lists, listName)
if (!list) {
throw Error('List not found!')
}
return makeRequestFn(routes.deleteCustomList(list.listId))
})
.then(utils.stripPropertyFromResponse())
}
function deleteListItem (listName, listItemName) {
validate.listItemName(listItemName)
return getListMetadata()
.then(function (lists) {
const list = utils.findSpecificList(lists, listName)
if (!list) {
throw Error('List not found!')
}
return makeRequestFn(routes.getList(list.listId, list.state))
})
.then(function (list) {
const listItem = utils.findSpecificListItem(_.get(list, 'body.items'), listItemName)
return makeRequestFn(routes.deleteListItem(_.get(list, 'body.listId'), listItem.id))
})
.then(utils.stripPropertyFromResponse())
}
function getList (listName, status = 'active') {
validate.listName(listName)
validate.status(status)
return getListMetadata()
.then(function (lists) {
const list = utils.findSpecificList(lists, listName, status)
if (!list) {
throw Error('List not found!')
}
return makeRequestFn(routes.getList(list.listId, status))
})
.then(utils.stripPropertyFromResponse())
}
function getListItem (listName, listItemName, status = 'active') {
validate.listItemName(listItemName)
return getList(listName, status)
.then(function (list) {
const listItem = utils.findSpecificListItem(list.items, listItemName)
return makeRequestFn(routes.getListItem(list.listId, listItem.id))
})
.then(utils.stripPropertyFromResponse())
}
function getListMetadata () {
return makeRequestFn(routes.getListMetadata())
.then(utils.stripPropertyFromResponse('body.lists'))
}
function updateCustomList (listName, body) {
validate.listName(listName)
return getListMetadata()
.then(function (lists) {
const list = utils.findSpecificList(lists, listName)
const reqBody = _.assign(_.pick(list, ['name', 'state', 'version']), body)
if (!list) {
throw Error('List not found')
}
validate.updateCustomList(reqBody)
return makeRequestFn(routes.updateCustomList(list.listId), reqBody)
})
.then(utils.stripPropertyFromResponse())
}
function updateListItem (listName, listItemName, body) {
validate.listName(listName)
validate.listItemName(listItemName)
return getListMetadata()
.then(function (lists) {
const list = utils.findSpecificList(lists, listName)
return makeRequestFn(routes.getList(list.listId, list.state))
})
.then(function (list) {
const listItem = utils.findSpecificListItem(_.get(list, 'body.items'), listItemName)
if (!listItem) {
throw Error('List item not found!')
}
const reqBody = _.assign(_.pick(listItem, ['status', 'value', 'version']), body)
validate.updateListItem(reqBody)
return makeRequestFn(routes.updateListItem(_.get(list, 'body.listId'), listItem.id), reqBody)
})
.then(utils.stripPropertyFromResponse())
}
return {
createCustomList,
createNewListItem,
deleteCustomList,
deleteListItem,
getList,
getListItem,
getListMetadata,
updateCustomList,
updateListItem
}
}
module.exports = {
getPermissionsCard,
init,
routes
}