-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (48 loc) · 1.33 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
54
55
56
57
const metascraper = require("metascraper")([
require("metascraper-author")(),
require("metascraper-date")(),
require("metascraper-description")(),
require("metascraper-image")(),
require("metascraper-logo")(),
require("metascraper-publisher")(),
require("metascraper-title")(),
require("metascraper-url")(),
require("metascraper-readerable")(),
require("metascraper-fulltext")(),
]);
const got = require("got");
const validateParams = (params, headers) => {
if (!headers || headers["x-auth-token"] !== process.env.DOSSIER_TOKEN) {
return false;
}
return params && params.url;
};
const urlMissingError = {
statusCode: 403,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
error:
"You must pass a url as a parameter (i.e. ?url=https://are.na) and your token in the X-AUTH-TOKEN header",
}),
};
module.exports.getURLMetadata = async (event) => {
const { queryStringParameters: params } = event;
if (!validateParams(params)) {
return urlMissingError;
}
const { body: html, url } = await got(decodeURIComponent(params.url));
const metadata = await metascraper({ html, url });
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
url,
metadata,
}),
};
return response;
};