{"id":24302883,"url":"https://github.com/digipolitan/perfect-cors","last_synced_at":"2025-09-17T23:07:53.192Z","repository":{"id":98675590,"uuid":"93261916","full_name":"Digipolitan/perfect-cors","owner":"Digipolitan","description":"Perfect CORS is a swift package for providing a middleware that can be used to enable CORS","archived":false,"fork":false,"pushed_at":"2017-06-05T08:46:01.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-17T00:20:11.791Z","etag":null,"topics":["cors","middleware","perfect","perfect-server","swift","swift-server"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Digipolitan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-03T16:57:27.000Z","updated_at":"2019-02-07T00:09:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"2bb3945b-fe3f-49be-acc4-f37d87abb431","html_url":"https://github.com/Digipolitan/perfect-cors","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-cors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-cors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-cors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-cors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Digipolitan","download_url":"https://codeload.github.com/Digipolitan/perfect-cors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242225897,"owners_count":20092670,"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","middleware","perfect","perfect-server","swift","swift-server"],"created_at":"2025-01-17T00:19:59.088Z","updated_at":"2025-09-17T23:07:48.142Z","avatar_url":"https://github.com/Digipolitan.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# perfect-cors-swift\n\n[![Twitter](https://img.shields.io/badge/twitter-@Digipolitan-blue.svg?style=flat)](http://twitter.com/Digipolitan)\n\nPerfect CORS is a swift package for providing a middleware that can be used to enable [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) with various options\n\n## Installation\n\n### Swift Package Manager\n\nTo install PerfectCORS with SPM, add the following lines to your `Package.swift`.\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"XXX\",\n    dependencies: [\n        .Package(url: \"https://github.com/Digipolitan/perfect-cors-swift.git\", majorVersion: 1)\n    ]\n)\n```\n\n## Usage\n\n### Simple Usage (Enable *All* CORS Requests)\n\n```swift\nlet server = HTTPServer()\n\nlet router = RouterMiddleware()\n\nrouter.use(event: .beforeAll, middleware: CORS())\n\nrouter.get(path: \"/products\").bind { context in\n    try context.response.setBody(json: [\n        \"msg\": \"This is CORS-enabled for all origins!\"\n    ])\n    context.next()\n }\n\nserver.use(router: router)\n\nserver.serverPort = 8080\n\ndo {\n    try server.start()\n} catch PerfectError.networkError(let err, let msg) {\n    print(\"Network error thrown: \\(err) \\(msg)\")\n}\n```\n\n### Enable CORS for a Single Route\n\n```swift\nlet server = HTTPServer()\n\nlet router = RouterMiddleware()\n\nrouter.get(path: \"/products\").bind(CORS()).bind { context in\n    try context.response.setBody(json: [\n        \"This is CORS-enabled for a Single Route\"\n    ])\n    context.next()\n }\n\nserver.use(router: router)\n\nserver.serverPort = 8080\n\ndo {\n    try server.start()\n} catch PerfectError.networkError(let err, let msg) {\n    print(\"Network error thrown: \\(err) \\(msg)\")\n}\n```\n\n### Configuring CORS\n\n```swift\nlet server = HTTPServer()\n\nlet router = RouterMiddleware()\n\nlet options = CORS.Options(origin: [\"http://example.com\"], optionsSuccess: .ok)\n\nrouter.get(path: \"/products\").bind(CORS(options: options)).bind { context in\n    try context.response.setBody(json: [\n        \"This is CORS-enabled for only example.com\"\n    ])\n    context.next()\n }\n\nserver.use(router: router)\n\nserver.serverPort = 8080\n\ndo {\n    try server.start()\n} catch PerfectError.networkError(let err, let msg) {\n    print(\"Network error thrown: \\(err) \\(msg)\")\n}\n```\n\n## Configuration Options\n\n\n| Property | type | Description  |\n| --- | --- | --- |\n| origin | `[String]` | Configures the **Access-Control-Allow-Origin** CORS header, set `origin` to an array of valid origins. Each origin can be a `String`. For example `[\"http://example1.com\", \"http://example2.com\"]` will accept any request from \"http://example1.com\" or from \"http://example2.com\" |\n| methods | `[HTTPMethod]` | Configures the **Access-Control-Allow-Methods** CORS header |\n| allowedHeaders | `HTTPRequestHeader.Name` | Configures the **Access-Control-Allow-Headers** CORS header. If not specified, defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header. |\n| exposedHeaders | `HTTPResponseHeader.Name` | Configures the **Access-Control-Expose-Headers** CORS header. If not specified, no custom headers are exposed. |\n| credentials | `Bool` | Configures the **Access-Control-Allow-Credentials** CORS header. Set to `true` to pass the header, otherwise it is omitted. |\n| maxAge | `Double` | Configures the **Access-Control-Max-Age** CORS header. Set to a double to pass the header, otherwise it is omitted. |\n| preflightContinue | `Bool` | Pass the CORS preflight response to the next handler. |\n| optionsSuccessStatus | `HTTPResponseStatus` | Provides a status code to use for successful `OPTIONS` requests, since some legacy browsers (IE11, various SmartTVs) choke on `204` |\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md) for more details!\n\nThis project adheres to the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).\nBy participating, you are expected to uphold this code. Please report\nunacceptable behavior to [contact@digipolitan.com](mailto:contact@digipolitan.com).\n\n## License\n\nPerfectCORS is licensed under the [BSD 3-Clause license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigipolitan%2Fperfect-cors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigipolitan%2Fperfect-cors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigipolitan%2Fperfect-cors/lists"}