Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CORS headers #256

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading