Fastify-based HTTP adapter for the NestJS framework, enabling high-performance HTTP server integration with NestJS applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Specialized decorators for configuring Fastify-specific route behavior including constraints, schema validation, and configuration options.
Sets Fastify route configuration metadata for enhanced route behavior and options.
/**
* Decorator for setting Fastify route configuration
* Applies configuration options that will be passed to Fastify's route registration
* @param config - Route configuration object
*
* @see https://fastify.dev/docs/latest/Reference/Routes/#config
*/
function RouteConfig(config: any): MethodDecorator;Usage Examples:
import { Controller, Get } from '@nestjs/common';
import { RouteConfig } from '@nestjs/platform-fastify';
@Controller('users')
export class UsersController {
@Get(':id')
@RouteConfig({
// Custom route-specific configuration
rateLimit: {
max: 100,
timeWindow: '1 minute'
},
// Can include any Fastify route config
custom: {
authRequired: true,
roles: ['admin', 'user']
}
})
getUser(@Param('id') id: string) {
// Route handler implementation
return { id, name: 'John Doe' };
}
@Post()
@RouteConfig({
bodyLimit: 1024 * 1024, // 1MB limit for this route
preHandler: async (request, reply) => {
// Custom pre-handler logic
}
})
createUser(@Body() userData: any) {
// Route handler implementation
return userData;
}
}Sets Fastify route constraints for advanced routing based on request properties.
/**
* Decorator for setting Fastify route constraints
* Enables advanced routing based on request properties like headers, host, etc.
* @param config - Route constraints configuration
*
* @see https://fastify.dev/docs/latest/Reference/Routes/#constraints
*/
function RouteConstraints(config: RouteShorthandOptions['config']): MethodDecorator;Usage Examples:
import { Controller, Get } from '@nestjs/common';
import { RouteConstraints } from '@nestjs/platform-fastify';
@Controller('api')
export class ApiController {
@Get('data')
@RouteConstraints({
// Route only matches for specific host
host: 'api.example.com'
})
getDataForApiDomain() {
return { message: 'API domain data' };
}
@Get('data')
@RouteConstraints({
// Route matches for different host
host: 'admin.example.com'
})
getDataForAdminDomain() {
return { message: 'Admin domain data' };
}
@Get('mobile-data')
@RouteConstraints({
// Route only matches for mobile user agents
headers: {
'user-agent': /Mobile|Android|iPhone/i
}
})
getMobileData() {
return { message: 'Mobile optimized data' };
}
@Get('version/:id')
@RouteConstraints({
// Route constraint based on custom logic
version: '2.0'
})
getVersionedData(@Param('id') id: string) {
return { id, version: '2.0', data: 'new format' };
}
}Sets Fastify route schema for automatic request/response validation using JSON Schema.
/**
* Decorator for setting Fastify route schema validation
* Enables automatic validation of request body, query parameters, route parameters, and response
* @param schema - JSON Schema definition for route validation
*
* @see https://fastify.dev/docs/latest/Reference/Routes/#routes-options
*/
function RouteSchema(schema: FastifySchema): MethodDecorator;
interface FastifySchema {
/** JSON Schema for request body validation */
body?: JsonSchema;
/** JSON Schema for query string validation */
querystring?: JsonSchema;
/** JSON Schema for query string validation (alias) */
query?: JsonSchema;
/** JSON Schema for route parameters validation */
params?: JsonSchema;
/** JSON Schema for response validation by status code */
response?: Record<number, JsonSchema>;
/** JSON Schema for request headers validation */
headers?: JsonSchema;
}
interface JsonSchema {
type?: string | string[];
properties?: Record<string, JsonSchema>;
required?: string[];
additionalProperties?: boolean;
items?: JsonSchema;
minimum?: number;
maximum?: number;
minLength?: number;
maxLength?: number;
pattern?: string;
enum?: any[];
format?: string;
[key: string]: any;
}Usage Examples:
import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common';
import { RouteSchema } from '@nestjs/platform-fastify';
@Controller('products')
export class ProductsController {
@Get(':id')
@RouteSchema({
// Validate route parameters
params: {
type: 'object',
required: ['id'],
properties: {
id: {
type: 'string',
pattern: '^[0-9]+$' // Only numeric IDs
}
}
},
// Validate response format
response: {
200: {
type: 'object',
required: ['id', 'name', 'price'],
properties: {
id: { type: 'string' },
name: { type: 'string' },
price: { type: 'number', minimum: 0 },
description: { type: 'string' },
category: { type: 'string' }
}
},
404: {
type: 'object',
required: ['error', 'message'],
properties: {
error: { type: 'string' },
message: { type: 'string' }
}
}
}
})
getProduct(@Param('id') id: string) {
return {
id,
name: 'Product Name',
price: 29.99,
description: 'Product description',
category: 'electronics'
};
}
@Get()
@RouteSchema({
// Validate query parameters
querystring: {
type: 'object',
properties: {
page: {
type: 'integer',
minimum: 1,
default: 1
},
limit: {
type: 'integer',
minimum: 1,
maximum: 100,
default: 10
},
category: {
type: 'string',
enum: ['electronics', 'clothing', 'books', 'home']
},
minPrice: {
type: 'number',
minimum: 0
},
maxPrice: {
type: 'number',
minimum: 0
}
}
},
// Validate response
response: {
200: {
type: 'object',
required: ['products', 'total', 'page'],
properties: {
products: {
type: 'array',
items: {
type: 'object',
required: ['id', 'name', 'price'],
properties: {
id: { type: 'string' },
name: { type: 'string' },
price: { type: 'number' }
}
}
},
total: { type: 'integer' },
page: { type: 'integer' },
totalPages: { type: 'integer' }
}
}
}
})
getProducts(
@Query('page') page = 1,
@Query('limit') limit = 10,
@Query('category') category?: string,
@Query('minPrice') minPrice?: number,
@Query('maxPrice') maxPrice?: number
) {
return {
products: [],
total: 0,
page,
totalPages: 0
};
}
@Post()
@RouteSchema({
// Validate request body
body: {
type: 'object',
required: ['name', 'price', 'category'],
additionalProperties: false,
properties: {
name: {
type: 'string',
minLength: 1,
maxLength: 100
},
price: {
type: 'number',
minimum: 0,
maximum: 10000
},
category: {
type: 'string',
enum: ['electronics', 'clothing', 'books', 'home']
},
description: {
type: 'string',
maxLength: 1000
},
tags: {
type: 'array',
items: {
type: 'string',
minLength: 1
},
maxItems: 10
}
}
},
// Validate request headers
headers: {
type: 'object',
required: ['content-type'],
properties: {
'content-type': {
type: 'string',
enum: ['application/json']
}
}
},
// Validate response
response: {
201: {
type: 'object',
required: ['id', 'name', 'price', 'category', 'createdAt'],
properties: {
id: { type: 'string' },
name: { type: 'string' },
price: { type: 'number' },
category: { type: 'string' },
description: { type: 'string' },
tags: {
type: 'array',
items: { type: 'string' }
},
createdAt: { type: 'string', format: 'date-time' }
}
},
400: {
type: 'object',
required: ['error', 'message'],
properties: {
error: { type: 'string' },
message: { type: 'string' },
details: {
type: 'array',
items: {
type: 'object',
properties: {
field: { type: 'string' },
message: { type: 'string' }
}
}
}
}
}
}
})
createProduct(@Body() productData: any) {
return {
id: 'new-product-id',
...productData,
createdAt: new Date().toISOString()
};
}
}Constants used internally by the decorators for storing metadata.
/** Metadata key for route configuration */
const FASTIFY_ROUTE_CONFIG_METADATA: string;
/** Metadata key for route constraints */
const FASTIFY_ROUTE_CONSTRAINTS_METADATA: string;
/** Metadata key for route schema */
const FASTIFY_ROUTE_SCHEMA_METADATA: string;These constants are used internally by the decorators and the FastifyAdapter to retrieve and apply the metadata to routes. They are exported for advanced use cases where manual metadata manipulation might be needed.
The decorators work seamlessly with standard NestJS decorators and can be combined for comprehensive route configuration:
import { Controller, Get, Post, UseGuards, UseInterceptors } from '@nestjs/common';
import { RouteConfig, RouteConstraints, RouteSchema } from '@nestjs/platform-fastify';
import { AuthGuard } from './auth.guard';
import { LoggingInterceptor } from './logging.interceptor';
@Controller('secure')
@UseGuards(AuthGuard)
@UseInterceptors(LoggingInterceptor)
export class SecureController {
@Get('data')
@RouteConfig({
rateLimit: { max: 10, timeWindow: '1 minute' }
})
@RouteConstraints({
headers: { 'x-api-key': /^[a-f0-9]{32}$/ }
})
@RouteSchema({
headers: {
type: 'object',
required: ['x-api-key'],
properties: {
'x-api-key': { type: 'string', pattern: '^[a-f0-9]{32}$' }
}
},
response: {
200: {
type: 'object',
properties: {
data: { type: 'array' },
timestamp: { type: 'string' }
}
}
}
})
getSecureData() {
return {
data: ['sensitive', 'information'],
timestamp: new Date().toISOString()
};
}
}