response library
Responses: what a handler answers with, and how failures become status codes.
Import this for a type that turns itself into a response, or for the encoders generated handlers call.
import 'package:dust_server/response.dart';
final class NotFound implements IntoResponse {
const NotFound(this.message);
final String message;
@override
Response intoResponse() => jsonResponse({'error': message}, status: 404);
}
Everything here is also exported from package:dust_server/server.dart.
Classes
- IntoResponse
- A value that can turn itself into an HTTP Response.
- Redirect
- Sends the client somewhere else.
- Rejection
- A typed extraction failure.
- Response
- The response returned by a Handler.
- ServerErrors
- Where errors that escape a handler are reported.
- ServerSentEvent
- One server-sent event.
Extensions
- ValidateOrReject on Validatable
- Validating a value at the point a handler uses it.
- ValidationRejection on ValidationResult
-
Turns the outcome of a
Validate()derive into a rejection.
Functions
-
bytesResponse(
Uint8List value, {int status = 200}) → Response -
Encodes
valueas an octet-stream response. -
decodeRejection(
Object error, {required String subject}) → Rejection -
The 422 for an
errorthrown while building a value out ofsubject. -
eventStream(
Stream< ServerSentEvent> events, {Duration? keepAlive = const Duration(seconds: 15), int status = 200}) → Response -
Streams
eventsto the client astext/event-stream. -
groupValidationErrors(
Iterable< ValidationError> errors) → Map<String, List< String> > -
Groups validation
errorsby the field they belong to, in report order. -
guard(
Future< Response> body()) → Future<Response> -
Runs
body, turning a thrown Rejection into its own response and anything else into a 500 with no detail in it. -
htmlResponse(
String markup, {int status = 200}) → Response -
Answers with
markupas an HTML document. -
jsonResponse(
Object? value, {int status = 200}) → Response -
Encodes
valueas a JSON response. -
noContent(
) → Response - An empty 204 response.
-
render(
TemplateEngine engine, String template, Map< String, Object?> values, {int status = 200}) → Response -
Renders
templatewithvaluesand answers with the result. -
responseFrom(
Object? value, {int status = 200}) → Response - Turns whatever a typed handler returned into a response.
-
streamed(
Stream< List< body, {int status = 200, String contentType = 'application/octet-stream', Map<int> >String, String> headers = const {}}) → Response -
Answers with
bodyas it arrives, without buffering it. -
streamResponse(
Stream< List< value, {int status = 200}) → Responseint> > -
Streams
valueas an octet-stream response. -
textResponse(
String value, {int status = 200}) → Response -
Encodes
valueas a plain-text response.