-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.63 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
'use strict';
const axios = require('axios');
const BASE_URL = 'https://api.github.com/search/issues';
class GitFirstTimers {
constructor(options = {}) {
this.options = options;
}
getIssues() {
const queryParams = this.buildQueryParam();
const path = `${BASE_URL}?${queryParams}`;
return this._request(path);
}
buildQueryParam() {
let url = [];
// destructure the object
const { q, sort, order, label = "good first issue" } = this.options;
if (!q) url = ['q=""'];
if (q) url = [`q="${q}"`];
if (this.isObject(this.options)) {
Object.keys(this.options).map((key) => {
if (key != 'sort' && key != 'order' && key != 'q') {
let value = `${key}:"${encodeURI(this.options[key])}"`;
url.push(value);
}
});
url = url.join('+');
if (sort) url = `${url}&sort:${sort}`;
if (order) url = `${url}&order:${order}`;
}
return url;
}
isObject(object) {
return (Object.prototype.toString.call(object) === "[object Object]");
}
_request(url, method = 'GET', reqParam = {}) {
const config = {
url,
method,
params: reqParam,
data: undefined,
responseType: 'json',
};
return new Promise((resolve, reject) => {
axios(config).then((obj) => {
resolve(obj.data);
}).catch((error) => {
reject(error);
});
})
}
};
module.exports = GitFirstTimers;