Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsMay 17, 2026·3 min de lectura

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.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Moya Overview
Comando CLI universal
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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados