Fastify Auto Add Routes
This project is maintained by owservable
Automatically register Fastify routes from your file system structure. This library scans your routes directory and automatically registers all route files with your Fastify instance, supporting both TypeScript and JavaScript files.
.ts
and .js
files@owservable/actions
npm install @owservable/fastify-auto-routes
or
yarn add @owservable/fastify-auto-routes
or
pnpm add @owservable/fastify-auto-routes
import fastify from 'fastify';
import { addFastifyRoutes } from '@owservable/fastify-auto-routes';
const server = fastify();
// Add all routes from the routes directory
addFastifyRoutes(server, './routes');
// Start the server
server.listen({ port: 3000 });
import fastify from 'fastify';
import { addFastifyRoutes } from '@owservable/fastify-auto-routes';
const server = fastify();
// Add routes with verbose logging enabled
addFastifyRoutes(server, './routes', true);
server.listen({ port: 3000 });
routes/users.ts
)export default {
method: 'GET',
url: '/users',
handler: async (request, reply) => {
return { users: [] };
}
};
routes/posts.ts
)export default [
{
method: 'GET',
url: '/posts',
handler: async (request, reply) => {
return { posts: [] };
}
},
{
method: 'POST',
url: '/posts',
handler: async (request, reply) => {
return { message: 'Post created' };
}
}
];
routes/api/v1/products.ts
)export default {
method: 'GET',
url: '/api/v1/products',
schema: {
tags: ['products'],
summary: 'Get all products',
response: {
200: {
type: 'object',
properties: {
products: { type: 'array' }
}
}
}
},
handler: async (request, reply) => {
return { products: [] };
}
};
import { addActionRoutes } from '@owservable/fastify-auto-routes';
// Add action-based routes
addActionRoutes(server, './actions');
import { RoutesMap } from '@owservable/fastify-auto-routes';
// Get all registered routes
const allRoutes = RoutesMap.list();
console.log(allRoutes);
// Get routes for specific method
const getRoutes = RoutesMap.getRoutes('GET');
console.log(getRoutes);
// Get all methods
const methods = RoutesMap.getMethods();
console.log(methods);
The library follows these conventions:
routes/
โโโ index.ts โ /
โโโ users.ts โ /users
โโโ posts.ts โ /posts
โโโ api/
โ โโโ v1/
โ โ โโโ users.ts โ /api/v1/users
โ โ โโโ posts.ts โ /api/v1/posts
โ โโโ v2/
โ โโโ users.ts โ /api/v2/users
โโโ admin/
โโโ dashboard.ts โ /admin/dashboard
interface RouteOptions {
method: string | string[]; // HTTP method(s)
url: string; // Route URL
handler: Function; // Route handler function
schema?: object; // JSON schema for validation
preHandler?: Function | Function[]; // Pre-handler hooks
onRequest?: Function | Function[]; // Request hooks
// ... other Fastify route options
}
The library automatically adds/fixes these properties:
addFastifyRoutes(fastify, folder, verbose?)
Recursively scans and registers all route files in the specified folder.
Parameters:
fastify
: Fastify instancefolder
: Path to routes directoryverbose
: Optional boolean for detailed logging (default: false)addActionRoutes(fastify, folder, verbose?)
Registers action-based routes from the specified folder.
Parameters:
fastify
: Fastify instancefolder
: Path to actions directoryverbose
: Optional boolean for detailed logging (default: false)RoutesMap
Static class for managing registered routes.
Methods:
add(method, route)
: Add a route to the mapgetMethods()
: Get all HTTP methodsgetRoutes(method)
: Get routes for specific methodlist()
: Get all routes organized by methodjson()
: Get routes in JSON formatclear()
: Clear all routeskeys()
: Get all method keysvalues()
: Get all route arrayscleanRelativePath(rootFolder, absolutePath, extension)
Utility function to generate clean URLs from file paths.
Parameters:
rootFolder
: Root directory pathabsolutePath
: Absolute file pathextension
: File extension to removeReturns: Clean relative path suitable for URL
npm test
Licensed under The Unlicense.
Contributions are welcome! Please feel free to submit a Pull Request.