diff options
Diffstat (limited to 'src/geddit.js')
-rw-r--r-- | src/geddit.js | 153 |
1 files changed, 46 insertions, 107 deletions
diff --git a/src/geddit.js b/src/geddit.js index d7e54e2..3b090c6 100644 --- a/src/geddit.js +++ b/src/geddit.js | |||
@@ -12,20 +12,18 @@ class Geddit { | |||
12 | }; | 12 | }; |
13 | } | 13 | } |
14 | 14 | ||
15 | async getSubmissions(sort = null, subreddit = null, options = {}) { | 15 | async getSubmissions(sort = "hot", subreddit = null, options = {}) { |
16 | const params = { | 16 | const params = { |
17 | limit: 20, | 17 | limit: 20, |
18 | include_over_18: true, | 18 | include_over_18: true, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | sort = sort ? sort : "hot"; | 21 | subreddit = subreddit ? `/r/${subreddit}` : ""; |
22 | subreddit = subreddit ? "/r/" + subreddit : ""; | ||
23 | 22 | ||
24 | return await fetch( | 23 | return await fetch( |
25 | this.host + | 24 | `${ |
26 | subreddit + | 25 | this.host + subreddit |
27 | `/${sort}.json?` + | 26 | }/${sort}.json?${new URLSearchParams(Object.assign(params, options))}`, |
28 | new URLSearchParams(Object.assign(params, options)), | ||
29 | ) | 27 | ) |
30 | .then((res) => res.json()) | 28 | .then((res) => res.json()) |
31 | .then((json) => json.data) | 29 | .then((json) => json.data) |
@@ -38,11 +36,7 @@ class Geddit { | |||
38 | 36 | ||
39 | async getDomainHot(domain, options = this.parameters) { | 37 | async getDomainHot(domain, options = this.parameters) { |
40 | return await fetch( | 38 | return await fetch( |
41 | this.host + | 39 | `${this.host}/domain/${domain}/hot.json?${new URLSearchParams(options)}`, |
42 | "/domain/" + | ||
43 | domain + | ||
44 | "/hot.json?" + | ||
45 | new URLSearchParams(options), | ||
46 | ) | 40 | ) |
47 | .then((res) => res.json()) | 41 | .then((res) => res.json()) |
48 | .then((json) => json.data) | 42 | .then((json) => json.data) |
@@ -55,11 +49,7 @@ class Geddit { | |||
55 | 49 | ||
56 | async getDomainBest(domain, options = this.parameters) { | 50 | async getDomainBest(domain, options = this.parameters) { |
57 | return await fetch( | 51 | return await fetch( |
58 | this.host + | 52 | `${this.host}/domain/${domain}/best.json?${new URLSearchParams(options)}`, |
59 | "/domain/" + | ||
60 | domain + | ||
61 | "/best.json?" + | ||
62 | new URLSearchParams(options), | ||
63 | ) | 53 | ) |
64 | .then((res) => res.json()) | 54 | .then((res) => res.json()) |
65 | .then((json) => json.data) | 55 | .then((json) => json.data) |
@@ -72,11 +62,7 @@ class Geddit { | |||
72 | 62 | ||
73 | async getDomainTop(domain, options = this.parameters) { | 63 | async getDomainTop(domain, options = this.parameters) { |
74 | return await fetch( | 64 | return await fetch( |
75 | this.host + | 65 | `${this.host}/domain/${domain}/top.json?${new URLSearchParams(options)}`, |
76 | "/domain/" + | ||
77 | domain + | ||
78 | "/top.json?" + | ||
79 | new URLSearchParams(options), | ||
80 | ) | 66 | ) |
81 | .then((res) => res.json()) | 67 | .then((res) => res.json()) |
82 | .then((json) => json.data) | 68 | .then((json) => json.data) |
@@ -89,11 +75,7 @@ class Geddit { | |||
89 | 75 | ||
90 | async getDomainNew(domain, options = this.parameters) { | 76 | async getDomainNew(domain, options = this.parameters) { |
91 | return await fetch( | 77 | return await fetch( |
92 | this.host + | 78 | `${this.host}/domain/${domain}/new.json?${new URLSearchParams(options)}`, |
93 | "/domain/" + | ||
94 | domain + | ||
95 | "/new.json?" + | ||
96 | new URLSearchParams(options), | ||
97 | ) | 79 | ) |
98 | .then((res) => res.json()) | 80 | .then((res) => res.json()) |
99 | .then((json) => json.data) | 81 | .then((json) => json.data) |
@@ -106,11 +88,7 @@ class Geddit { | |||
106 | 88 | ||
107 | async getDomainRising(domain, options = this.parameters) { | 89 | async getDomainRising(domain, options = this.parameters) { |
108 | return await fetch( | 90 | return await fetch( |
109 | this.host + | 91 | `${this.host}/domain/${domain}/rising.json?${new URLSearchParams(options)}`, |
110 | "/domain/" + | ||
111 | domain + | ||
112 | "/rising.json?" + | ||
113 | new URLSearchParams(options), | ||
114 | ) | 92 | ) |
115 | .then((res) => res.json()) | 93 | .then((res) => res.json()) |
116 | .then((json) => json.data) | 94 | .then((json) => json.data) |
@@ -123,11 +101,7 @@ class Geddit { | |||
123 | 101 | ||
124 | async getDomainControversial(domain, options = this.parameters) { | 102 | async getDomainControversial(domain, options = this.parameters) { |
125 | return await fetch( | 103 | return await fetch( |
126 | this.host + | 104 | `${this.host}/domain/${domain}/controversial.json?${new URLSearchParams(options)}`, |
127 | "/domain/" + | ||
128 | domain + | ||
129 | "/controversial.json?" + | ||
130 | new URLSearchParams(options), | ||
131 | ) | 105 | ) |
132 | .then((res) => res.json()) | 106 | .then((res) => res.json()) |
133 | .then((json) => json.data) | 107 | .then((json) => json.data) |
@@ -139,51 +113,47 @@ class Geddit { | |||
139 | } | 113 | } |
140 | 114 | ||
141 | async getSubreddit(subreddit) { | 115 | async getSubreddit(subreddit) { |
142 | return await fetch(this.host + "/r/" + subreddit + "/about.json") | 116 | return await fetch(`${this.host}/r/${subreddit}/about.json`) |
143 | .then((res) => res.json()) | 117 | .then((res) => res.json()) |
144 | .then((json) => json.data) | 118 | .then((json) => json.data) |
145 | .catch((err) => null); | 119 | .catch((err) => null); |
146 | } | 120 | } |
147 | 121 | ||
148 | async getSubredditRules(subreddit) { | 122 | async getSubredditRules(subreddit) { |
149 | return await fetch(this.host + "/r/" + subreddit + "/about/rules.json") | 123 | return await fetch(`${this.host}/r/${subreddit}/about/rules.json`) |
150 | .then((res) => res.json()) | 124 | .then((res) => res.json()) |
151 | .then((json) => json.data) | 125 | .then((json) => json.data) |
152 | .catch((err) => null); | 126 | .catch((err) => null); |
153 | } | 127 | } |
154 | 128 | ||
155 | async getSubredditModerators(subreddit) { | 129 | async getSubredditModerators(subreddit) { |
156 | return await fetch(this.host + "/r/" + subreddit + "/about/moderators.json") | 130 | return await fetch(`${this.host}/r/${subreddit}/about/moderators.json`) |
157 | .then((res) => res.json()) | 131 | .then((res) => res.json()) |
158 | .then((json) => json.data) | 132 | .then((json) => json.data) |
159 | .then( | 133 | .then({ |
160 | (data = { | 134 | data: { |
161 | users: data.children, | 135 | users: data.children, |
162 | }), | 136 | }, |
163 | ) | 137 | }) |
164 | .catch((err) => null); | 138 | .catch((err) => null); |
165 | } | 139 | } |
166 | 140 | ||
167 | async getSubredditWikiPages(subreddit) { | 141 | async getSubredditWikiPages(subreddit) { |
168 | return await fetch(this.host + "/r/" + subreddit + "/wiki/pages.json") | 142 | return await fetch(`${this.host}/r/${subreddit}/wiki/pages.json`) |
169 | .then((res) => res.json()) | 143 | .then((res) => res.json()) |
170 | .then((json) => json.data) | 144 | .then((json) => json.data) |
171 | .catch((err) => null); | 145 | .catch((err) => null); |
172 | } | 146 | } |
173 | 147 | ||
174 | async getSubredditWikiPage(subreddit, page) { | 148 | async getSubredditWikiPage(subreddit, page) { |
175 | return await fetch( | 149 | return await fetch(`${this.host}/r/${subreddit}/wiki/${page}.json`) |
176 | this.host + "/r/" + subreddit + "/wiki/" + page + ".json", | ||
177 | ) | ||
178 | .then((res) => res.json()) | 150 | .then((res) => res.json()) |
179 | .then((json) => json.data) | 151 | .then((json) => json.data) |
180 | .catch((err) => null); | 152 | .catch((err) => null); |
181 | } | 153 | } |
182 | 154 | ||
183 | async getSubredditWikiPageRevisions(subreddit, page) { | 155 | async getSubredditWikiPageRevisions(subreddit, page) { |
184 | return await fetch( | 156 | return await fetch(`${this.host}/r/${subreddit}/wiki/revisions${page}.json`) |
185 | this.host + "/r/" + subreddit + "/wiki/revisions" + page + ".json", | ||
186 | ) | ||
187 | .then((res) => res.json()) | 157 | .then((res) => res.json()) |
188 | .then((json) => json.data.children) | 158 | .then((json) => json.data.children) |
189 | .catch((err) => null); | 159 | .catch((err) => null); |
@@ -191,7 +161,7 @@ class Geddit { | |||
191 | 161 | ||
192 | async getPopularSubreddits(options = this.parameters) { | 162 | async getPopularSubreddits(options = this.parameters) { |
193 | return await fetch( | 163 | return await fetch( |
194 | this.host + "/subreddits/popular.json?" + new URLSearchParams(options), | 164 | `${this.host}/subreddits/popular.json?${new URLSearchParams(options)}`, |
195 | ) | 165 | ) |
196 | .then((res) => res.json()) | 166 | .then((res) => res.json()) |
197 | .then((json) => json.data) | 167 | .then((json) => json.data) |
@@ -204,7 +174,7 @@ class Geddit { | |||
204 | 174 | ||
205 | async getNewSubreddits(options = this.parameters) { | 175 | async getNewSubreddits(options = this.parameters) { |
206 | return await fetch( | 176 | return await fetch( |
207 | this.host + "/subreddits/new.json?" + new URLSearchParams(options), | 177 | `${this.host}/subreddits/new.json?${new URLSearchParams(options)}`, |
208 | ) | 178 | ) |
209 | .then((res) => res.json()) | 179 | .then((res) => res.json()) |
210 | .then((json) => json.data) | 180 | .then((json) => json.data) |
@@ -217,7 +187,7 @@ class Geddit { | |||
217 | 187 | ||
218 | async getPremiumSubreddits(options = this.parameters) { | 188 | async getPremiumSubreddits(options = this.parameters) { |
219 | return await fetch( | 189 | return await fetch( |
220 | this.host + "/subreddits/premium.json?" + new URLSearchParams(options), | 190 | `${this.host}/subreddits/premium.json?${new URLSearchParams(options)}`, |
221 | ) | 191 | ) |
222 | .then((res) => res.json()) | 192 | .then((res) => res.json()) |
223 | .then((json) => json.data) | 193 | .then((json) => json.data) |
@@ -230,7 +200,7 @@ class Geddit { | |||
230 | 200 | ||
231 | async getDefaultSubreddits(options = this.parameters) { | 201 | async getDefaultSubreddits(options = this.parameters) { |
232 | return await fetch( | 202 | return await fetch( |
233 | this.host + "/subreddits/default.json?" + new URLSearchParams(options), | 203 | `${this.host}/subreddits/default.json?${new URLSearchParams(options)}`, |
234 | ) | 204 | ) |
235 | .then((res) => res.json()) | 205 | .then((res) => res.json()) |
236 | .then((json) => json.data) | 206 | .then((json) => json.data) |
@@ -243,7 +213,7 @@ class Geddit { | |||
243 | 213 | ||
244 | async getPopularUsers(options = this.parameters) { | 214 | async getPopularUsers(options = this.parameters) { |
245 | return await fetch( | 215 | return await fetch( |
246 | this.host + "/users/popular.json?" + new URLSearchParams(options), | 216 | `${this.host}/users/popular.json?${new URLSearchParams(options)}`, |
247 | ) | 217 | ) |
248 | .then((res) => res.json()) | 218 | .then((res) => res.json()) |
249 | .then((json) => json.data) | 219 | .then((json) => json.data) |
@@ -256,7 +226,7 @@ class Geddit { | |||
256 | 226 | ||
257 | async getNewUsers(options = this.parameters) { | 227 | async getNewUsers(options = this.parameters) { |
258 | return await fetch( | 228 | return await fetch( |
259 | this.host + "/users/new.json?" + new URLSearchParams(options), | 229 | `${this.host}/users/new.json?${new URLSearchParams(options)}`, |
260 | ) | 230 | ) |
261 | .then((res) => res.json()) | 231 | .then((res) => res.json()) |
262 | .then((json) => json.data) | 232 | .then((json) => json.data) |
@@ -277,9 +247,7 @@ class Geddit { | |||
277 | }; | 247 | }; |
278 | 248 | ||
279 | return await fetch( | 249 | return await fetch( |
280 | this.host + | 250 | `${this.host}/search.json?${new URLSearchParams(Object.assign(params, options))}`, |
281 | "/search.json?" + | ||
282 | new URLSearchParams(Object.assign(params, options)), | ||
283 | ) | 251 | ) |
284 | .then((res) => res.json()) | 252 | .then((res) => res.json()) |
285 | .then((json) => json.data) | 253 | .then((json) => json.data) |
@@ -299,9 +267,7 @@ class Geddit { | |||
299 | }; | 267 | }; |
300 | 268 | ||
301 | return await fetch( | 269 | return await fetch( |
302 | this.host + | 270 | `${this.host}/subreddits/search.json?${new URLSearchParams(Object.assign(params, options))}`, |
303 | "/subreddits/search.json?" + | ||
304 | new URLSearchParams(Object.assign(params, options)), | ||
305 | ) | 271 | ) |
306 | .then((res) => res.json()) | 272 | .then((res) => res.json()) |
307 | .then((json) => json.data) | 273 | .then((json) => json.data) |
@@ -321,9 +287,7 @@ class Geddit { | |||
321 | }; | 287 | }; |
322 | 288 | ||
323 | return await fetch( | 289 | return await fetch( |
324 | this.host + | 290 | `${this.host}/users/search.json?${new URLSearchParams(Object.assign(params, options))}`, |
325 | "/users/search.json?" + | ||
326 | new URLSearchParams(Object.assign(params, options)), | ||
327 | ) | 291 | ) |
328 | .then((res) => res.json()) | 292 | .then((res) => res.json()) |
329 | .then((json) => json.data) | 293 | .then((json) => json.data) |
@@ -336,7 +300,7 @@ class Geddit { | |||
336 | 300 | ||
337 | async searchAll(query, subreddit = null, options = {}) { | 301 | async searchAll(query, subreddit = null, options = {}) { |
338 | options.q = query; | 302 | options.q = query; |
339 | subreddit = subreddit ? "/r/" + subreddit : ""; | 303 | subreddit = subreddit ? `/r/${subreddit}` : ""; |
340 | 304 | ||
341 | const params = { | 305 | const params = { |
342 | limit: 25, | 306 | limit: 25, |
@@ -345,10 +309,9 @@ class Geddit { | |||
345 | }; | 309 | }; |
346 | 310 | ||
347 | return await fetch( | 311 | return await fetch( |
348 | this.host + | 312 | `${ |
349 | subreddit + | 313 | this.host + subreddit |
350 | "/search.json?" + | 314 | }/search.json?${new URLSearchParams(Object.assign(params, options))}`, |
351 | new URLSearchParams(Object.assign(params, options)), | ||
352 | ) | 315 | ) |
353 | .then((res) => res.json()) | 316 | .then((res) => res.json()) |
354 | .then((json) => | 317 | .then((json) => |
@@ -366,7 +329,7 @@ class Geddit { | |||
366 | } | 329 | } |
367 | 330 | ||
368 | async getSubmission(id) { | 331 | async getSubmission(id) { |
369 | return await fetch(this.host + "/by_id/" + id + ".json") | 332 | return await fetch(`${this.host}/by_id/${id}.json`) |
370 | .then((res) => res.json()) | 333 | .then((res) => res.json()) |
371 | .then((json) => json.data.children[0].data) | 334 | .then((json) => json.data.children[0].data) |
372 | .catch((err) => null); | 335 | .catch((err) => null); |
@@ -374,7 +337,7 @@ class Geddit { | |||
374 | 337 | ||
375 | async getSubmissionComments(id, options = this.parameters) { | 338 | async getSubmissionComments(id, options = this.parameters) { |
376 | return await fetch( | 339 | return await fetch( |
377 | this.host + "/comments/" + id + ".json?" + new URLSearchParams(options), | 340 | `${this.host}/comments/${id}.json?${new URLSearchParams(options)}`, |
378 | ) | 341 | ) |
379 | .then((res) => res.json()) | 342 | .then((res) => res.json()) |
380 | .then((json) => ({ | 343 | .then((json) => ({ |
@@ -386,11 +349,7 @@ class Geddit { | |||
386 | 349 | ||
387 | async getSubredditComments(subreddit, options = this.parameters) { | 350 | async getSubredditComments(subreddit, options = this.parameters) { |
388 | return await fetch( | 351 | return await fetch( |
389 | this.host + | 352 | `${this.host}/r/${subreddit}/comments.json?${new URLSearchParams(options)}`, |
390 | "/r/" + | ||
391 | subreddit + | ||
392 | "/comments.json?" + | ||
393 | new URLSearchParams(options), | ||
394 | ) | 353 | ) |
395 | .then((res) => res.json()) | 354 | .then((res) => res.json()) |
396 | .then((json) => json.data.children) | 355 | .then((json) => json.data.children) |
@@ -398,7 +357,7 @@ class Geddit { | |||
398 | } | 357 | } |
399 | 358 | ||
400 | async getUser(username) { | 359 | async getUser(username) { |
401 | return await fetch(this.host + "/user/" + username + "/about.json") | 360 | return await fetch(`${this.host}/user/${username}/about.json`) |
402 | .then((res) => res.json()) | 361 | .then((res) => res.json()) |
403 | .then((json) => json.data) | 362 | .then((json) => json.data) |
404 | .catch((err) => null); | 363 | .catch((err) => null); |
@@ -406,11 +365,7 @@ class Geddit { | |||
406 | 365 | ||
407 | async getUserOverview(username, options = this.parameters) { | 366 | async getUserOverview(username, options = this.parameters) { |
408 | return await fetch( | 367 | return await fetch( |
409 | this.host + | 368 | `${this.host}/user/${username}/overview.json?${new URLSearchParams(options)}`, |
410 | "/user/" + | ||
411 | username + | ||
412 | "/overview.json?" + | ||
413 | new URLSearchParams(options), | ||
414 | ) | 369 | ) |
415 | .then((res) => res.json()) | 370 | .then((res) => res.json()) |
416 | .then((json) => json.data) | 371 | .then((json) => json.data) |
@@ -423,11 +378,7 @@ class Geddit { | |||
423 | 378 | ||
424 | async getUserComments(username, options = this.parameters) { | 379 | async getUserComments(username, options = this.parameters) { |
425 | return await fetch( | 380 | return await fetch( |
426 | this.host + | 381 | `${this.host}/user/${username}/comments.json?${new URLSearchParams(options)}`, |
427 | "/user/" + | ||
428 | username + | ||
429 | "/comments.json?" + | ||
430 | new URLSearchParams(options), | ||
431 | ) | 382 | ) |
432 | .then((res) => res.json()) | 383 | .then((res) => res.json()) |
433 | .then((json) => json.data) | 384 | .then((json) => json.data) |
@@ -440,11 +391,7 @@ class Geddit { | |||
440 | 391 | ||
441 | async getUserSubmissions(username, options = this.parameters) { | 392 | async getUserSubmissions(username, options = this.parameters) { |
442 | return await fetch( | 393 | return await fetch( |
443 | this.host + | 394 | `${this.host}/user/${username}/submitted.json?${new URLSearchParams(options)}`, |
444 | "/user/" + | ||
445 | username + | ||
446 | "/submitted.json?" + | ||
447 | new URLSearchParams(options), | ||
448 | ) | 395 | ) |
449 | .then((res) => res.json()) | 396 | .then((res) => res.json()) |
450 | .then((json) => json.data) | 397 | .then((json) => json.data) |
@@ -456,7 +403,7 @@ class Geddit { | |||
456 | } | 403 | } |
457 | 404 | ||
458 | async getLiveThread(id) { | 405 | async getLiveThread(id) { |
459 | return await fetch(this.host + "/live/" + id + "/about.json") | 406 | return await fetch(`${this.host}/live/${id}/about.json`) |
460 | .then((res) => res.json()) | 407 | .then((res) => res.json()) |
461 | .then((json) => json.data) | 408 | .then((json) => json.data) |
462 | .catch((err) => null); | 409 | .catch((err) => null); |
@@ -464,7 +411,7 @@ class Geddit { | |||
464 | 411 | ||
465 | async getLiveThreadUpdates(id, options = this.parameters) { | 412 | async getLiveThreadUpdates(id, options = this.parameters) { |
466 | return await fetch( | 413 | return await fetch( |
467 | this.host + "/live/" + id + ".json?" + new URLSearchParams(options), | 414 | `${this.host}/live/${id}.json?${new URLSearchParams(options)}`, |
468 | ) | 415 | ) |
469 | .then((res) => res.json()) | 416 | .then((res) => res.json()) |
470 | .then((json) => json.data.children) | 417 | .then((json) => json.data.children) |
@@ -473,11 +420,7 @@ class Geddit { | |||
473 | 420 | ||
474 | async getLiveThreadContributors(id, options = this.parameters) { | 421 | async getLiveThreadContributors(id, options = this.parameters) { |
475 | return await fetch( | 422 | return await fetch( |
476 | this.host + | 423 | `${this.host}/live/${id}/contributors.json?${new URLSearchParams(options)}`, |
477 | "/live/" + | ||
478 | id + | ||
479 | "/contributors.json?" + | ||
480 | new URLSearchParams(options), | ||
481 | ) | 424 | ) |
482 | .then((res) => res.json()) | 425 | .then((res) => res.json()) |
483 | .then((json) => json.data.children) | 426 | .then((json) => json.data.children) |
@@ -486,11 +429,7 @@ class Geddit { | |||
486 | 429 | ||
487 | async getLiveThreadDiscussions(id, options = this.parameters) { | 430 | async getLiveThreadDiscussions(id, options = this.parameters) { |
488 | return await fetch( | 431 | return await fetch( |
489 | this.host + | 432 | `${this.host}/live/${id}/discussions.json?${new URLSearchParams(options)}`, |
490 | "/live/" + | ||
491 | id + | ||
492 | "/discussions.json?" + | ||
493 | new URLSearchParams(options), | ||
494 | ) | 433 | ) |
495 | .then((res) => res.json()) | 434 | .then((res) => res.json()) |
496 | .then((json) => json.data.children) | 435 | .then((json) => json.data.children) |
@@ -499,7 +438,7 @@ class Geddit { | |||
499 | 438 | ||
500 | async getLiveThreadsNow(options = this.parameters) { | 439 | async getLiveThreadsNow(options = this.parameters) { |
501 | return await fetch( | 440 | return await fetch( |
502 | this.host + "/live/happening_now.json?" + new URLSearchParams(options), | 441 | `${this.host}/live/happening_now.json?${new URLSearchParams(options)}`, |
503 | ) | 442 | ) |
504 | .then((res) => res.json()) | 443 | .then((res) => res.json()) |
505 | .then((json) => json.data.children) | 444 | .then((json) => json.data.children) |