Suvidha provides default handlers that manage the request/response lifecycle.
These handlers support Http classes and plain Protocol objects for responses.
You can customize the format of the response body using a formatter function.
The Formatter function is used to structure the final HTTP response body.
It takes the status code, body, and metadata as arguments, and returns the formatted response body.
The DefaultHandlers class implements the Handlers interface and provides default
implementations for handling errors, schema errors, successful responses, and post-response actions.
It sets the Content-Type to application/json by default.
If output is a Protocol object (but not an Http.End instance), itβs wrapped in an Http.End object.
If output is an Http.End instance, it sends the response with the specified status code, headers, and body (formatted by the formatter).
If output is a primitive type (string, number, boolean, undefined(converted to null), object, or null), it sends a 200 OK response with the output as the body (formatted by the formatter).
Otherwise, it logs an error and throws a Http.InternalServerError exception.
Copy
onComplete(output: unknown, conn: Conn): Promise<void> | void { // ... (logic to handle different output types and send the response)}
Called after a response has been sent (headers already sent). It is primarily used for logging or cleanup. The default implementation logs the response data.