{"id":20298986,"url":"https://github.com/ktonon/elm-serverless-cors","last_synced_at":"2025-06-20T23:34:43.609Z","repository":{"id":57675156,"uuid":"80584935","full_name":"ktonon/elm-serverless-cors","owner":"ktonon","description":"CORS middleware for elm-serverless","archived":false,"fork":false,"pushed_at":"2017-08-15T10:56:05.000Z","size":16,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T13:32:45.433Z","etag":null,"topics":["cors","elm","elm-serverless","middleware","serverless"],"latest_commit_sha":null,"homepage":null,"language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ktonon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-01T03:03:13.000Z","updated_at":"2018-05-22T22:03:06.000Z","dependencies_parsed_at":"2022-09-02T15:02:09.066Z","dependency_job_id":null,"html_url":"https://github.com/ktonon/elm-serverless-cors","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/ktonon/elm-serverless-cors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktonon%2Felm-serverless-cors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktonon%2Felm-serverless-cors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktonon%2Felm-serverless-cors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktonon%2Felm-serverless-cors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ktonon","download_url":"https://codeload.github.com/ktonon/elm-serverless-cors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktonon%2Felm-serverless-cors/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261036860,"owners_count":23100931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cors","elm","elm-serverless","middleware","serverless"],"created_at":"2024-11-14T16:13:02.879Z","updated_at":"2025-06-20T23:34:38.584Z","avatar_url":"https://github.com/ktonon.png","language":"Elm","funding_links":[],"categories":[],"sub_categories":[],"readme":"# elm-serverless-cors\n\n[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)\n[![Elm Package](https://img.shields.io/badge/elm-1.0.1-blue.svg)](http://package.elm-lang.org/packages/ktonon/elm-serverless-cors/latest)\n[![CircleCI](https://img.shields.io/circleci/project/github/ktonon/elm-serverless-cors.svg)](https://circleci.com/gh/ktonon/elm-serverless-cors)\n\nThis is [CORS][] middleware for [elm-serverless][].\n\nThere are two ways to use it.\n\n1. Set the headers that you need, individually.\n2. Set the headers using externally provided configuration.\n\n## Method 1\n\nSet the headers that you need, individually.\n\n```elm\nimport Serverless.Conn.Request exposing (Method(..))\nimport Serverless.Cors exposing (..)\nimport Serverless.Plug exposing (pipeline, plug)\n\n\nmyPipeline =\n    pipeline\n        -- Sets access-control-allow-origin\n        -- the same as the origin request header\n        -- or if that is not found then \"*\"\n        |\u003e\n            plug (allowOrigin ReflectRequest)\n        -- Sets access-control-allow-methods\n        |\u003e\n            plug (allowMethods [ GET, OPTIONS ])\n```\n\n## Method 2\n\nSet the headers using externally provided configuration (i.e. AWS Environment variables), and decode them into your app's config record. This method is demonstrated in [elm-serverless-demo][]. In summary, you need to do the following.\n\nFirst add CORS configuration to your Serverless app's `Config` type. And decode it with the provided `Cors.configDecoder`.\n\n```elm\nimport Json.Decode exposing (Decoder, list, string)\nimport Json.Decode.Pipeline exposing (required, decode, hardcoded)\nimport Serverless.Cors as Cors\n\n\ntype alias Config =\n    { cors : Cors.Config\n    -- ...\n    }\n\n\nconfigDecoder : Json.Decode.Decoder Config\nconfigDecoder =\n    decode Config\n        |\u003e required \"cors\" Cors.configDecoder\n        -- ...\n```\n\nThen you can access the config from your `Conn` and use it to configure CORS.\n\n```elm\nimport Serverless.Conn exposing (config)\nimport Serverless.Plug exposing (pipeline, plug)\nimport Serverless.Cors exposing (cors)\n\nmyPipeline =\n    pipeline\n        |\u003e plug (Cors.fromConfig .cors)\n```\n\nOn the JavaScript side, you can do something like this to map AWS Lambda environment variables to a JavaScript object.\n\n```javascript\nconst elmServerless = require('elm-serverless');\nconst rc = require('strip-debug!shebang!rc');\n\nconst elm = require('./API.elm');\n\n// Use AWS Lambda environment variables to override these values\n// See the npm rc package README for more details\nconst config = rc('myApi', {\n  cors: {\n    origin: '*',\n    methods: 'get,post,options',\n  },\n});\n\nmodule.exports.handler = elmServerless.httpApi({\n  handler: elm.API,\n  config,\n});\n```\n\nSo in the [AWS Lambda console][], you can set environment variables, which will get loaded by [rc][] into a JavaScript configuration object, and passed into elm for decoding. In this example, you could set\n\n* `myApi_cors__origin=foo.com,bar.com`: decodes to `Exactly [\"foo.com\", \"bar.com\"]` for `origin`\n* `myApi_cors__credentials=true`: decodes to `True` for `credentials`\n\n[AWS Lambda console]:https://console.aws.amazon.com/lambda/home\n[CORS]:https://en.wikipedia.org/wiki/Cross-origin_resource_sharing\n[elm-serverless]:http://package.elm-lang.org/packages/ktonon/elm-serverless/latest\n[elm-serverless-demo]:https://github.com/ktonon/elm-serverless-demo\n[rc]:https://www.npmjs.com/package/rc#standards\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktonon%2Felm-serverless-cors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fktonon%2Felm-serverless-cors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktonon%2Felm-serverless-cors/lists"}