Skip to main content

InversifyFastifyHttpAdapter

The Fastify HTTP adapter implementation. This adapter allows you to use InversifyJS framework with Fastify applications, providing high performance and low overhead.

Base class documentation

This adapter extends InversifyHttpAdapter. See the base class documentation for information about common methods like applyGlobalMiddleware, useGlobalFilters, applyGlobalGuards, useGlobalInterceptors, and useGlobalPipe.

Installation

npm install @inversifyjs/http-fastify fastify inversify reflect-metadata

Constructor

constructor(
container: Container,
httpAdapterOptions?: FastifyHttpAdapterOptions,
customApp?: FastifyInstance,
)

Creates a Fastify adapter instance.

Parameters

  • container: The Inversify container that holds controllers, guards, pipes, middleware, interceptors, and filters.
  • httpAdapterOptions (optional): Configuration options for the adapter. See Options below.
  • customApp (optional): A custom Fastify instance. If not provided, a default Fastify instance is created.

Default Options

The adapter uses these defaults if not overridden:

{
logger: true,
useCookies: false,
useFormUrlEncoded: false,
useMultipartFormData: false,
}

Methods

build

async build(): Promise<FastifyInstance>

Builds and returns the Fastify instance with all configured routes, middleware, and handlers.

Returns: The Fastify FastifyInstance ready to be used with fastify.listen().

Options

FastifyHttpAdapterOptions

interface FastifyHttpAdapterOptions extends HttpAdapterOptions {
useCookies?: boolean;
useFormUrlEncoded?: boolean;
useMultipartFormData?:
| boolean
| FastifyMultipartAttachFieldsToBodyOptions
| FastifyMultipartOptions;
}

Properties

  • logger (inherited): Set to true to log route mappings on build, or false to disable logging, or provide a custom logger implementing the Logger interface from @inversifyjs/logger.
  • useCookies: Enable cookie parsing with @fastify/cookie plugin. Default: false
  • useFormUrlEncoded: Enable form URL-encoded body parsing with @fastify/formbody plugin. Default: false
  • useMultipartFormData: Enable multipart/form-data parsing with @fastify/multipart plugin. Can be:
    • false: Disabled (default)
    • true: Enabled with default options
    • Configuration object: Custom options for the multipart plugin

Usage Example

const container: Container = new Container();
// ... bind your controllers, services, etc.

// Create the adapter with options
const adapter: InversifyFastifyHttpAdapter = new InversifyFastifyHttpAdapter(
container,
{
logger: true,
useCookies: true,
useFormUrlEncoded: true,
useMultipartFormData: true,
},
);

// Build the Fastify instance
const app: FastifyInstance = await adapter.build();

// Start the server
await app.listen({ host: '0.0.0.0', port: 3000 });
console.log('Server listening on port 3000');

Fastify-Specific Features

High Performance

Fastify is one of the fastest Node.js web frameworks. The InversifyJS adapter maintains this performance while providing the full DI framework capabilities.

Schema Validation

While InversifyJS provides its own validation through pipes, you can also leverage Fastify's built-in JSON Schema validation for additional performance.

Async/Await Support

The adapter fully supports async/await patterns and properly handles promise rejections in route handlers.

Type Parameters

The adapter is strongly typed with Fastify types:

InversifyFastifyHttpAdapter extends InversifyHttpAdapter<
InversifyFastifyRequest, // Fastify Request
InversifyFastifyReply, // Fastify Reply
() => void, // Next function
void, // Handler return type
FastifyHttpAdapterOptions
>

See Also