-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
186 lines (169 loc) · 5.09 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
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
186
const axios = require('axios');
const _ = require('lodash');
const config = require('./config');
/**
* @param {string} userId - the user or token id
* @param {string} userKey - the user key or token key
* @param {object} options - {routerHost}, override default router
* @return {object} The API client object
*/
module.exports = (userId, userKey, { routerHost = config.ROUTER_URL }) => {
function executeRequest(method, path, data, params) {
return axios({
url: `${routerHost}/api/v1/user/${userId}${path}`,
method,
data,
params,
headers: {
Authorization: `Bearer ${userKey}`,
},
}).then(payload => payload.data);
}
return {
/**
* @return {string} The base URL to the router
*/
routerUrl() {
return routerHost;
},
/**
* @return {string} The userId/tokenId the API is currently using
*/
tokenId() {
return userId;
},
/**
* @return {object} user object for current token/user id
*/
getUser() {
return executeRequest('GET', '');
},
/**
* Retrieves the topic for the given id
* @param {string} id - Topic id
* @return {object} topic object
*/
getTopicById(id) {
return executeRequest('GET', `/topic/${id}`);
},
/**
* Get all topic objects
* @return {array} List of all topic objects
*/
getTopics() {
return executeRequest('GET', '/topic');
},
/**
* Retrieve a topic by looking for its id first, and fail that, comparing to its name
* @param {string} idOrName - Topic id or name
* @return {object} topic - Single topic
*/
getTopicByIdOrName(idOrName) {
return this.getTopicById(idOrName)
.catch(() => this.getTopics()
.then((topics) => {
const topic = _.find(topics, x => x.name === idOrName);
if (!topic) throw new Error(`No topic found with id or name of '${idOrName}'`);
return topic;
}));
},
/**
* Get all templates associated with the user
* @return {object} Template
*/
getTemplates() {
return executeRequest('GET', '/template');
},
/**
* Create a new topic. Will fail on name collision
* @param {string} name - Name of the topic to create
* @param {Boolean_string} key - True if want key, false if not key, or string for specific key
* @return {object} Topic
*/
createTopic(name, key = true) {
return executeRequest('POST', '/topic', {
name,
key,
});
},
/**
* Delete a topic
* @param {string} id - Delete topic by id
* @return {object} Deletion object
*/
deleteTopic(id) {
return executeRequest('DELETE', `/topic/${id}`);
},
/**
* Retrieve template object with id
* @param {string} id - Id of template
* @return {object} Template
*/
getTemplate(id) {
if (!id) return Promise.reject(Error('No such template'));
return executeRequest('GET', `/template/${id}`);
},
/**
* Create a new template
* @param {string} name - Name of the template
* @param {string} language - Language of template. See /docs/languages for valid types
* @param {string} source - Source of template
* @return {object} The template object
*/
createTemplate(name, language, source) {
return executeRequest('POST', '/template', {
name,
language,
source,
});
},
/**
* Update information on template
* @param {string} id - Id of template to update
* @param {object} {name, language, source} - Optional pieces to update for template
* @return {object} template
*/
updateTemplate(id, { name, language, source }) {
if (!id) return Promise.reject(Error('No such template'));
return executeRequest('PATCH', `/template/${id}`, {
name,
language,
source,
});
},
/* eslint object-curly-newline: off */
/**
* Helper method to update a template, or create if it doesn't exist
* @param {object} {id, name, language, source}
* @return {object} template
*/
createOrUpdateTemplate({ id, name, language, source }) {
return this.updateTemplate(id, { name, language, source })
.catch(() => this.createTemplate(name, language, source));
},
/**
* @return {array} Returns list of all tokens present
*/
getTokens() {
return executeRequest('GET', '/token');
},
/**
* Create a new token
* @param {string} name - Name of token to create
* @param {string} scope - Scope of token
* @param {string} clientId - ClientId the token can be used for
* @return {object} The token
*/
createToken(name, scope, clientId = undefined) {
return executeRequest('POST', '/token', { name, scope, client_id: clientId });
},
/**
* Get events given search options
* @param {object} searchOpts - Search options
* @return {array} events
*/
getEvents(searchOpts) {
return executeRequest('GET', '/events', null, searchOpts);
},
};
};