laconia
laconia copied to clipboard
Make cors feature built-in
Is your feature request related to a problem? Please describe. It's common to configure CORS headers in API endpoints. Laconia at the moment does not provide anything out of the box to configure CORS.
Describe the solution you'd like CORS capability must be provided built-in and configured through environment variables. The reason it must be configured through environment variables is because CORS must also be configured in AWS API Gateway.
For example if serverless framework is used, it may look like:
my-function:
handler: myfunction.handler
environment:
LACONIA_ADAPTER_CORS_ORIGIN: https://my-domain.co.uk
events:
- http:
method: get
path: foobar
cors:
origins: https://my-domain.co.uk
The function code:
const adapterApi = require("@laconia/adapter-api");
// Automatically reads LACONIA_ADAPTER_CORS_ORIGIN and respond with CORS headers
const apigateway = adapterApi.apigateway()
exports.handler = laconia(apigateway(app)).register(instances);
The above code will return a HTTP api gateway response with this header:
'Access-Control-Allow-Origin': https://my-domain.co.uk
ORIGIN is used instead of ORIGINS in the environment variable, because I don't think we HTTP protocol allow multiple origins.
Acceptance Criteria
- The example composition in our documentation will need to be updated as it's configuring cors at the moment
- Must be able to set CORS headers on error
@laconiajs/contributors Sorry I should have started this issue as a discussion. But it would be great if you can review the above public contract / API.