Handlers
Handlers are middleware that enable enhancing, modifying, or responding to requests. They are best used for handling concerns shared by a large number of requests - whereas builders are better suited for customizing the details of a specific request.
Handlers are also the primary tool to use for massaging response data into a better format. For instance, the data returned by many RESTful APIs will need transformed into {json:api} in order to take advantage of features specific to polymorphism, caching, reactivity and relational mapping.
Below, we show an example of how to write a simple handler to transform a response containing only non-relational field.
json
{
"user_accounts": [
{
"id": 1,
"first_name": "Chris",
"last_name": "Thoburn",
"username": "runspired"
}
],
"_user_accounts": [
{
"id": 2,
"first_name": "Krystan",
"last_name": "HuffMenne",
"username": "gitKrystan"
}
],
}