or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

decorators.mdfastify-adapter.mdindex.mdinterfaces.md
tile.json

tessl/npm-nestjs--platform-fastify

Fastify-based HTTP adapter for the NestJS framework, enabling high-performance HTTP server integration with NestJS applications

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@nestjs/platform-fastify@11.1.x

To install, run

npx @tessl/cli install tessl/npm-nestjs--platform-fastify@11.1.0

index.mddocs/

NestJS Platform Fastify

The NestJS Platform Fastify package provides a Fastify-based HTTP adapter for the NestJS framework, enabling developers to leverage Fastify's high-performance HTTP server as an alternative to Express. It offers seamless integration with NestJS applications through adapters, decorators, and interfaces while maintaining the framework's architectural patterns and providing access to Fastify's superior performance characteristics.

Package Information

  • Package Name: @nestjs/platform-fastify
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @nestjs/platform-fastify

Core Imports

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';

For individual components:

import { 
  FastifyAdapter,
  NestFastifyApplication,
  RouteConfig,
  RouteConstraints,
  RouteSchema,
  FASTIFY_ROUTE_CONFIG_METADATA,
  FASTIFY_ROUTE_CONSTRAINTS_METADATA,
  FASTIFY_ROUTE_SCHEMA_METADATA
} from '@nestjs/platform-fastify';

Basic Usage

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import * as path from 'path';

async function bootstrap() {
  try {
    // Create NestJS application with Fastify adapter
    const app = await NestFactory.create<NestFastifyApplication>(
      AppModule,
      new FastifyAdapter()
    );
    
    // Enable CORS if needed
    app.enableCors();
    
    // Serve static assets
    app.useStaticAssets({
      root: path.join(__dirname, '..', 'public'),
      prefix: '/public/',
    });
    
    // Listen on port 3000
    await app.listen(3000);
    console.log('Application is running on: http://localhost:3000');
  } catch (error) {
    console.error('Error starting the application:', error);
    process.exit(1);
  }
}
bootstrap();

Architecture

The NestJS Platform Fastify package is built around several key components:

  • FastifyAdapter: Core HTTP adapter that bridges NestJS and Fastify, handling request/response lifecycle
  • Route Decorators: Specialized decorators for Fastify-specific route configuration, constraints, and schema validation
  • Application Interface: Enhanced NestJS application interface with Fastify-specific methods
  • Type Integration: Full TypeScript support with generic server, request, and response types
  • Plugin System: Direct access to Fastify's plugin ecosystem through the adapter

Capabilities

HTTP Adapter Integration

Core adapter functionality for integrating Fastify with NestJS applications. Provides lifecycle management, middleware support, and request/response handling.

class FastifyAdapter<
  TServer extends RawServerBase = RawServerDefault,
  TRawRequest extends FastifyRawRequest<TServer> = FastifyRawRequest<TServer>,
  TRawResponse extends RawReplyDefaultExpression<TServer> = RawReplyDefaultExpression<TServer>,
  TRequest extends FastifyRequest<RequestGenericInterface, TServer, TRawRequest> = FastifyRequest<RequestGenericInterface, TServer, TRawRequest>,
  TReply extends FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse> = FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse>,
  TInstance extends FastifyInstance<TServer, TRawRequest, TRawResponse> = FastifyInstance<TServer, TRawRequest, TRawResponse>
> extends AbstractHttpAdapter<TServer, TRequest, TReply>

interface FastifyAdapterConstructorOptions<
  Server extends RawServerBase = RawServerDefault,
  Logger extends FastifyBaseLogger = FastifyBaseLogger
> extends FastifyServerOptions<Server, Logger> {
  skipMiddie?: boolean;
}

HTTP Adapter

Route Configuration Decorators

Specialized decorators for configuring Fastify-specific route behavior including constraints, schema validation, and configuration options.

function RouteConfig(config: any): MethodDecorator;
function RouteConstraints(config: RouteShorthandOptions['config']): MethodDecorator;
function RouteSchema(schema: FastifySchema): MethodDecorator;

Route Decorators

Application and Configuration Interfaces

Enhanced interfaces for NestJS applications with Fastify-specific functionality and configuration options for static assets, view engines, and body parsing.

interface NestFastifyApplication<TServer extends RawServerBase = RawServerDefault> 
  extends INestApplication<TServer> {
  getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>;
  register<Options extends FastifyPluginOptions = any>(
    plugin: FastifyPluginCallback<Options> | FastifyPluginAsync<Options> | Promise<{ default: FastifyPluginCallback<Options> }> | Promise<{ default: FastifyPluginAsync<Options> }>,
    opts?: FastifyRegisterOptions<Options>
  ): Promise<FastifyInstance>;
  useBodyParser<TServer extends RawServerBase = RawServerBase>(
    type: string | string[] | RegExp,
    options?: NestFastifyBodyParserOptions,
    parser?: FastifyBodyParser<Buffer, TServer>
  ): this;
  useStaticAssets(options: FastifyStaticOptions): this;
  enableCors(options?: FastifyCorsOptions): void;
  setViewEngine(options: FastifyViewOptions | string): this;
}

Interfaces and Configuration

Types

Core Types

type NestFastifyBodyParserOptions = Omit<Parameters<AddContentTypeParser>[1], 'parseAs'>;

// Metadata constants for decorators
const FASTIFY_ROUTE_CONFIG_METADATA: string;
const FASTIFY_ROUTE_CONSTRAINTS_METADATA: string;
const FASTIFY_ROUTE_SCHEMA_METADATA: string;

Server Type Options

The adapter supports various Fastify server configurations:

type FastifyHttp2SecureOptions<
  Server extends http2.Http2SecureServer,
  Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
  http2: true;
  https: http2.SecureServerOptions;
};

type FastifyHttp2Options<
  Server extends http2.Http2Server,
  Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
  http2: true;
  http2SessionTimeout?: number;
};

type FastifyHttpsOptions<
  Server extends https.Server,
  Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
  https: https.ServerOptions;
};

type FastifyHttpOptions<
  Server extends http.Server,
  Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
  http: http.ServerOptions;
};