# Moya — Network Abstraction Layer for Swift > A network abstraction layer built on Alamofire that uses enum-based API targets to provide compile-time safety, testability, and clean separation between networking code and business logic. ## Install Save in your project root: # Moya — Network Abstraction Layer for Swift ## Quick Use ```swift import Moya enum GitHubAPI { case userProfile(String) case repositories(String) } extension GitHubAPI: TargetType { var baseURL: URL { URL(string: "https://api.github.com")! } var path: String { switch self { case .userProfile(let name): return "/users/\(name)" case .repositories(let name): return "/users/\(name)/repos" } } var method: Moya.Method { .get } var task: Task { .requestPlain } var headers: [String: String]? { nil } } let provider = MoyaProvider() provider.request(.userProfile("octocat")) { result in // handle response } ``` ## Introduction Moya is a network abstraction layer written in Swift that wraps Alamofire with a protocol-oriented approach. By defining API endpoints as Swift enum cases conforming to TargetType, developers get compile-time guarantees about request construction while keeping networking logic cleanly separated from the rest of the app. ## What Moya Does - Defines API endpoints as enum cases with associated values for parameters - Enforces correct request construction through the TargetType protocol at compile time - Provides stubbing capability for unit testing without hitting real servers - Supports plugins for logging, authentication, and request modification - Offers built-in RxSwift, Combine, and async/await reactive extensions ## Architecture Overview Moya introduces a MoyaProvider generic over a TargetType enum. When a request is made, the provider maps the enum case to a concrete Endpoint (URL, method, headers, body), then converts it to a URLRequest via Alamofire. Plugins intercept requests and responses at defined hook points. For testing, the provider accepts a stubClosure that returns sample data from the target without network access. ## Self-Hosting & Configuration - Install via Swift Package Manager with Moya, RxMoya, or CombineMoya targets - Define a TargetType enum listing every endpoint your app consumes - Implement sampleData on each case for unit test stubs - Configure MoyaProvider with plugins for NetworkLoggerPlugin or custom auth - Use MultiTarget for heterogeneous providers serving multiple API enums ## Key Features - Enum-based targets prevent typos in URLs and guarantee all required parameters are provided - Built-in stubbing mode enables full network-layer unit testing without mocking - Plugin architecture for cross-cutting concerns like logging and credential injection - First-class reactive support: RxMoya, CombineMoya, and native async/await - Endpoint customization allows modifying requests before they are sent ## Comparison with Similar Tools - **Alamofire (raw)** — lower-level; Moya adds structure and testability on top - **URLSession** — foundation layer; Moya provides abstraction over the entire API surface - **Apollo (GraphQL)** — type-safe queries for GraphQL; Moya serves REST APIs - **Retrofit (Android)** — similar concept of interface-defined endpoints using annotations ## FAQ **Q: Does Moya replace Alamofire?** A: No, Moya builds on Alamofire. It adds an abstraction layer while Alamofire handles the actual HTTP transport. **Q: How do I test networking code with Moya?** A: Create a MoyaProvider with stubClosure: MoyaProvider.immediatelyStub and define sampleData on your targets. **Q: Can I use Moya with Combine?** A: Yes, import CombineMoya and call provider.requestPublisher(.endpoint) for a publisher-based API. **Q: Is Moya suitable for large apps with many endpoints?** A: Yes, use multiple TargetType enums grouped by feature domain, each with its own provider. ## Sources - https://github.com/Moya/Moya - https://moya.github.io/Moya/ --- Source: https://tokrepo.com/en/workflows/asset-45d9e1e8 Author: AI Open Source