NextRush
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

PropertyTypeDescription
controller (c)stringClass-based controller with @Controller, @Get, @Post, @Param, @Body decorators
service (s)stringInjectable service class with @Service decorator and CRUD stubs
middleware (mw)stringAsync middleware function with timing pattern
guard (g)stringGuard function with authorization token check pattern
route (r)stringFunctional router with GET, GET/:id, and POST routes

Examples

Generate a Controller

npx nextrush g controller user

Creates src/controllers/user.controller.ts:

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 user

Creates src/services/user.service.ts:

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-logger

Creates src/middleware/request-logger.ts:

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 auth

Creates src/guards/auth.guard.ts:

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 product

Creates src/routes/product.ts:

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

TypeOutput DirectoryFile Suffix
controllersrc/controllers/.controller.ts
servicesrc/services/.service.ts
middlewaresrc/middleware/.ts
guardsrc/guards/.guard.ts
routesrc/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:

InputClass NameFunction Name
userUserControlleruser
user-profileUserProfileControlleruserProfile
authAuthControllerauthGuard

Existing Files

The generator will not overwrite existing files. If the target file already exists, the command exits with an error.

What's Next?

On this page