Guides
Code Generators
Scaffold controllers, services, middleware, guards, and routes from the command line
The nextrush generate command (alias nextrush g) creates new files from templates with the correct structure, imports, and naming conventions.
Usage
npx nextrush generate <type> <name>
npx nextrush g <type> <name>Generator Types
Available generators
| Property | Type | Description |
|---|---|---|
controller (c) | string | Class-based controller with @Controller, @Get, @Post, @Param, @Body decorators |
service (s) | string | Injectable service class with @Service decorator and CRUD stubs |
middleware (mw) | string | Async middleware function with timing pattern |
guard (g) | string | Guard function with authorization token check pattern |
route (r) | string | Functional router with GET, GET/:id, and POST routes |
Examples
Generate a Controller
npx nextrush g controller userCreates src/controllers/user.controller.ts:
import { Controller, Get, Post, Body, Param } from 'nextrush/class';
@Controller('/user')
export class UserController {
@Get()
async findAll() {
return [];
}
@Get('/:id')
async findOne(@Param('id') id: string) {
return { id };
}
@Post()
async create(@Body() data: unknown) {
return data;
}
}Generate a Service
npx nextrush g service userCreates src/services/user.service.ts:
import { Service } from 'nextrush/class';
@Service()
export class UserService {
async findAll() {
return [];
}
async findOne(id: string) {
return { id };
}
async create(data: unknown) {
return data;
}
}Generate Middleware
npx nextrush g middleware request-loggerCreates src/middleware/request-logger.ts:
import type { Middleware } from 'nextrush';
export const requestLogger: Middleware = async (ctx) => {
const start = Date.now();
await ctx.next();
const duration = Date.now() - start;
console.log(`${ctx.method} ${ctx.path} ${ctx.status} ${duration}ms`);
};Generate a Guard
npx nextrush g guard authCreates src/guards/auth.guard.ts:
import type { GuardFn } from 'nextrush/class';
export const authGuard: GuardFn = async (ctx) => {
const token = ctx.get('authorization');
if (!token) return false;
// TODO: Validate token
return true;
};Generate a Route
npx nextrush g route productCreates src/routes/product.ts:
import { createRouter } from 'nextrush';
const router = createRouter();
router.get('/', (ctx) => {
ctx.json([]);
});
router.get('/:id', (ctx) => {
ctx.json({ id: ctx.params.id });
});
router.post('/', (ctx) => {
ctx.json(ctx.body);
});
export default router;Output Directories
| Type | Output Directory | File Suffix |
|---|---|---|
| controller | src/controllers/ | .controller.ts |
| service | src/services/ | .service.ts |
| middleware | src/middleware/ | .ts |
| guard | src/guards/ | .guard.ts |
| route | src/routes/ | .ts |
Directories are created automatically if they don't exist.
Naming Convention
Names must be lowercase with optional hyphens. The generator converts them to PascalCase for class names and camelCase for function names:
| Input | Class Name | Function Name |
|---|---|---|
user | UserController | user |
user-profile | UserProfileController | userProfile |
auth | AuthController | authGuard |
Existing Files
The generator will not overwrite existing files. If the target file already exists, the command exits with an error.