首页/ AI 技能 / @skilllib/skill-2117939930272411298
Skill站外收录 @skilllib/skill-2117939930272411298

retrofit-networking

提供类型安全的 HTTP 网络通信能力,支持自动序列化与反序列化、认证令牌注入及失效刷新机制,统一处理网络异常并映射为领域级错误类型,确保 API 调用与业务逻辑严格分离。

0.0 (0) 20 次安装 原作者 编程导航
收录于 2026-01-22
测试与安全 AI 技能

Skills.MD

# Retrofit & Dio Networking ## **Priority: P0 (CRITICAL)** Type-safe REST API communication using `Dio` and `Retrofit`. ## Structure ```text infrastructure/ ├── data_sources/ │ ├── remote/ # Retrofit abstract classes │ └── local/ # Cache/Storage └── network/ ├── dio_client.dart # Custom Dio setup └── interceptors/ # Auth, Logging, Cache ``` ## Implementation Guidelines - **Retrofit Clients**: Define abstract classes with `@RestApi()`. Use standard HTTP annotations (`@GET`, `@POST`). - **DTOs (Data Transfer Objects)**: Use `@freezed` and `json_serializable` for all response/request bodies. - **Mapping**: Data sources MUST map DTOs to Domain Entities (e.g., `userDto.toDomain()`). - **AuthInterceptor**: Logic for `Authorization: Bearer <token>` injection in `onRequest`. - **Token Refresh**: Handle `401 Unauthorized` in `onError` by locking Dio, refreshing, and retrying. - **Failures**: Map `DioException` to custom `Failure` objects (ServerFailure, NetworkFailure). ## Anti-Patterns - **No Manual JSON Parsing**: Do not use `jsonDecode(response.body)`; use Retrofit's generated mappers. - **No Global Dio**: Do not use a static global Dio instance; use dependency injection. - **No Try-Catch in API**: Do not put `try-catch` inside the Retrofit interface methods. ## Reference & Examples For RestClient definitions and Auth Interceptor implementation: See [references/REFERENCE.md](references/REFERENCE.md). ## Related Topics feature-based-clean-architecture | error-handling

评论 0

还没有评论,来抢沙发。