ResponseHandler class provides a fluent API for creating type-safe JSON, binary, text, and error responses. Use createResponseHandler to instantiate a handler with custom options.
createResponseHandler
Factory function to create aResponseHandler instance with custom configuration.
Parameters
Configuration object or function that receives option builders and returns configuration.
meta- Metadata options for responsesjson- JSON response configurationbinary- Binary response configurationerror- Error handling configuration
Returns
Returns a configuredResponseHandler instance.
ResponseHandler Class
The main response handler class that provides methods for creating different types of responses.Constructor
Prefer usingcreateResponseHandler instead of calling the constructor directly.
Handler configuration options
Optional pre-defined error registry
Optional meta merged into each response
Methods
json()
Create a JSON response with automatic serialization and validation.JSON-serializable data to include in the response body
Optional response configuration
status- HTTP status code (default: 200)statusText- HTTP status textheaders- Additional HTTP headers
JsonResponse.Base<IInput> - JSON response instance
binary()
Create a binary response for files, images, or streams.Binary payload:
Blob, ArrayBuffer, Uint8Array, or ReadableStreamOptional response configuration
status- HTTP status code (default: 200)statusText- HTTP status textheaders- Additional HTTP headers
BinaryResponse.Base - Binary response instance
text()
Create a plain text response.Raw text response body
Optional response configuration
TextResponse.Base - Text response instance
fail()
Create an error response by name. Supports both built-in default errors and custom errors defined viadefineError.
Error name (registered via
defineError or built-in default type)Built-in error types:unauthorized- 401 Unauthorizedforbidden- 403 ForbiddennotFound- 404 Not FoundbadRequest- 400 Bad Requestconflict- 409 ConflicttooMany- 429 Too Many Requestsinternal- 500 Internal Server Error
Optional error payload to validate (if error has an input schema)
ErrorResponse.Base - Error response instance
defineError()
Define a custom error handler. Returns a newResponseHandler instance with the error registered.
Error name to register
Error handler function or static error output object
Error configuration
status- HTTP status codestatusText- HTTP status textinput- Input schema for validationvalidationType- Validation type (“parse” or “assert”)
ResponseHandler instance with the error registered
withMeta()
Create a new handler with preassigned metadata that will be merged into each response.Partial metadata that will be merged into each response
ResponseHandler instance with preassigned metadata