Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 17, 2026·3 min de lecture

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.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Moya Overview
Commande CLI universelle
npx tokrepo install 45d9e1e8-520c-11f1-9bc6-00163e2b0d79

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires