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. This alpha does not generate a route-typed API client yet.