-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathunshort.js
48 lines (39 loc) · 1.01 KB
/
unshort.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
import axios from 'axios';
const merchants = [
'amazon',
'flipkart',
'snapdeal',
'ajio',
'myntra',
'jabong',
'bewakoof',
'limeroad',
'shein',
]
/**
* Get the long url from a short url
* @param {string} url
* @returns
*/
const unshort = async (url) => {
const extractUrl = req => req?.request?.res?.responseUrl ||
req?.request?._redirectable?._currentUrl ||
req?.request?._currentUrl ||
req?.request?._options?.href ||
'https://' + req?.request?.host + req?.request?.path;
const host = new URL(url).hostname.split(".");
const merchant = host[0] === 'www' ? host[1] : host[0];
if (merchants.includes(merchant))
return url;
let longUrl = url;
try {
const req = await axios.get(url);
const result = extractUrl(req);
longUrl = result ? result : url;
} catch (err) {
const result = extractUrl(err);
longUrl = result ? result : url;
}
return longUrl;
}
export default unshort;