/
index.js
53 lines (42 loc) · 1.22 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
const axios = require('axios');
import { axiosResponseDataUnpacker } from 'axios-data-unpacker';
axiosResponseDataUnpacker(axios);
const config = {
host: 'https://favicongrabber.com',
path: '/api',
route: {
grab: '/grab'
}
};
const leftExtract = (str, of) => {
const inx = str.lastIndexOf(of);
return inx > -1 ? str.slice(inx + of.length) : str;
};
const rightExtract = (str, of) => {
const inx = str.indexOf(of);
return inx > -1 ? str.slice(0, inx) : str;
};
const extractDomain = url => {
if (url == null) {
throw new Error('The url parameter is required.');
}
let next = url;
// https://user:pass@theanubhav.com:8080/p/a/t/h?query=string#has
next = leftExtract(next, '://');
// user:pass@theanubhav.com:8080/p/a/t/h?query=string#has
next = rightExtract(next, '/');
// user:pass@theanubhav.com:8080
next = leftExtract(next, '@');
// theanubhav.com:8080
next = rightExtract(next, ':');
return next;
};
const getGrabAPIUrl = site => {
const { host, path, route } = config;
return `${host}${path}${route.grab}/${extractDomain(site)}`;
};
const grabFavicon = site => {
return axios.get(getGrabAPIUrl(site)).catch(e => (e.response ? e.response.data : e));
};
export default grabFavicon;
export { grabFavicon };