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

Make cors feature built-in #137

Open
ceilfors opened this issue Jun 28, 2019 · 1 comment
Open

Make cors feature built-in #137

ceilfors opened this issue Jun 28, 2019 · 1 comment

Comments

@ceilfors
Copy link
Collaborator

ceilfors commented Jun 28, 2019

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
@ceilfors
Copy link
Collaborator Author

ceilfors commented Jul 3, 2019

@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.

@ceilfors ceilfors changed the title Make cors middleware built-in Make cors feature built-in Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant