极简接口定义和响应定义,支持更多的解析器
This commit is contained in:
@@ -1,49 +1,56 @@
|
||||
import 'package:networks_sdk/src/domain/entities/api_request_type.dart';
|
||||
import 'package:networks_sdk/src/domain/entities/http_method.dart';
|
||||
|
||||
|
||||
/// API 请求注解 — 标记一个类为 API 请求
|
||||
///
|
||||
/// 配合 `build_runner` 代码生成器,自动生成 `ApiRequestable<T>` 协议实现,
|
||||
/// 使用侧只需定义字段 + 注解,path / method / requestType / includeToken
|
||||
/// 全部由生成器自动提供。
|
||||
///
|
||||
/// ## 使用方式
|
||||
/// ## 有响应数据(指定 responseType)
|
||||
///
|
||||
/// ```dart
|
||||
/// @ApiRequest(
|
||||
/// path: '/auth/login',
|
||||
/// path: ApiPaths.authLogin,
|
||||
/// method: HttpMethod.post,
|
||||
/// responseType: LoginData,
|
||||
/// responseType: LoginResponse,
|
||||
/// requestType: ApiRequestType.login,
|
||||
/// )
|
||||
/// @JsonSerializable()
|
||||
/// class LoginRequest extends ApiRequestable<LoginData>
|
||||
/// class LoginRequest extends ApiRequestable<LoginResponse>
|
||||
/// with _$LoginRequestApi {
|
||||
/// final String email;
|
||||
/// final String password;
|
||||
///
|
||||
/// LoginRequest({required this.email, required this.password});
|
||||
///
|
||||
/// @override
|
||||
/// Map<String, dynamic> toJson() => _$LoginRequestToJson(this);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 生成器自动生成 `_$LoginRequestApi` mixin,提供:
|
||||
/// - `path` → `'/auth/login'`
|
||||
/// - `method` → `HttpMethod.post`
|
||||
/// - `requestType` → `ApiRequestType.login`
|
||||
/// - `includeToken` → `false`(login 类型自动设为 false)
|
||||
/// ## 无响应数据(省略 responseType)
|
||||
///
|
||||
/// ```dart
|
||||
/// @ApiRequest(
|
||||
/// path: ApiPaths.authLogout,
|
||||
/// method: HttpMethod.post,
|
||||
/// )
|
||||
/// class LogoutRequest extends ApiRequestable<void>
|
||||
/// with _$LogoutRequestApi {
|
||||
/// LogoutRequest();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 生成器自动生成 `_$XxxRequestApi` mixin,提供:
|
||||
/// - `path` / `method` / `requestType` / `includeToken`
|
||||
/// - `toJson()` — 从声明字段自动生成
|
||||
/// - `responseType` 存在时:`parameters` getter 自动注册 `fromJson`
|
||||
class ApiRequest {
|
||||
/// API 路径(如 `'/auth/login'`)
|
||||
/// API 路径(如 `ApiPaths.authLogin`)
|
||||
final String path;
|
||||
|
||||
/// HTTP 方法(默认 POST)
|
||||
final HttpMethod method;
|
||||
|
||||
/// 响应类型(用于泛型绑定)
|
||||
final Type responseType;
|
||||
/// 响应数据类型(省略表示无响应数据,对应 `ApiRequestable<void>`)
|
||||
final Type? responseType;
|
||||
|
||||
/// 请求类型(决定 header 处理方式,默认 request)
|
||||
final ApiRequestType requestType;
|
||||
@@ -57,7 +64,7 @@ class ApiRequest {
|
||||
const ApiRequest({
|
||||
required this.path,
|
||||
this.method = HttpMethod.post,
|
||||
required this.responseType,
|
||||
this.responseType,
|
||||
this.requestType = ApiRequestType.request,
|
||||
this.includeToken,
|
||||
this.customHeaders,
|
||||
|
||||
31
packages/networks_sdk/lib/src/annotations/api_response.dart
Normal file
31
packages/networks_sdk/lib/src/annotations/api_response.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
/// Response DTO 无需任何注解。
|
||||
///
|
||||
/// 生成器从 `@ApiRequest(responseType: T)` 声明出发,自动推导并生成
|
||||
/// `_$TFromJson` 反序列化函数,以及所有嵌套自定义类型的 `fromJson`。
|
||||
///
|
||||
/// ## 使用方式(只需标注 Request 类)
|
||||
///
|
||||
/// ```dart
|
||||
/// // Request:唯一需要注解的地方
|
||||
/// @ApiRequest(path: ApiPaths.authLogin, responseType: LoginResponse)
|
||||
/// class LoginRequest extends ApiRequestable<LoginResponse>
|
||||
/// with _$LoginRequestApi {
|
||||
/// final String email;
|
||||
/// final String password;
|
||||
/// LoginRequest({required this.email, required this.password});
|
||||
/// }
|
||||
///
|
||||
/// // Response:纯 Dart 类,零注解,零样板
|
||||
/// class LoginResponse {
|
||||
/// @JsonKey(name: 'access_token') final String accessToken;
|
||||
/// final LoginProfile profile;
|
||||
/// const LoginResponse({required this.accessToken, required this.profile});
|
||||
/// // 生成器自动提供 _$LoginResponseFromJson
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 此文件保留作占位符,供未来扩展使用(如自定义序列化策略)。
|
||||
/// 当前版本 Response 类无需标注任何注解,所有 fromJson 由生成器自动推导。
|
||||
class ApiResponse {
|
||||
const ApiResponse();
|
||||
}
|
||||
Reference in New Issue
Block a user