1 | const axios = require('axios'); // Promise based HTTP client.
|
2 | const qs = require('qs'); // A querystring parsing and stringifying library.
|
3 | const API_URL = "https://utopian.rocks/api"; // The current host for Utopian API.
|
4 |
|
5 | let endpoints = {}; // JS object for API endpoints.
|
6 | /**
|
7 | * Get all utopian contributions.
|
8 | * @param {string} category - Retrieve posts from a specific category.
|
9 | * @param {string} status - Status to filter contributions.
|
10 | * @param {string} author - Retrieve posts by a specific author.
|
11 | * @param {string} moderator - Retrieve posts reviewed by a specific moderator.
|
12 | * @param {boolean} staff_picked - Retrieve posts that were or weren't staff picked.
|
13 | * @returns {object} All posts by used parameters.
|
14 | */
|
15 | endpoints.getPosts = (category, status, author, moderator, staff_picked) => axios.get(API_URL + '/posts', {
|
16 | // Multiple parameters list to filter contributions.
|
17 | params: {
|
18 | category: category, // Any category according to that post (https://steemit.com/utopian-io/@utopian-io/utopian-now-contributions-are-welcome)
|
19 | status: status, // Values unreviewed,reviewed,pending,unvoted
|
20 | author: author, // Author Steem username
|
21 | moderator: moderator, // Moderator Steem username
|
22 | staff_picked: staff_picked // Boolean
|
23 | },
|
24 | paramsSerializer: function (params) {
|
25 | // Stringifying parameters using querystring parsing.
|
26 | return qs.stringify(params, { indices: false }).replace(/([^=&?]+)=(&)/g, ''); // A regular expression to remove empty or null parameters.
|
27 | }
|
28 | }).then(function (response) {
|
29 | return response.data; // Returns an object contains all contributions.
|
30 | })
|
31 | .catch(function (error) {
|
32 | return error; // Returns an error of something wrong with response.
|
33 | });
|
34 | /**
|
35 | * Get all Utopian moderators usernames.
|
36 | * @returns {object} Total Utopian moderators.
|
37 | */
|
38 | endpoints.getModerators = () => axios.get(API_URL + '/moderators') //requesting moderators endpoint
|
39 | .then(function (response) {
|
40 | return response.data; // Returns an object contains all moderators usernames.
|
41 | })
|
42 | .catch(function (error) {
|
43 | return error; // Returns an error of something wrong with response.
|
44 | });
|
45 | /**
|
46 | * Get all Utopian statistics by date or today.
|
47 | * @param {string} specificDate
|
48 | * @returns {object}
|
49 | */
|
50 | endpoints.getStats = (specificDate, section) => axios.get(API_URL + '/statistics/' + specificDate)
|
51 | .then(function (response) {
|
52 | switch (section) {
|
53 | case 'moderators':
|
54 | return response.data[0]; // Returns an object.
|
55 | break;
|
56 | case 'categories':
|
57 | return response.data[1]; // Returns an object.
|
58 | break;
|
59 | case 'projects':
|
60 | return response.data[2]; // Returns an object.
|
61 | break;
|
62 | case 'staff_picks':
|
63 | return response.data[3]; // Returns an object.
|
64 | break;
|
65 | case 'task_requests':
|
66 | return response.data[4]; // Returns an object.
|
67 | break;
|
68 | default:
|
69 | return response.data; // Returns an object for all statistics sections.
|
70 | }
|
71 | })
|
72 | .catch(function (error) {
|
73 | return error; // Returns an error of something wrong with response.
|
74 | });
|
75 | //Exports all endpoints
|
76 | module.exports = endpoints; |
\ | No newline at end of file |