Why nestjs-endpoints
In a NestJS app a route is described several times: in decorators, in DTOs or schemas, in parameter and return types, and again in the front end. Each copy can drift from the others.
nestjs-endpoints has you describe a route once, in one file, and derives the rest.
The same route, twice
Here is a route that creates an order, as a NestJS controller that validates with Zod. It relies on NestJS's StandardSchemaValidationPipe being bound globally, in main.ts:
import {
Body,
Controller,
HttpCode,
Post,
SerializeOptions,
StandardSchemaSerializerInterceptor,
UseInterceptors,
} from '@nestjs/common';
import { z } from 'zod';
const createOrderInput = z.object({
productId: z.string(),
quantity: z.number().int().min(1),
});
const createOrderOutput = z.object({
id: z.number(),
status: z.enum(['pending', 'paid', 'cancelled']),
});
@Controller('orders')
@UseInterceptors(StandardSchemaSerializerInterceptor)
export class OrdersController {
constructor(private readonly orders: OrdersService) {}
@Post('create')
@HttpCode(200)
@SerializeOptions({ schema: createOrderOutput })
create(
@Body({ schema: createOrderInput })
input: z.output<typeof createOrderInput>,
) {
return this.orders.create(input);
}
}And as an endpoint:
// src/endpoints/orders/create.endpoint.ts
import { endpoint, z } from 'nestjs-endpoints';
import { OrdersService } from './orders.service.js';
export default endpoint.mutation({
input: z.object({
productId: z.string(),
quantity: z.number().int().min(1),
}),
output: z.object({
id: z.number(),
status: z.enum(['pending', 'paid', 'cancelled']),
}),
inject: { orders: OrdersService },
handler: ({ input, orders }) => orders.create(input),
});Both serve POST /orders/create, validate the body, and validate the response. The differences are in what you leave out, and in what the compiler can check:
| Controller | Endpoint | |
|---|---|---|
| The route's path | In @Controller() and @Post() | The file's path |
| The handler's parameter types | Written beside each schema | Inferred from the schemas |
| What a schema is given | Every argument, through every pipe | Only its own value |
| The return value | Validated when the request runs | Also checked by the compiler, against output |
| Registering it | Listed in a module's controllers | Found by the router |
| A client for the front end | Yours to generate | Generated from the same schemas |
What you get
- One file per endpoint: its schemas, its handler and what it injects, the REPR pattern.
- Types and validation from the schemas, for requests and responses.
- File-based routing, and routers that give a folder its own providers and guards, which NestJS can't.
- Faster validation, and a typed client for axios or React Query.
It leaves NestJS alone: an endpoint is a controller class. See How it fits NestJS.
What it asks of you
It is opinionated in a few places, and they are worth knowing before you start:
- Endpoints are
POSTs with a JSON body, unless you say otherwise. The API reads as a set of operations (/orders/create,/orders/list) more than as REST resources. An endpoint that needs another method setsmethod. - Zod is the schema library. Zod 4.1 or later.
- ESM only. The project needs
"type": "module". - File-based routing needs the files on disk. A build that bundles the app into one file (Webpack,
nest build --webpack) is not supported.
Where the ideas come from
nestjs-endpoints is inspired by FastEndpoints for .NET, by tRPC, and by the file-based routing of TanStack Router.