import path from 'path'; import fs from 'fs'; import fp from 'fastify-plugin'; import type { FastifyInstance, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault, RouteShorthandOptions, FastifyPluginAsync, FastifyRequest, FastifyReply, HTTPMethods, } from 'fastify'; import type { RouteGenericInterface, RouteHandlerMethod } from 'fastify/types/route'; const methods: HTTPMethods[] = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']; const typeScriptEnabled = Boolean( // @ts-expect-error 7053 https://github.com/TypeStrong/ts-node/issues/846#issuecomment-631828160 process[Symbol.for('ts-node.register.instance')] || process.env.TS_NODE_DEV, ); const extensions = ['.js']; if (typeScriptEnabled) { extensions.push('.ts'); } const isRoute = (ext: string) => extensions.includes(ext); const isTest = (name: string) => name.endsWith('.test') || name.endsWith('.spec') || name.endsWith('.bench'); const isDeclaration = (name: string, ext: string) => ext === '.ts' && name.endsWith('.d'); function addRequestHandler( module: Record, method: HTTPMethods, server: FastifyInstance, fileRouteServerPath: string, ) { const handler = module[method]; if (handler) { server.log.debug(`${method} ${fileRouteServerPath}`); const methodFunctionName = method.toLowerCase() as keyof Pick< FastifyInstance, 'get' | 'put' | 'delete' | 'head' | 'post' | 'options' | 'patch' >; server[methodFunctionName]( fileRouteServerPath, handler.opts || {}, handler as RouteHandlerMethod, ); } } export async function registerRoutes(server: FastifyInstance, folder: string, pathPrefix = '') { const registerRoutesFolders = fs .readdirSync(folder, { withFileTypes: true }) .map(async (folderOrFile) => { const currentPath = path.join(folder, folderOrFile.name); const routeServerPath = `${pathPrefix}/${folderOrFile.name .replace('[', ':') .replace(']', '')}`; if (folderOrFile.isDirectory()) { await registerRoutes(server, currentPath, routeServerPath); } else if (folderOrFile.isFile()) { const { ext, name } = path.parse(folderOrFile.name); if (!isRoute(ext) || isTest(name) || isDeclaration(name, ext)) { return; } let fileRouteServerPath = pathPrefix; if (name !== 'index') { fileRouteServerPath += '/' + name.replace('[', ':').replace(/\]?$/, ''); } if (fileRouteServerPath.length === 0) { fileRouteServerPath = '/'; } const module = await import(currentPath); Object.values(methods).forEach((method) => { addRequestHandler(module, method as HTTPMethods, server, fileRouteServerPath); }); } }); await Promise.all(registerRoutesFolders); } interface FastifyNowOpts { routesFolder: string; pathPrefix?: string; } const fastifyNow: FastifyPluginAsync = async ( server: FastifyInstance, opts: FastifyNowOpts, ) => { if (!(opts && opts.routesFolder)) { throw new Error('fastify-now: should provide opts.routesFolder'); } try { await registerRoutes(server, opts.routesFolder, opts.pathPrefix); } catch (error) { const { message } = error as Error; throw new Error(`fastify-now: error registering routers: ${message}`); } }; type NowRouteHandlerMethod< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, > = ( this: FastifyInstance, request: FastifyRequest, reply: FastifyReply, server: FastifyInstance, ) => void | RouteGeneric['Reply'] | Promise; export interface NowRequestHandler< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, > extends NowRouteHandlerMethod { opts?: RouteShorthandOptions; } export default fp(fastifyNow);