极简接口定义和响应定义,支持更多的解析器
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();
|
||||
}
|
||||
|
||||
@@ -12,17 +12,20 @@ part 'login_request.g.dart';
|
||||
///
|
||||
/// ```
|
||||
/// AuthRepositoryImpl.login(email, password)
|
||||
/// → _client.executeRequest( ★ LoginRequest ★ ) ← 你在这里
|
||||
/// → _client.executeRequest( ★ LoginRequest ★ ) ← 你在这里
|
||||
/// → 服务端 POST /auth/login
|
||||
/// → 响应 JSON → ★ LoginResponse ★ ← 也在这里
|
||||
/// → LoginResponse.toEntity() → User
|
||||
/// → SDK 内部 ApiResponseWrapper 拆包 { code, message, data }
|
||||
/// → ★ LoginResponse ★ = data 字段,T in APIResponseWrapper<T> ← 也在这里
|
||||
/// → LoginResponse.toEntity() → User
|
||||
/// ```
|
||||
|
||||
// ─────────────────────────────────────────────
|
||||
// Response DTO
|
||||
// ─────────────────────────────────────────────
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
/// 登录响应中的用户档案,嵌套在 [LoginResponse.profile] 中。
|
||||
///
|
||||
/// 纯 Dart 类,无需任何注解。`_$LoginProfileFromJson` 由生成器从 `@ApiRequest` 声明中自动推导生成。
|
||||
class LoginProfile {
|
||||
final int uid;
|
||||
final String uuid;
|
||||
@@ -70,9 +73,6 @@ class LoginProfile {
|
||||
required this.hint,
|
||||
});
|
||||
|
||||
factory LoginProfile.fromJson(Map<String, dynamic> json) =>
|
||||
_$LoginProfileFromJson(json);
|
||||
|
||||
User toEntity() => User(
|
||||
uid: uid,
|
||||
uuid: uuid,
|
||||
@@ -94,8 +94,11 @@ class LoginProfile {
|
||||
);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false, explicitToJson: true)
|
||||
class LoginData {
|
||||
/// 登录接口的业务响应数据(对应服务端 `data` 字段,即 T in `APIResponseWrapper<T>`)。
|
||||
///
|
||||
/// `{ code, message }` 由 SDK 内部的 `ApiResponseWrapper` 统一处理,
|
||||
/// App 层只接触此类,不感知 envelope 结构。纯 Dart 类,无需任何注解。
|
||||
class LoginResponse {
|
||||
@JsonKey(name: 'account_id')
|
||||
final String accountId;
|
||||
final LoginProfile profile;
|
||||
@@ -109,7 +112,7 @@ class LoginData {
|
||||
@JsonKey(name: 'login_data')
|
||||
final String loginData;
|
||||
|
||||
const LoginData({
|
||||
const LoginResponse({
|
||||
required this.accountId,
|
||||
required this.profile,
|
||||
required this.nonce,
|
||||
@@ -119,31 +122,9 @@ class LoginData {
|
||||
required this.loginData,
|
||||
});
|
||||
|
||||
factory LoginData.fromJson(Map<String, dynamic> json) =>
|
||||
_$LoginDataFromJson(json);
|
||||
|
||||
User toEntity() => profile.toEntity();
|
||||
}
|
||||
|
||||
/// Top-level envelope: { "code": 0, "message": "OK", "data": { ... } }
|
||||
@JsonSerializable(createToJson: false, explicitToJson: true)
|
||||
class LoginResponse {
|
||||
final int code;
|
||||
final String message;
|
||||
final LoginData data;
|
||||
|
||||
const LoginResponse({
|
||||
required this.code,
|
||||
required this.message,
|
||||
required this.data,
|
||||
});
|
||||
|
||||
factory LoginResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$LoginResponseFromJson(json);
|
||||
|
||||
User toEntity() => data.toEntity();
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────
|
||||
// Request
|
||||
// ─────────────────────────────────────────────
|
||||
@@ -152,9 +133,7 @@ class LoginResponse {
|
||||
///
|
||||
/// `@ApiRequest` 一个注解搞定一切:
|
||||
/// - mixin 自动生成 path / method / requestType / includeToken / toJson
|
||||
/// - toJson 只序列化类自身字段(email, password),不含继承属性
|
||||
/// - Response 的 fromJson 在 parameters getter 中自动注册
|
||||
/// - 无需 @JsonSerializable,无需手写 fromJson / toJson
|
||||
/// - parameters getter 自动注册 `_$LoginResponseFromJson` 到 SDK 全局注册表
|
||||
@ApiRequest(
|
||||
path: ApiPaths.authLogin,
|
||||
method: HttpMethod.post,
|
||||
|
||||
@@ -2,14 +2,14 @@ import 'package:networks_sdk/networks_sdk.dart';
|
||||
|
||||
import '../../../core/foundation/api_paths.dart';
|
||||
|
||||
/// # /auth/logout — 登出接口(无响应数据示例)
|
||||
part 'logout_request.g.dart';
|
||||
|
||||
/// # /auth/logout — 登出接口(无响应数据)
|
||||
///
|
||||
/// 演示:POST 请求 + 无 Response DTO 的模式。
|
||||
/// 服务端返回 `{"code": 0, "message": "ok"}` 无 data 字段,
|
||||
/// `executeRequest` 返回 null,调用方直接 await 即可。
|
||||
///
|
||||
/// 此接口不使用 @ApiRequest 注解,直接实现 ApiRequestable,
|
||||
/// 演示手动实现方式(适用于不需要代码生成器的简单接口)。
|
||||
/// `responseType` 省略 → 生成器跳过 `fromJson` 注册,mixin 泛型为 `void`。
|
||||
///
|
||||
/// ## 数据流位置
|
||||
///
|
||||
@@ -17,16 +17,9 @@ import '../../../core/foundation/api_paths.dart';
|
||||
/// AuthRepositoryImpl.logout()
|
||||
/// → _client.executeRequest( ★ LogoutRequest ★ ) ← 你在这里
|
||||
/// → 服务端 POST /auth/logout
|
||||
/// → 响应 {"code": 0, "message": "ok"} → null
|
||||
/// → 响应 {"code": 0, "message": "ok"} → null(无 data)
|
||||
/// ```
|
||||
class LogoutRequest extends ApiRequestable<void> {
|
||||
@override
|
||||
String get path => ApiPaths.authLogout;
|
||||
|
||||
@override
|
||||
HttpMethod get method => HttpMethod.post;
|
||||
|
||||
/// 登出不需要请求体参数
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {};
|
||||
@ApiRequest(path: ApiPaths.authLogout, method: HttpMethod.post)
|
||||
class LogoutRequest extends ApiRequestable<void> with _$LogoutRequestApi {
|
||||
LogoutRequest();
|
||||
}
|
||||
|
||||
@@ -32,8 +32,7 @@ part 'upload_file_request.g.dart';
|
||||
// Response DTO
|
||||
// ─────────────────────────────────────────────
|
||||
|
||||
/// 文件上传响应 DTO(只需反序列化,禁止生成无用的 toJson)
|
||||
@JsonSerializable(createToJson: false)
|
||||
/// 文件上传接口的业务响应数据(对应服务端 `data` 字段)。纯 Dart 类,无需任何注解。
|
||||
class UploadResult {
|
||||
final String url;
|
||||
|
||||
@@ -41,9 +40,6 @@ class UploadResult {
|
||||
final String fileId;
|
||||
|
||||
const UploadResult({required this.url, required this.fileId});
|
||||
|
||||
factory UploadResult.fromJson(Map<String, dynamic> json) =>
|
||||
_$UploadResultFromJson(json);
|
||||
}
|
||||
|
||||
// ═════════════════════════════════════════════
|
||||
|
||||
Reference in New Issue
Block a user