Skip to content

Commit

Permalink
Merge pull request #256 from jonfairbanks/fix/response-headers
Browse files Browse the repository at this point in the history
Fix CORS headers
  • Loading branch information
jonfairbanks authored Oct 15, 2024
2 parents 4366d1a + 38ec624 commit 1d4a5f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ resource "aws_api_gateway_method_response" "yo_api_catch_all_method_response" {
resource_id = aws_api_gateway_resource.yo_api_catch_all.id
http_method = aws_api_gateway_method.yo_api_catch_all_method.http_method
status_code = "200"

response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = true
}
Expand Down Expand Up @@ -222,7 +223,7 @@ resource "aws_api_gateway_integration_response" "yo_api_catch_all_integration_re
status_code = "200"

response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = "'*'" # TODO: specify a domain here instead of "*"
"method.response.header.Access-Control-Allow-Origin" = "integration.response.header.Access-Control-Allow-Origin"
}

response_templates = {
Expand Down
15 changes: 13 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,19 @@ export const handler = async (
}

// Pass the event and context to the serverless app
const response = await serverlessApp(event, context);
return response;
const response: any = await serverlessApp(event, context);

const headers = {
...response?.headers || {},
'Access-Control-Allow-Origin': '*', // TODO: specify a domain instead of '*'
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};

return {
...response,
headers,
};
} catch (error) {
console.error('Error handling the request:', error); // Log the error
return {
Expand Down

0 comments on commit 1d4a5f4

Please sign in to comment.