fastify-file-router
Version:
A fastify plugin that automatically registers routes from files in a directory.
118 lines • 5.14 kB
TypeScript
import type { FastifyInstance, LogLevel } from 'fastify';
import { type LimitFunction } from 'p-limit';
import type { z } from 'zod';
import type { FileRouteConvention } from './FastifyFileRouterOptions.js';
type RouteRegistrationProfileEntry = {
filePath: string;
method: string;
url: string;
importMs: number;
prepareMs: number;
registerMs: number;
totalMs: number;
};
type DirectoryScanProfileEntry = {
dirPath: string;
readdirMs: number;
entryCount: number;
};
type RouteRegistrationProfiling = {
enabled: boolean;
routes: RouteRegistrationProfileEntry[];
directories: DirectoryScanProfileEntry[];
};
export type RouteRegistrationRuntimeContext = {
maxConcurrentTasks: number;
taskLimit: LimitFunction;
profiling: RouteRegistrationProfiling;
getAjv: () => Promise<import('ajv').Ajv | undefined>;
};
export declare function createRouteRegistrationRuntimeContext(options?: {
maxConcurrentTasks?: number;
profiling?: {
enabled?: boolean;
logIndividualRoutes?: boolean;
slowestRoutesCount?: number;
};
}): RouteRegistrationRuntimeContext;
/**
* Parses a filename into its components: route segments, method, and extension.
* @param fileName - The filename to parse
* @param extensions - Valid file extensions
* @param fullPath - The full path to the file (for error messages)
* @returns Object with routeSegments, method, and extension, or null if invalid
*/
export declare function parseFileName(fileName: string, extensions: string[], fullPath: string): {
routeSegments: string[];
method: string;
extension: string;
} | null;
/**
* Converts route segments to a route path based on the convention.
* @param segments - Array of route segments
* @param convention - The route convention to use ('remix' or 'next')
* @param fullPath - The full path to the file (for error messages)
* @returns The converted route path
*/
export declare function convertRoutePath(segments: string[], convention: FileRouteConvention, fullPath: string): string;
/**
* Builds the full URL path from route path and mount point.
* @param routePath - The route path
* @param mount - The mount point
* @returns The full URL path
*/
export declare function buildUrl(routePath: string, mount: string): string;
/**
* Returns true if a directory segment is a route group (parenthesized).
* Route groups like (auth) or (marketing) are used for organization only and do not appear in the URL.
* @param segment - A single path segment (directory name)
*/
export declare function isRouteGroupSegment(segment: string): boolean;
/**
* Checks if a file should be excluded based on exclude patterns.
* @param fileName - The filename to check
* @param excludePatterns - Array of regex patterns to match against
* @returns The matching exclude pattern, or undefined if not excluded
*/
export declare function shouldExcludeFile(fileName: string, excludePatterns: RegExp[]): RegExp | undefined;
/**
* Extracts parameter names from a route path.
* @param routePath - The route path (e.g., '/files/:oid' or '/users/:id/posts/:postId')
* @returns Array of parameter names, or empty array if route contains wildcard
*/
export declare function extractRouteParams(routePath: string): string[];
/**
* Extracts property names from a JSON Schema params object.
* @param schema - The JSON Schema params object
* @returns Array of property names
*/
export declare function extractJsonSchemaParams(schema: unknown): string[];
/**
* Extracts property names from a Zod object schema.
* @param zodSchema - The Zod schema
* @returns Array of property names
*/
export declare function extractZodSchemaParams(zodSchema: z.ZodTypeAny): string[];
/**
* Validates that param schema properties match route path parameters.
* @param routePath - The route path
* @param paramsSchema - The params schema (JSON Schema or Zod)
* @param schemaType - The type of schema ('zod' | 'json' | undefined)
* @param fullPath - The full path to the route file (for error messages)
* @throws Error if schema properties don't match route parameters
*/
export declare function validateParamsSchema(routePath: string, paramsSchema: unknown, schemaType: 'zod' | 'json' | undefined, fullPath: string): void;
/**
* Registers routes from a directory recursively.
* @param fastify - The Fastify instance
* @param mount - The mount point for routes
* @param extensions - Valid file extensions
* @param convention - The route convention to use
* @param logLevel - The log level for messages
* @param excludePatterns - Patterns for files to exclude
* @param dir - The directory to scan
* @param baseRootDir - The base root directory for calculating route paths
*/
export declare function registerRoutes(fastify: FastifyInstance, mount: string, extensions: string[], convention: FileRouteConvention, logLevel: LogLevel, excludePatterns: RegExp[], dir: string, baseRootDir: string, logRoutes?: boolean, zodResponseValidation?: boolean, runtimeContext?: RouteRegistrationRuntimeContext): Promise<void>;
export {};
//# sourceMappingURL=routeRegistration.d.ts.map