网络请求打通,ws 打通
This commit is contained in:
@@ -97,7 +97,24 @@ class ApiRequestGenerator extends GeneratorForAnnotation<ApiRequest> {
|
||||
}
|
||||
|
||||
final className = element.name!;
|
||||
final path = annotation.read('path').stringValue;
|
||||
|
||||
// 尝试保留原始常量引用(如 ApiPaths.authSendOtp),
|
||||
// 这样修改 ApiPaths 里的值不需要重新跑 gen。
|
||||
// 若注解传的是字面量字符串,则回退为带引号字符串。
|
||||
final pathObject = annotation.read('path').objectValue;
|
||||
final pathVariable = pathObject.variable;
|
||||
final String pathExpression;
|
||||
if (pathVariable != null) {
|
||||
final enclosing = pathVariable.enclosingElement;
|
||||
if (enclosing is ClassElement) {
|
||||
pathExpression = '${enclosing.name}.${pathVariable.name!}';
|
||||
} else {
|
||||
pathExpression = pathVariable.name!;
|
||||
}
|
||||
} else {
|
||||
final pathValue = pathObject.toStringValue()!;
|
||||
pathExpression = "'$pathValue'";
|
||||
}
|
||||
|
||||
// 读取 HttpMethod 枚举值
|
||||
final methodName = _readEnumName(
|
||||
@@ -133,17 +150,21 @@ class ApiRequestGenerator extends GeneratorForAnnotation<ApiRequest> {
|
||||
|
||||
// 有响应类型:parameters getter 中注册 fromJson(使用生成的私有函数)
|
||||
// ApiResponseGenerator 在同一 .g.dart 中生成 _$XFromJson,同 library 可访问
|
||||
// 无响应类型(void):跳过注册,直接返回 super.parameters
|
||||
final parametersBody = hasResponseType
|
||||
? ''' registerResponse<$responseTypeName>(_\$${responseTypeName}FromJson);
|
||||
return super.parameters;'''
|
||||
: ' return super.parameters;';
|
||||
// 无响应类型(void):无需注册,不生成 parameters getter(避免 unnecessary_override)
|
||||
final parametersGetter = hasResponseType
|
||||
? '''
|
||||
@override
|
||||
Map<String, dynamic>? get parameters {
|
||||
registerResponse<$responseTypeName>(_\$${responseTypeName}FromJson);
|
||||
return super.parameters;
|
||||
}'''
|
||||
: '';
|
||||
|
||||
return '''
|
||||
/// Generated by @ApiRequest for [$className]
|
||||
mixin _\$${className}Api on ApiRequestable<$responseTypeName> {
|
||||
@override
|
||||
String get path => '$path';
|
||||
String get path => $pathExpression;
|
||||
@override
|
||||
HttpMethod get method => HttpMethod.$methodName;
|
||||
@override
|
||||
@@ -151,11 +172,7 @@ mixin _\$${className}Api on ApiRequestable<$responseTypeName> {
|
||||
@override
|
||||
bool get includeToken => $includeToken;
|
||||
@override
|
||||
Map<String, dynamic> toJson() => $toJsonBody;
|
||||
@override
|
||||
Map<String, dynamic>? get parameters {
|
||||
$parametersBody
|
||||
}
|
||||
Map<String, dynamic> toJson() => $toJsonBody;$parametersGetter
|
||||
}
|
||||
''';
|
||||
}
|
||||
|
||||
@@ -190,6 +190,9 @@ $params );
|
||||
if (type.isDartCoreDouble) return '$access as double$q';
|
||||
if (type.isDartCoreNum) return '$access as num$q';
|
||||
|
||||
// Map<String, dynamic>:已经是 JSON Map,直接 cast,无需 fromJson
|
||||
if (type.isDartCoreMap) return '$access as Map<String, dynamic>$q';
|
||||
|
||||
// 嵌套对象:调用同一 part 文件中生成的 _$TypeFromJson 私有函数
|
||||
if (type is InterfaceType) {
|
||||
final typeName = type.element.name!;
|
||||
|
||||
Reference in New Issue
Block a user