-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.js
31 lines (30 loc) · 955 Bytes
/
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
function appendAlias(carrierName) {
for (const alias in aliases) {
if (aliases[alias].includes(carrierName)) {
return `(${alias}) ${carrierName}`;
}
}
return carrierName;
}
async function lookupPhoneNumber(phoneNumber) {
let response;
try {
response = await (await fetch('https://api.telnyx.com/anonymous/v2/number_lookup/' + phoneNumber)).json();
} catch (e) {
return 'Unknown error occurred.'
}
if (response.errors) {
return response.errors[0].detail;
}
const data = response.data;
return {
display: {
'Phone type': data.carrier.type || 'Unknown type',
'Carrier provider': appendAlias(data.carrier.name || 'Unknown carrier'),
'Phone number': data.phone_number,
'National format': data.national_format,
'Country Code': data.country_code
},
analysis: analyze(data)
};
}