-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapi.js
More file actions
49 lines (38 loc) · 1.37 KB
/
api.js
File metadata and controls
49 lines (38 loc) · 1.37 KB
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
'use strict';
const undici = require('undici');
const { API_BASE } = require('./constants');
const logger = require('./logger');
const IframelyAPI = {};
IframelyAPI.query = async function (data, endpoint = '') {
if (!endpoint) {
logger.error('No API key or endpoint configured, skipping Iframely');
return null;
}
const custom_endpoint = /^https?:\/\//i.test(endpoint);
let iframelyAPI = custom_endpoint ? endpoint : `${API_BASE}&api_key=${endpoint}`;
iframelyAPI += `${iframelyAPI.indexOf('?') > -1 ? '&' : '?'}url=${encodeURIComponent(data.url)}`;
if (custom_endpoint) {
iframelyAPI += '&group=true';
}
try {
const response = await undici.request(iframelyAPI);
if (response.statusCode === 404) {
logger.verbose(`Not found: ${data.url}`);
return null;
}
const json = await response.body.json();
if (response.statusCode !== 200 || !json) {
logger.verbose(`Iframely responded with error: ${JSON.stringify(json)}. Url: ${data.url}. Api call: ${iframelyAPI}`);
return null;
}
if (!json.meta || !json.links) {
logger.error(`Invalid Iframely API response. Url: ${data.url}. Api call: ${iframelyAPI}. Body: ${JSON.stringify(json)}`);
return null;
}
return json;
} catch (err) {
logger.error(`Encountered error querying Iframely API: ${err.message}. Url: ${data.url}. Api call: ${iframelyAPI}`);
return null;
}
};
module.exports = IframelyAPI;