Fastify APIs and validation
Names and methods
src/server/api/health.get.ts becomes GET /api/health. Supported suffixes are get, post, put, patch, delete, head and options. [name] creates a dynamic parameter; files without a method remain helpers and are not registered.
Create a POST endpoint
import { defineApiRoute } from '@fastang/core';
export default defineApiRoute<{ Body: { text: string } }>({
schema: {
body: {
type: 'object', required: ['text'], additionalProperties: false,
properties: { text: { type: 'string', minLength: 1, maxLength: 120 } },
},
response: {
201: {
type: 'object', required: ['message'],
properties: { message: { type: 'string' } },
},
},
},
handler: async (request, reply) => reply.code(201).send({ message: request.body.text.trim() }),
});Try the response
curl -i -X POST http://localhost:3278/api/messages \
-H 'content-type: application/json' \
-d '{"text":"Hello"}'{"message":"Hello"}. A missing text, an empty string or a value longer than 120 characters returns HTTP 400. TypeScript types alone do not validate external requests.
Use Fastify
request and reply: read parameters and headers or set the status. Register shared plugins and hooks in src/server/plugins.ts. The initial body limit is 1 MiB. Internal errors do not expose stacks to the client; unknown API endpoints return JSON with HTTP 404.
src/server modules into Angular components. Share types through a module without server dependencies and use type-only imports where possible. For shared schemas and inferred Angular requests, see typed API contracts.
Native route options
0.1.0-alpha.3, defineApiRoute() accepts native Fastify route options such as preHandler, hook arrays, onSend, bodyLimit, config, exposeHeadRoute and route error handlers. The following example uses these options.
import { defineApiRoute } from '@fastang/core';
export default defineApiRoute<{ Body: { text: string } }>({
bodyLimit: 64,
schema: {
body: {
type: 'object', required: ['text'],
properties: { text: { type: 'string' } },
},
},
preHandler: async request => { request.body.text = request.body.text.trim(); },
onSend: async (_request, reply, payload) => {
reply.header('x-route-example', 'limited');
return payload;
},
handler: async request => ({ message: request.body.text }),
});POST /api/limited trims the validated body and adds a response header. A JSON body larger than 64 bytes returns HTTP 413. Hook ordering, short-circuit replies, validation and error behavior remain Fastify's. Ordinary function hooks retain Fastify's this binding; arrow functions use normal JavaScript lexical binding.
defineContractRoute() accepts the same options, with request/reply types inferred in hooks, but its schema always comes from the shared contract. Its error replies must match the statuses declared there. Keep plugin-specific option types in the plugin's Fastify type augmentation. A second generic parameter can describe a custom route config object. Schema-less routes are allowed, as in native Fastify; TypeScript types alone never validate input.