极简接口定义和响应定义,支持更多的解析器
This commit is contained in:
@@ -6,28 +6,30 @@ import '../../../domain/entities/user.dart';
|
||||
|
||||
part 'get_profile_request.g.dart';
|
||||
|
||||
/// # /user/profile — 获取用户资料(GET 请求示例)
|
||||
/// # /user/profile — 获取用户资料(GET 请求)
|
||||
///
|
||||
/// 演示:GET 请求 + 无 body 参数的模式。
|
||||
/// GET 请求的 toJson() 结果会自动作为 URL query parameters 发送。
|
||||
/// GET 请求无 body,`toJson()` 结果自动作为 URL query parameters 发送。
|
||||
/// 如需 query 参数(如分页),直接在类中添加字段,生成器自动序列化。
|
||||
///
|
||||
/// ## 数据流位置
|
||||
///
|
||||
/// ```
|
||||
/// UserRepositoryImpl.getProfile()
|
||||
/// → _client.executeRequest( ★ GetProfileRequest ★ ) ← 你在这里
|
||||
/// → _client.executeRequest( ★ GetProfileRequest ★ ) ← 你在这里
|
||||
/// → 服务端 GET /user/profile
|
||||
/// → 响应 JSON → ★ ProfileData ★ ← 也在这里
|
||||
/// → ProfileData.toEntity() → User
|
||||
/// → SDK 内部 ApiResponseWrapper 拆包 { code, message, data }
|
||||
/// → ★ ProfileResponse ★ = data 字段 ← 也在这里
|
||||
/// → ProfileResponse.toEntity() → User
|
||||
/// ```
|
||||
|
||||
// ─────────────────────────────────────────────
|
||||
// Response DTO
|
||||
// ─────────────────────────────────────────────
|
||||
|
||||
/// 用户资料响应 DTO(只需反序列化,禁止生成无用的 toJson)
|
||||
@JsonSerializable(createToJson: false)
|
||||
class ProfileData {
|
||||
/// 用户资料接口的业务响应数据(对应服务端 `data` 字段)。
|
||||
///
|
||||
/// `{ code, message }` 由 SDK 内部的 `ApiResponseWrapper` 统一处理。纯 Dart 类,无需任何注解。
|
||||
class ProfileResponse {
|
||||
final int uid;
|
||||
final String uuid;
|
||||
@JsonKey(name: 'last_online')
|
||||
@@ -54,7 +56,7 @@ class ProfileData {
|
||||
final int channelGroupId;
|
||||
final String hint;
|
||||
|
||||
const ProfileData({
|
||||
const ProfileResponse({
|
||||
required this.uid,
|
||||
required this.uuid,
|
||||
required this.lastOnline,
|
||||
@@ -74,10 +76,6 @@ class ProfileData {
|
||||
required this.hint,
|
||||
});
|
||||
|
||||
factory ProfileData.fromJson(Map<String, dynamic> json) =>
|
||||
_$ProfileDataFromJson(json);
|
||||
|
||||
/// DTO → Domain Entity
|
||||
User toEntity() => User(
|
||||
uid: uid,
|
||||
uuid: uuid,
|
||||
@@ -104,16 +102,12 @@ class ProfileData {
|
||||
// ─────────────────────────────────────────────
|
||||
|
||||
/// 获取用户资料请求(GET,无参数)
|
||||
///
|
||||
/// GET 请求无 body,mixin 自动生成 toJson() → 空 map。
|
||||
/// 如需 query 参数(如分页),添加字段即可,
|
||||
/// toJson() 会自动将字段序列化为 URL query string。
|
||||
@ApiRequest(
|
||||
path: ApiPaths.userProfile,
|
||||
method: HttpMethod.get,
|
||||
responseType: ProfileData,
|
||||
responseType: ProfileResponse,
|
||||
)
|
||||
class GetProfileRequest extends ApiRequestable<ProfileData>
|
||||
class GetProfileRequest extends ApiRequestable<ProfileResponse>
|
||||
with _$GetProfileRequestApi {
|
||||
GetProfileRequest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user