-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
317 lines (272 loc) · 8.53 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Node.js client for the Untappd API (v4)
*
* By: Matt Le
*/
const axios = require('axios');
class Untappd{
constructor(accessToken){
this.accessToken = accessToken;
}
/**
* Make request
* @param {String} method GET or POST
* @param {String} path Endpoint path
* @param {Object} params Query paramters
* @param {Object} data POST data (null for GET requests)
* @param {Function} callback Callback (optional)
* @return {Promise} Promise containing result
*/
req(method, path, params, data, callback) {
if (!params) params = {};
Object.keys(params).forEach(k => {
if (params[k] == null)
delete params[k];
});
if(this.accessToken){
params.access_token = this.accessToken;
}else{
params.client_id = this.clientId;
params.client_secret = this.clientSecret;
}
return axios.request({
url : path,
method,
baseURL : 'https://api.untappd.com',
params,
data
}).then(({data}) => {
if(callback){
callback(null, data)
}else{
return data;
}
}).catch(err => {
if(callback){
callback(err);
}else{
throw(err);
}
})
}
/**
* Perform POST request
* @param { String } path - URL path
* @param { Object } params - Query parameters
* @param { Object } data - The data to send in POST
* @param { Function } callback - (Optional) callback (returns promise also)
* @return { Promise } - Promise containing data or exception
*/
post(path, params, data, callback){
return this.req("POST",path,params,data,callback);
}
/**
* Perform GET request
* @param { String } path - URL path
* @param { Object } params - Query parameters
* @param { Function } callback - (Optional) callback (returns promise also)
* @return { Promise } - Promise containing data or exception
*/
get(path, params, callback){
return this.req("GET",path,params,null,callback);
}
validate(object, fields, path){
fields.forEach(field => {
if(object[field] == null){
throw new Error(`Field ${field} cannot be null in ${path}`)
}
});
}
/**
* Explicitly set the access token. This method is useful if
* you have more than one access token and want to use a different
* token than the one specified in the constructor
* @param {String} token - Access token
*/
setAccessToken(token){
this.accessToken = token;
}
setClientId(clientId){
this.clientId = clientId;
}
setClientSecret(clientSecret){
this.clientSecret = clientSecret;
}
/**
* Return the current access token
* @return {String} - Access token
*/
getAccessToken(){
return this.accessToken;
}
/* -------------------- FEEDS ---------------------- */
// https://untappd.com/api/docs#activityfeed
activityFeed(data, callback) {
data = data || {};
return this.get("/v4/checkin/recent", data, callback);
}
// https://untappd.com/api/docs#useractivityfeed
userActivityFeed(data, callback) {
data = data || {};
this.validate(data, ['USERNAME']);
return this.get(`/v4/user/checkins/${data.USERNAME}`, data, callback);
}
// https://untappd.com/api/docs#theppublocal
pubFeed(data, callback) {
data = data || {};
return this.get("/v4/thepub/local", data, callback);
}
// https://untappd.com/api/docs#venueactivityfeed
venueActivityFeed(data, callback) {
data = data || {};
this.validate(data, ["VENUE_ID"])
return this.get(`/v4/venue/checkins/${data.VENUE_ID}`, data, callback);
}
// https://untappd.com/api/docs#beeractivityfeed
beerActivityFeed(data, callback) {
data = data || {}
this.validate(data, ['BID']);
return this.get(`/v4/beer/checkins/${data.BID}`, data, callback);
}
// https://untappd.com/api/docs#breweryactivityfeed
breweryActivityFeed(data, callback) {
data = data || {};
this.validate(data, ['BREWERY_ID']);
return this.get(`/v4/brewery/checkins/${data.BREWERY_ID}`, data, callback);
}
// https://untappd.com/api/docs#notifications
notifications(data, callback) {
data = data || {};
return this.get("/v4/notifications", data, callback);
}
/* --------------------- The INFO / SEARCH ------------------------- */
// https://untappd.com/api/docs#userinfo
userInfo(data, callback) {
data = data || {};
return this.get(`/v4/user/info/${(data.USERNAME || '')}`, data, callback);
}
// https://untappd.com/api/docs#userwishlist
userWishList(data, callback) {
data = data || {};
return this.get(`/v4/user/wishlist/${(data.USERNAME || '')}`, data, callback);
}
// https://untappd.com/api/docs#userfriends
userFriends(data, callback) {
data = data || {};
return this.get(`/v4/user/friends/${data.USERNAME || ''})`, data, callback);
}
// https://untappd.com/api/docs#userbadges
userBadges(data, callback) {
data = data || {};
return this.get(`/v4/user/badges/${data.USERNAME || ''})`, data, callback);
}
// https://untappd.com/api/docs#userbeers
userDistinctBeers(data, callback) {
data = data || {};
return this.get(`/v4/user/beers/${data.USERNAME || ''})`, data, callback);
}
// https://untappd.com/api/docs#breweryinfo
breweryInfo(data, callback) {
data = data || {};
this.validate(data, ['BREWERY_ID']);
return this.get(`/v4/brewery/info/${data.BREWERY_ID}`, data, callback);
}
// https://untappd.com/api/docs#beerinfo
beerInfo(data, callback) {
data = data || {};
this.validate(data, ['BID'], '/v4/beer/info');
return this.get(`/v4/beer/info/${data.BID}`, data, callback);
}
// https://untappd.com/api/docs#venueinfo
venueInfo(data, callback) {
data = data || {};
this.validate(data, ['VENUE_ID']);
return this.get(`/v4/venue/info/${data.VENUE_ID}`, data, callback);
}
// https://untappd.com/api/docs#beersearch
beerSearch(data, callback) {
data = data || {};
this.validate(data, ['q']);
return this.get("/v4/search/beer", data, callback);
}
// https://untappd.com/api/docs#brewerysearch
brewerySearch(data, callback) {
data = data || {};
this.validate(data, ['q']);
return this.get("/v4/search/brewery", data, callback);
}
/* ---------------------- Actions -------------------------- */
// https://untappd.com/api/docs#checkin
checkin(data, callback) {
data = data || {};
this.validate(data, ['gmt_offset', 'timezone', 'bid']);
return this.post("/v4/checkin/add", {} , data, callback);
}
// https://untappd.com/api/docs#toast
// If already toasted, this will untoast, otherwise it toasts.
toast(data, callback) {
data = data || {};
this.validate(data, ['CHECKIN_ID']);
return this.get(`/v4/checkin/toast/${data.CHECKIN_ID}`, data, callback);
}
// https://untappd.com/api/docs#pendingfriends
pendingFriends(data, callback) {
data = data || {};
return this.get("/v4/user/pending", data, callback);
}
// https://untappd.com/api/docs#addfriend
requestFriends(data, callback) {
data = data || {};
this.validate(data, ['TARGET_ID']);
return this.get(`/v4/friend/request/${data.TARGET_ID}`, data, callback);
}
// https://untappd.com/api/docs#removefriend
removeFriends(data, callback) {
data = data || {};
this.validate(data, ['TARGET_ID']);
return this.get(`/v4/friend/remove/${data.TARGET_ID}`, data, callback);
}
// https://untappd.com/api/docs#acceptfriend
acceptFriends(data, callback) {
data = data || {};
this.validate(data, ['TARGET_ID']);
return this.post(`/v4/friend/accept/${data.TARGET_ID}`, {}, data, callback);
}
// https://untappd.com/api/docs#rejectfriend
rejectFriends(data, callback) {
data = data || {};
this.validate(data, ['TARGET_ID']);
return this.post(`/v4/friend/reject/${data.TARGET_ID}`, {}, data, callback);
}
// https://untappd.com/api/docs#addcomment
addComment(data, callback) {
data = data || {};
this.validate(data, ['CHECKIN_ID', 'shout']);
return this.post(`/v4/checkin/addcomment/${data.CHECKIN_ID}`, {}, data, callback);
}
// https://untappd.com/api/docs#removecommment
removeComment(data, callback) {
data = data || {};
this.validate(data, ['COMMENT_ID']);
return this.post(`/v4/checkin/deletecomment/${data.COMMENT_ID}`, {}, data, callback);
}
// https://untappd.com/api/docs#addwish
addToWishList(data, callback) {
data = data || {};
this.validate(data, ['bid']);
return this.get("/v4/user/wishlist/add", data, callback);
}
// https://untappd.com/api/docs#removewish
removeFromWishList(data, callback) {
data = data || {};
this.validate(data, ['bid']);
return this.get("/v4/user/wishlist/remove", data, callback);
}
// https://untappd.com/api/docs#foursquarelookup
foursquareVenueLookup(data, callback) {
data = data || {};
this.validate(data, ['VENUE_ID']);
return this.get(`/v4/venue/foursquare_lookup/${data.VENUE_ID}`, data, callback);
}
}
module.exports = Untappd;