A response handler takes the finished Response and writes it to the underlying HttpResponse: the status, the headers, the transfer encoding, compression, and the body stream. Revali's
DefaultResponseHandler covers normal HTTP responses, so you only need your own handler when you must control the bytes on the wire yourself.
Example#
lib/components/plain_response_handler.dart
import 'dart:io';
import 'package:revali_router/revali_router.dart';
class PlainResponseHandler implements ResponseHandler {
const PlainResponseHandler();
@override
Future<void> handle(
Response response,
RequestContext context,
HttpResponse httpResponse,
) async {
httpResponse.statusCode = response.statusCode;
// write headers and body to httpResponse...
await httpResponse.close();
}
}
Apply it as an annotation on the app, a controller, or an endpoint:
@PlainResponseHandler()
@App()
final class MyApp extends AppConfig {
// ...
}
Scoping#
- Only one response handler can be applied per app, controller, or endpoint.
-
The handler closest to the endpoint wins: endpoint, then controller, then app, then
DefaultResponseHandler. - WebSocket and server-sent event routes use their own built-in handlers, so a handler set on the app or controller does not apply to them.