{"id":24839992,"url":"https://github.com/xtro/swiftusecase","last_synced_at":"2025-10-14T13:31:05.774Z","repository":{"id":65163193,"uuid":"582008087","full_name":"xtro/SwiftUseCase","owner":"xtro","description":"SwiftUseCase is library for creating independent \u0026 test coverable use-cases with a powerful execution API.","archived":false,"fork":false,"pushed_at":"2024-09-02T06:10:23.000Z","size":140,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-04T14:32:09.188Z","etag":null,"topics":["blocks","combine","concurrent-programming","swift","swift-packages","unit-testing","usecase"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/xtro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["xtro"]}},"created_at":"2022-12-25T09:25:46.000Z","updated_at":"2025-03-13T19:49:11.000Z","dependencies_parsed_at":"2024-02-13T08:22:43.242Z","dependency_job_id":"c89f44ba-7990-4725-9983-3a3978a16927","html_url":"https://github.com/xtro/SwiftUseCase","commit_stats":{"total_commits":5,"total_committers":2,"mean_commits":2.5,"dds":0.4,"last_synced_commit":"e20a0d6a4fc52a4e5d25fc02cc64d932ab96382d"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/xtro/SwiftUseCase","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtro%2FSwiftUseCase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtro%2FSwiftUseCase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtro%2FSwiftUseCase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtro%2FSwiftUseCase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtro","download_url":"https://codeload.github.com/xtro/SwiftUseCase/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtro%2FSwiftUseCase/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279018715,"owners_count":26086609,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["blocks","combine","concurrent-programming","swift","swift-packages","unit-testing","usecase"],"created_at":"2025-01-31T06:54:00.004Z","updated_at":"2025-10-14T13:31:05.317Z","avatar_url":"https://github.com/xtro.png","language":"Swift","funding_links":["https://github.com/sponsors/xtro"],"categories":[],"sub_categories":[],"readme":"# SwiftUseCase\n\n[![Swift](https://github.com/xtro/SwiftUseCase/actions/workflows/swift.yml/badge.svg?branch=main)](https://github.com/xtro/SwiftUseCase/actions/workflows/swift.yml) ![platforms](https://img.shields.io/badge/platform-iOS%20%7C%20watchOS%20%7C%20tvOS%20%7C%20macOS-333333) [![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager) ![GitHub](https://img.shields.io/github/license/xtro/SwiftUseCase) ![Current version](https://img.shields.io/github/v/tag/xtro/SwiftUseCase)\n\n**SwiftUseCase** is library for creating independent \u0026 test coverable usecases with a powerfull execution api.\n\n## Installation\nYou can use Swift Package Manager to integrate the library by adding the following dependency in your Package.swift file or by adding directly within Xcode:\n\n```swift\n.package(url: \"https://github.com/xtro/SwiftUseCase.git\", .upToNextMajor(from: \"0.0.1\"))\n```\n\n## Definitions\n\nA UseCase is a simple container that contains a ``Parameter`` and ``Result`` type and executing an ``Execution``.\n \n```swift\npublic protocol UseCaseable {\n    associatedtype Parameter: Sendable\n    associatedtype Result: Sendable\n    associatedtype Execution: Sendable\n    var execute: Execution { get }\n}\n```\n\nThere are four types of executions:\n```swift\npublic typealias Executable\u003cParameter: Sendable, Result: Sendable\u003e = @Sendable (Parameter) -\u003e Result\npublic typealias AsyncExecutable\u003cParameter: Sendable, Result: Sendable\u003e = @Sendable (Parameter) async -\u003e Result\npublic typealias ThrowingExecutable\u003cParameter: Sendable, Result: Sendable\u003e = @Sendable (Parameter) throws -\u003e Result\npublic typealias AsyncThrowingExecutable\u003cParameter: Sendable, Result: Sendable\u003e = @Sendable (Parameter) async throws -\u003e Result\n```\n\n![](./Documentation/SwiftUseCase.docc/Resources/usecase_map.png)\n\n## Getting started\nIn this example we implement a data task using swift concurrency. The parameters are an URLRequest and an URLSession:\n```swift\nimport SwiftUseCase\n\npublic enum Network {\n    public struct DataTask: AsyncThrowingUseCase {\n        public struct Parameter {\n            let request: URLRequest\n            let session: URLSession\n        }\n        public typealias Result = (response: HTTPURLResponse, data: Data)\n        \n        public var execute: AsyncThrowingExecutable\u003cParameter, Result\u003e = { parameter in\n            class CancellableWrapper {\n                var dataTask: URLSessionDataTask?\n            }\n            let urlSessionTask = CancellableWrapper()\n            return try await withTaskCancellationHandler {\n                return try await withUnsafeThrowingContinuation { continuation in\n                    urlSessionTask.dataTask = parameter.session.dataTask(with: parameter.request) { data, response, error in\n                        if let error = error {\n                            continuation.resume(throwing: error)\n                        }\n                        if let data = data {\n                            let result = (response as! HTTPURLResponse, data)\n                            continuation.resume(returning: result)\n                        }\n                    }\n                    urlSessionTask.dataTask?.resume()\n                }\n            } onCancel: {\n                urlSessionTask.dataTask?.cancel()\n            }\n        }\n    }\n}\n```\n\nAfter that we can use the implemented code in many ways. \n\nUse as a combine publisher:\n```swift\nlet usecase = Network.DataTask()\nusecase.publisher(\n    .init(\n        request: URLRequest(url: URL(string: path)!),\n        session: session ?? .shared\n    )\n)\n```\n\nIt looks nice, but the parameter and initialization is too complex in this case, we can reduce it by writing an extension like this:\n\n```swift\nextension Network.DataTask.Parameter {\n    static func get(_ path: String, session: URLSession? = nil) -\u003e Self {\n        .init(\n            request: URLRequest(url: URL(string: path)!),\n            session: session ?? .shared\n        )\n    }\n}\nextension Network {\n    static var dataTask: DataTask {\n        DataTask()\n    }\n}\n```\n\nmuch better, now we can use as a Combine publisher:\n```swift\nNetwork.dataTask\n    .publisher( .get(\"https://api.coincap.io/v2/assets\") )\n    ...\n```\nor using blocks:\n```swift\nNetwork.dataTask( .get(\"https://api.coincap.io/v2/assets\") ) { result in\n   ...\n}\n```\n\nor in swift concurrency:\n```swift\nlet result = try await Network.dataTask( .get(\"https://api.coincap.io/v2/assets\") )\n```\n\nfinally every UseCase is convertable to ``AnyUseCase`` a type-erased representation of a usecase:\n```swift\nlet usecase = MyUseCase().eraseToAnyUseCase\nusecase.onComplete = {\n    print(\"Result: \\($0)\")\n}\nusecase.onFailure = {\n    print(\"Failure: \\($0)\")\n}\nusecase(parameter)\n```\n\n## Sponsors\nSwiftUseCase is an MIT-licensed open source project with its ongoing development made possible entirely by the support of awesome backers. If you'd like to join them, please consider sponsoring this development.\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first\nto discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\nThis library is released under the [MIT](https://choosealicense.com/licenses/mit/) license. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtro%2Fswiftusecase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtro%2Fswiftusecase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtro%2Fswiftusecase/lists"}