-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
61 lines (51 loc) · 1.2 KB
/
handler.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
58
59
60
61
'use strict';
const embedly = require('./embedly.js');
/** Generic wrappers**/
const error = function(callback, code, msg){
callback(null, {
statusCode: code,
headers: {
"Access-Control-Allow-Origin" : "*",
},
body: JSON.stringify({
error: true,
message: msg
})
});
};
const success = function(callback, result){
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"X-Cache-Hit": result.hit
},
body: JSON.stringify(result.data)
});
};
module.exports.oembed = (event, context, callback) => {
const url = event.queryStringParameters && event.queryStringParameters.url;
if (!url){
error(callback, 422, 'URL required');
}
embedly.oembed(url)
.then((data)=>{
success(callback, data);
})
.catch((err)=>{
error(callback, 422, err);
});
};
module.exports.extract = (event, context, callback) => {
const url = event.queryStringParameters && event.queryStringParameters.url;
if (!url){
error(callback, 422, 'URL required');
}
embedly.extract(url)
.then((data)=>{
success(callback, data);
})
.catch((err)=>{
error(callback, 422, err);
});
};