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 value as an octet-stream response.
decodeRejection(Object error, {required String subject}) Rejection
The 422 for an error thrown while building a value out of subject.
eventStream(Stream<ServerSentEvent> events, {Duration? keepAlive = const Duration(seconds: 15), int status = 200}) Response
Streams events to the client as text/event-stream.
groupValidationErrors(Iterable<ValidationError> errors) Map<String, List<String>>
Groups validation errors by 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 markup as an HTML document.
jsonResponse(Object? value, {int status = 200}) Response
Encodes value as a JSON response.
noContent() Response
An empty 204 response.
render(TemplateEngine engine, String template, Map<String, Object?> values, {int status = 200}) Response
Renders template with values and answers with the result.
responseFrom(Object? value, {int status = 200}) Response
Turns whatever a typed handler returned into a response.
streamed(Stream<List<int>> body, {int status = 200, String contentType = 'application/octet-stream', Map<String, String> headers = const {}}) Response
Answers with body as it arrives, without buffering it.
streamResponse(Stream<List<int>> value, {int status = 200}) Response
Streams value as an octet-stream response.
textResponse(String value, {int status = 200}) Response
Encodes value as a plain-text response.