-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
35 lines (30 loc) · 1013 Bytes
/
handler.js
File metadata and controls
35 lines (30 loc) · 1013 Bytes
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
'use strict';
const axios = require('axios');
const allowOriginFilter = require('./lib/utils').allowOriginFilter;
module.exports.corsProxy = async (event, context) => {
const { ALLOW_ORIGINS } = process.env;
try {
// assigns url, origin from event body
const { queryStringParameters: { url }, headers: { origin } } = event;
const response = await axios.get(decodeURIComponent(url));
const allowedOrigins = ALLOW_ORIGINS.split(',');
return {
statusCode: 200,
body: response.data,
headers: {
"Access-Control-Allow-Origin": allowOriginFilter(origin, allowedOrigins), // Required for CORS support to work
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
"content-type": response.headers['content-type']
}
};
} catch(err) {
console.error(err);
return {
statusCode: 422,
body: JSON.stringify({
event: event,
errorDetails: err
})
};
}
};