-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
47 lines (41 loc) · 1.05 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
const axios = require('axios');
const { toHumanName, hostName, findFavicon } = require('../../utils');
// Prototype for sources
class API {
static fetch(axiosConfig) {
const request = axios.create(axiosConfig);
return (endpoint, config = {}) => (
request.get(endpoint, config).then(x => x.data)
);
}
static createAxiosConfig(baseURL, apiKey) {
let params = {};
if (!apiKey) {
return {
baseURL,
timeout: 10000,
};
}
if (typeof apiKey === 'string') {
params = { apiKey };
} else {
const keyName = Object.keys(apiKey)[0];
params = { [keyName]: apiKey[keyName] };
}
return {
baseURL,
timeout: 10000,
params,
};
}
constructor(uri, type, baseHost, apiKey) {
const axiosConfig = API.createAxiosConfig(uri, apiKey);
const host = baseHost || hostName(uri);
this.fetch = API.fetch(axiosConfig);
this.name = toHumanName(type);
this.host = host;
this.type = type;
this.faviconURL = findFavicon(host);
}
}
module.exports = API;