Skip to content

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:

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:

ts
// 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:

ControllerEndpoint
The route's pathIn @Controller() and @Post()The file's path
The handler's parameter typesWritten beside each schemaInferred from the schemas
What a schema is givenEvery argument, through every pipeOnly its own value
The return valueValidated when the request runsAlso checked by the compiler, against output
Registering itListed in a module's controllersFound by the router
A client for the front endYours to generateGenerated from the same schemas

What you get ​

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 sets method.
  • 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.

Released under the MIT License.