Initial project
This commit is contained in:
65
packages/networks_sdk/lib/src/annotations/api_request.dart
Normal file
65
packages/networks_sdk/lib/src/annotations/api_request.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
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
|
||||
/// 全部由生成器自动提供。
|
||||
///
|
||||
/// ## 使用方式
|
||||
///
|
||||
/// ```dart
|
||||
/// @ApiRequest(
|
||||
/// path: '/auth/login',
|
||||
/// method: HttpMethod.post,
|
||||
/// responseType: LoginData,
|
||||
/// requestType: ApiRequestType.login,
|
||||
/// )
|
||||
/// @JsonSerializable()
|
||||
/// class LoginRequest extends ApiRequestable<LoginData>
|
||||
/// 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)
|
||||
class ApiRequest {
|
||||
/// API 路径(如 `'/auth/login'`)
|
||||
final String path;
|
||||
|
||||
/// HTTP 方法(默认 POST)
|
||||
final HttpMethod method;
|
||||
|
||||
/// 响应类型(用于泛型绑定)
|
||||
final Type responseType;
|
||||
|
||||
/// 请求类型(决定 header 处理方式,默认 request)
|
||||
final ApiRequestType requestType;
|
||||
|
||||
/// 是否在 header 中包含 token(默认根据 requestType 推断:login → false,其余 → true)
|
||||
final bool? includeToken;
|
||||
|
||||
/// 自定义请求头
|
||||
final Map<String, String>? customHeaders;
|
||||
|
||||
const ApiRequest({
|
||||
required this.path,
|
||||
this.method = HttpMethod.post,
|
||||
required this.responseType,
|
||||
this.requestType = ApiRequestType.request,
|
||||
this.includeToken,
|
||||
this.customHeaders,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user