Serializers
An app-wide or per-route hook that turns route() results into a Response and reads non-JSON request bodies.
A serializer is an object with two hooks:
interface Serializer {
serialize(result: unknown, event: RequestEvent): Response | null;
parseBody?(request: Request, contentType: string): Promise<unknown> | undefined;
}serializeturns the handler's return value into aResponse, or returnsnullto fall through to the default JSON serialization.parseBodyreads a request body for a content typeroute()does not handle natively, or returnsundefinedto fall through to the built-in JSON and form readers.
#Installing one
App-wide, through boot:
import { tronSerializer } from '@human-synthesis/norns-tron/server'
app := await boot { features, serializer: tronSerializer() }Per route, which overrides the app-wide one. null forces plain JSON:
export GET := route
serializer: tronSerializer({ schema: noteWire })
handler: ...
export POST := route
serializer: null
handler: ...setSerializer(s) and getSerializer() are the underlying functions; boot({ serializer }) calls the first. The serializer is looked up per request, so module evaluation order does not matter.
#The one that ships
tronSerializer() from norns-tron content-negotiates: clients that send Accept: application/tron get TRON, everyone else keeps getting JSON. Remove the option to roll back.