No description
- TypeScript 98.2%
- JavaScript 1.8%
| .gitignore | ||
| example.controller.ts | ||
| example.module.ts | ||
| generate-get-routes.decorator.test.ts | ||
| generate-get-routes.decorator.ts | ||
| jest.config.js | ||
| main.ts | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
NestJS Generated Routes Example
Example demonstrating dynamic route generation with NestJS decorators.
Overview
This project shows how to use a custom @GenerateGetRoutes decorator to automatically create GET endpoints from a configuration object, reducing boilerplate code.
Installation
npm install
Running the Application
npm start
The server will start on http://localhost:3000.
Running Tests
npm test
Usage
Define routes as a data object and apply the decorator to your controller:
import { Controller } from '@nestjs/common';
import { GenerateGetRoutes, RoutesData } from './generate-get-routes.decorator';
const staticRoutes: RoutesData = {
health: {
path: 'health',
response: { status: 'ok', timestamp: new Date().toISOString() },
},
version: {
path: 'version',
response: { version: '1.0.0', build: '12345' },
},
config: {
path: 'config',
response: { env: 'development', debug: true },
},
};
@GenerateGetRoutes(staticRoutes)
@Controller('example')
export class ExampleController {}
This automatically creates:
GET /example/health→{ status: 'ok', timestamp: '...' }GET /example/version→{ version: '1.0.0', build: '12345' }GET /example/config→{ env: 'development', debug: true }