serverpod-endpoints
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseServerpod Endpoints
Serverpod 端点
Extend with instance methods; first parameter is , return (or for real-time data streaming). Place anywhere under server . If is not running with hot-reload, run to update the client.
EndpointSessionFuture<T>Stream<T>lib/serverpod startserverpod generate通过实例方法扩展;第一个参数为,返回(若为实时数据流则返回)。可放置在服务端目录下的任意位置。如果未启用热重载,请运行来更新客户端。
EndpointSessionFuture<T>Stream<T>lib/serverpod startserverpod generateDefining an endpoint
定义端点
dart
import 'package:serverpod/serverpod.dart';
class ExampleEndpoint extends Endpoint {
Future<String> hello(Session session, String name) async {
return 'Hello $name';
}
}Client name is derived from the class name minus suffix ( → ).
EndpointExampleEndpointexampledart
import 'package:serverpod/serverpod.dart';
class ExampleEndpoint extends Endpoint {
Future<String> hello(Session session, String name) async {
return 'Hello $name';
}
}客户端名称由类名去掉后缀派生而来( → )。
EndpointExampleEndpointexampleCalling from the client
从客户端调用
dart
var result = await client.example.hello('World');Client initialized once:
dart
final serverUrl = await getServerUrl();
client = Client(serverUrl)
// When using Flutter:
..connectivityMonitor = FlutterConnectivityMonitor()
// When using authentication:
..authSessionManager = FlutterAuthSessionManager();dart
var result = await client.example.hello('World');客户端只需初始化一次:
dart
final serverUrl = await getServerUrl();
client = Client(serverUrl)
// 使用Flutter时:
..connectivityMonitor = FlutterConnectivityMonitor()
// 使用身份验证时:
..authSessionManager = FlutterAuthSessionManager();Supported parameter and return types
支持的参数和返回类型
- Primitives: ,
bool,int,doubleString - ,
Duration(UTC),DateTime,ByteData,UuidValue,UriBigInt - Generated serializable models (from )
.spy.yaml - ,
List,Map,Set— strictly typed with the aboveRecord
Default request size limit: 512 kB. Change with in config. Use the file upload API for large files.
maxRequestSize- 基本类型:、
bool、int、doubleString - 、
Duration(UTC)、DateTime、ByteData、UuidValue、UriBigInt - 生成的可序列化模型(来自)
.spy.yaml - 、
List、Map、Set—— 需严格使用上述类型进行类型标注Record
默认请求大小限制:512 kB。可通过配置中的修改。大文件请使用文件上传API。
maxRequestSizeSession
Session
Provides: database access (, ), cache (), logging, request context. Do not capture for use after the request completes.
session.dbModel.dbsession.caches提供:数据库访问(、)、缓存()、日志记录、请求上下文。请勿在请求完成后捕获并使用Session。
session.dbModel.dbsession.cachesExcluding from code generation
排除代码生成
- Entire endpoint: on the class.
@doNotGenerate - Single method: on the method.
@doNotGenerate
- 整个端点: 在类上添加注解。
@doNotGenerate - 单个方法: 在方法上添加注解。
@doNotGenerate
Endpoint inheritance
端点继承
- Concrete extends concrete: Client gets both; subclass exposes own + inherited methods.
- Abstract endpoint: Not registered; only concrete subclass is exposed.
- Parent with : Parent hidden; subclass gets a client implementing inherited methods.
@doNotGenerate
Overriding is allowed: same signature, different behavior, client code unchanged.
- 具体类继承具体类: 客户端会获取两者的方法;子类会暴露自身及继承的方法。
- 抽象端点: 不会被注册;仅具体子类会被暴露。
- 父类带有: 父类会被隐藏;子类的客户端会实现继承的方法。
@doNotGenerate
允许重写:签名相同,行为不同,客户端代码无需修改。
Backward compatibility
向后兼容性
Older app versions may still call your server. Do not rename parameters (REST API passes by name). Do not delete methods, add required parameters, or change signatures; add new methods or optional named parameters instead.
When you must break an endpoint's API, create a versioned endpoint:
dart
('Use TeamV2Endpoint instead')
class TeamEndpoint extends Endpoint {
Future<TeamInfo> join(Session session) async { /* ... */ }
}
class TeamV2Endpoint extends TeamEndpoint {
Future<TeamInfo> join(Session session) async => throw UnimplementedError();
Future<NewTeamInfo> joinWithCode(Session session, String invitationCode) async {
// New implementation
}
}Old clients use ; new clients use . Remove the old endpoint after all clients upgrade. Alternative: extract logic into a helper class callable from both endpoints.
client.team.join()client.teamV2.joinWithCode(...)旧版本应用可能仍会调用你的服务端。请勿重命名参数(REST API按名称传递参数)。请勿删除方法、添加必填参数或修改签名;应添加新方法或可选命名参数。
当必须打破端点API时,请创建版本化端点:
dart
('Use TeamV2Endpoint instead')
class TeamEndpoint extends Endpoint {
Future<TeamInfo> join(Session session) async { /* ... */ }
}
class TeamV2Endpoint extends TeamEndpoint {
Future<TeamInfo> join(Session session) async => throw UnimplementedError();
Future<NewTeamInfo> joinWithCode(Session session, String invitationCode) async {
// 新实现
}
}旧客户端使用;新客户端使用。待所有客户端升级后再移除旧端点。替代方案:将逻辑提取到可从两个端点调用的辅助类中。
client.team.join()client.teamV2.joinWithCode(...)