Endpoint options
ts
import { endpoint } from 'nestjs-endpoints';
export default endpoint.query({ /* options */ });
export default endpoint.mutation({ /* options */ });Both builders take the same options and return a NestJS controller class. Use it as a file's default export for a router to find, in a router's endpoints, or in a module's controllers.
endpoint.query() is for reads and endpoint.mutation() for writes. The builder decides which hooks the React Query client generates for the operation.
Options
| Option | Type | |
|---|---|---|
handler | function | Required. Receives one object, and returns the response body or a promise of it. |
input | Zod schema | The JSON body. Not allowed with method: 'get'. |
query | Zod object schema | The search parameters, on any method. |
params | Zod object schema | The path parameters. Its keys must match the :names in the URL. |
output | Zod schema, or an object of them by status code | The response body. With a schema per status, the handler returns response(status, body). |
method | 'get' | 'post' | 'put' | 'patch' | 'delete' | The HTTP method. Defaults to 'post'. |
path | string | An explicit URL, in place of the one from the file's location. Required for an endpoint registered by hand outside a router. |
inject | object of provider classes or decorated<T>() | Providers, as a constructor would inject them. See Dependency injection. |
injectOnRequest | object of decorated<T>() | Per-request values, as parameter decorators would inject them. |
decorators | MethodDecorator[] | Decorators for the handler method, such as UseGuards(AuthGuard). |
summary | string | The operation's summary in an OpenAPI document. |
tags | string[] | OpenAPI tags, added to the one the operation gets from its router. |
query and params take an object schema: bare, behind a transform or preprocess step, or given through schema(). Anything else is a type error.
The handler
The handler receives one object:
| Key | Present when | Value |
|---|---|---|
input | input is declared | The parsed body. |
rawInput | input is declared | The body before parsing. |
query | query is declared | The parsed search parameters. |
params | params is declared | The parsed path parameters. |
schemas | always | The Zod schemas: { input?, query?, params? }. |
response | always | response(status, body). Required when output has a schema per status. |
| your own keys | Everything from inject and injectOnRequest, and from the router's injections, by name. |
Behavior
- An endpoint answers
200, aPOSTincluded, unless its handler returns another status withresponse(status, body). - A request without a body reads as
{}, unlessinputaccepts a missing body. queryandparamsvalues arrive as strings. Coerce them:z.coerce.number(),z.coerce.date(), andz.stringbool()for booleans.z.coerce.boolean()reads the string'false'astrue.- An array in
querytakes repeated keys,?tags=a&tags=b. A single?tags=areads as a one-item array. - A request that fails
input,queryorparamsis answered400, with aZodValidationException. - The response is parsed with
outputbefore it is sent, which strips the keys the schema doesn't declare. A return value that fails it is answered500, with aZodSerializationException. - Without
output, the response is sent unvalidated, and the generated client types it asany. - A
nullresponse is sent as the JSONnull.
invoke()
Every endpoint class has an invoke() method, which runs the endpoint without HTTP. See Integration tests.
| Endpoint declares | Call |
|---|---|
No input, query or params | invoke() |
input only | invoke(rawInput) |
query and/or params, without input | invoke({ query, params }) |
input, and query and/or params | invoke(rawInput, { query, params }) |
Helpers
| Export | |
|---|---|
decorated<T>(decorator) | Pairs a NestJS property or parameter decorator with the type the handler should see. For inject and injectOnRequest. |
schema(zodSchema, { description }) | Attaches an OpenAPI description to a schema. Accepted wherever a schema is, and shown for output. |
z | Zod, re-exported. |