阿里·达特代码gen
设置
1) 将生成器添加到您的 arri 配置
// arri.config.ts
import { defineConfig, generators } from 'arri';
export default defineConfig({
generators: [
generators.dartClient({
clientName: 'MyClient',
outputFile: './client/src/my_client.g.dart',
}),
],
});
选项 :
| 名称 | 说明 |
|---|---|
| 输出文件( 需要) | 由生成器创建的文件路径 |
| 客户端Name | 生成客户端类的名称( 默认为“ 客户端 ” ) |
| 格式 | 运行 dart format 在生成的文件( 默认为 true(中文(简体) ) |
| 类型 前缀 | 在生成的类名中添加前缀 |
| 根服务 | 生成客户端的根服务 |
2) 安装 Dart 客户端库
生成的代码依赖于 Dart 客户端 图书馆。 所以在使用生成的代码时一定要安装它。 版本号应该与您的 Arri CLI 版本相匹配 。 (运行) arri version 检查).
dart pub add arri_client
使用生成代码
初始化客户端
// this will match whatever you put in the arri config
import "./my_client.g.dart";
main() async {
final client = MyClient(
baseUrl: "https://example.com",
headers: () async {
return {
"Authorization": "<some-token>",
};
},
);
await client.myProcedure();
}
Root客户端将是一个包含单一类中所有服务和程序的类. 如果您只需要特定的服务,您可以初始化该服务.
final service = MyClientUsersService(
baseUrl: "https://example.com",
headers: () async {
return {
"Authorization": "<some-token>",
};
},
);
客户端/ 服务选项
| 名称 | 类型 | 说明 |
|---|---|---|
| httpclient 语句 | http.Client | 使用此选项通过自定义 http.Client 实例 |
| 基数Url | String | 后端服务器的基础 URL |
| 页眉 | FutureOr<Map<String, String>> Function()? | 返回信头映射的函数 |
| 错误 | Function(Object)? | 当客户端出现任何错误时 就会起火. |
使用Arri模型
所有生成的模型都将是不可改变的. 他们将获得以下特征:
方法数字 :
Map<String, dynamic> toJson()String toJsonString()String toUrlQueryParams()copyWith()
工厂方法数字 :
empty()fromJson(Map<String, dynamic> input)fromJsonString(String input)
覆盖数字 :
==操作员(允许进行深度平等检查)hashMap(允许进行深度平等检查)toString(将打印出所有属性和值而不是)Instance of X(中文(简体) )
这个库是用 无。 。 。 .
发展
# build the library
pnpm nx build codegen-dart
# test
pnpm nx test codegen-dart
# lint
pnpm nx lint codegen-dart