added CORS logic.

This commit is contained in:
Michael Mainguy 2023-08-15 09:57:08 -05:00
parent 6c0ed7bc25
commit c54ce61395

View File

@ -3,21 +3,37 @@ import axios from 'axios';
export const handler: Handler = async (event: HandlerEvent, context: HandlerContext) => { export const handler: Handler = async (event: HandlerEvent, context: HandlerContext) => {
try { try {
console.log(JSON.stringify(event.headers)); switch (event.httpMethod) {
const apiKey = event.headers['api-key']; case 'POST':
console.log(apiKey.substring(-5)); const apiKey = event.headers['api-key'];
const query = event.body; const query = event.body;
console.log(query); const response = await axios.post('https://api.newrelic.com/graphql', // use account token to get a temp user token
const response = await axios.post('https://api.newrelic.com/graphql', // use account token to get a temp user token query,
query, {headers: {'Api-Key': apiKey, 'Content-Type': 'application/json'}});
{headers: {'Api-Key': apiKey, 'Content-Type': 'application/json'}}); const data = await response.data;
return {
const data = await response.data; headers: {'Content-Type': 'application/json'},
console.log(data) statusCode: 200,
return { body: JSON.stringify(data)
headers: {'Content-Type': 'application/json'}, }
statusCode: 200, break;
body: JSON.stringify(data) case 'OPTIONS':
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE'
};
return {
statusCode: 200,
headers,
body: 'OK'
}
break;
default:
return {
statusCode: 405,
body: 'Method Not Allowed'
}
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);