Skip to content

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 ​

OptionType
handlerfunctionRequired. Receives one object, and returns the response body or a promise of it.
inputZod schemaThe JSON body. Not allowed with method: 'get'.
queryZod object schemaThe search parameters, on any method.
paramsZod object schemaThe path parameters. Its keys must match the :names in the URL.
outputZod schema, or an object of them by status codeThe 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'.
pathstringAn explicit URL, in place of the one from the file's location. Required for an endpoint registered by hand outside a router.
injectobject of provider classes or decorated<T>()Providers, as a constructor would inject them. See Dependency injection.
injectOnRequestobject of decorated<T>()Per-request values, as parameter decorators would inject them.
decoratorsMethodDecorator[]Decorators for the handler method, such as UseGuards(AuthGuard).
summarystringThe operation's summary in an OpenAPI document.
tagsstring[]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:

KeyPresent whenValue
inputinput is declaredThe parsed body.
rawInputinput is declaredThe body before parsing.
queryquery is declaredThe parsed search parameters.
paramsparams is declaredThe parsed path parameters.
schemasalwaysThe Zod schemas: { input?, query?, params? }.
responsealwaysresponse(status, body). Required when output has a schema per status.
your own keysEverything from inject and injectOnRequest, and from the router's injections, by name.

Behavior ​

  • An endpoint answers 200, a POST included, unless its handler returns another status with response(status, body).
  • A request without a body reads as {}, unless input accepts a missing body.
  • query and params values arrive as strings. Coerce them: z.coerce.number(), z.coerce.date(), and z.stringbool() for booleans. z.coerce.boolean() reads the string 'false' as true.
  • An array in query takes repeated keys, ?tags=a&tags=b. A single ?tags=a reads as a one-item array.
  • A request that fails input, query or params is answered 400, with a ZodValidationException.
  • The response is parsed with output before it is sent, which strips the keys the schema doesn't declare. A return value that fails it is answered 500, with a ZodSerializationException.
  • Without output, the response is sent unvalidated, and the generated client types it as any.
  • A null response is sent as the JSON null.

invoke() ​

Every endpoint class has an invoke() method, which runs the endpoint without HTTP. See Integration tests.

Endpoint declaresCall
No input, query or paramsinvoke()
input onlyinvoke(rawInput)
query and/or params, without inputinvoke({ query, params })
input, and query and/or paramsinvoke(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.
zZod, re-exported.

Released under the MIT License.