HTTP Interceptors
4.0.0 uses request/reply functions and native Fastify hooks. Express next() middleware is no longer supported.
import 'reflect-metadata';
import {Rest, Get, Interceptors, type FastifyInterceptor} from '@heronjs/express';
const requestId: FastifyInterceptor = (request, reply) => {
reply.header('x-request-id', request.id);
};
@Rest('/health')
@Interceptors([requestId])
export class HealthController {
@Get({uri: '/'})
health() {
return {ok: true};
}
}@Interceptors applies at class or method level. Register the controller in @Module({controllers: [...]}). Interceptors support values, Promises and Observables; finite interceptor execution is awaited.
For global behavior, pass options.interceptors to app.listen(). options.globalError accepts a FastifyErrorInterceptor with (error, request, reply) arguments. Return a sanitized response rather than exposing internal exception details.
Use options.hooks(instance) and instance.addHook(...) for native Fastify lifecycle hooks. Third-party integrations belong in Fastify plugins. See configuration and migration.
Last updated on