Response Schema
Fastify serializes responses using fast-json-stringfy
instead of JSON.stringfy()
when a json schema is defined.
This schema must be JSON Schema Draft 7.
In frourio, you can specify this as schamas
in ServerMethodHandler
to speed up serialization not only in Fastify (frourio
) but also in Express (frourio-express
).
See the Fastify documentation for response schema definitions.
controller.ts
import { defineController } from './$relay';
export default defineController(() => ({
get: () => ({ status: 200, body: 'Hello' }),
post: {
validators: {
query: z.object({ ... }),
body: z.object({ ... }),
},
schemas: {
response: {
201: {
type: 'object',
properties: { id: { type: 'number' }, port: { type: 'string' } }
}
}
},
hooks: {
preValidation: [],
preHandler: (req, _, done) => { ...; done(); },
},
handler: ({ body }) => ({ status: 201, body: { ... } }),
},
}));