Skip to content

Responses ​

output is the Zod schema of an endpoint's response body. The compiler checks the handler's return value against it, the response is parsed with it before it is sent, and the generated clients take the response's type from it.

Without output, the response is sent unvalidated and the generated client types it as any.

One response ​

With a single schema, return the value. The endpoint answers 200, whatever its method.

ts
export default 
endpoint
.
query
({
params
:
z
.
object
({
orderId
:
z
.
coerce
.
number
() }),
output
:
z
.
object
({
id
:
z
.
number
(),
status
:
z
.
enum
(['pending', 'paid', 'cancelled']),
total
:
z
.
number
(),
}),
inject
: {
orders
:
OrdersService
},
// `orders.find()` returns more fields than `output` declares
handler
: ({
params
,
orders
}) =>
orders
.
find
(
params
.
orderId
),
});

Parsing strips the keys the schema doesn't declare, so a handler can return a whole record and send only what output names. A return value that fails output is answered 500.

Several statuses ​

Give output a schema per status code, and return response(status, body). The status must be one output declares, and the body is type-checked against that status's schema.

ts
export default 
endpoint
.
mutation
({
input
:
z
.
object
({
userId
:
z
.
number
(),
date
:
z
.
coerce
.
date
(),
}),
output
: {
201:
schema
(
z
.
object
({
id
:
z
.
number
(),
address
:
z
.
string
() }), {
description
: 'Appointment created',
}), 409:
z
.
object
({
message
:
z
.
string
(),
errorCode
:
z
.
string
(),
}), },
inject
: {
appointments
:
AppointmentsService
},
handler
: async ({
input
,
appointments
,
response
}) => {
if (await
appointments
.
hasConflict
(
input
.
date
)) {
return
response
(409, {
message
: 'Appointment has conflict',
errorCode
: 'APPOINTMENT_CONFLICT',
}); } return
response
(201, await
appointments
.
create
(
input
.
userId
,
input
.
date
));
}, });

schema() attaches a description to one status, for an OpenAPI document. In the generated client, a status outside the 2xx range is thrown as a ResponseError.

For failures that aren't part of an endpoint's documented responses, throw NestJS's HTTP exceptions.

Transforms in output schemas ​

The generated clients are typed from what an output schema produces, and a .transform() runs code that can't be read for it. Use .overwrite() for a transform that keeps the type, or say what the transform produces with .meta():

ts
// the same type in and out
z.string().overwrite((value) => value.toUpperCase());

// another type out: say which
z.date()
  .transform((date) => date.toISOString())
  .meta({ type: 'string', format: 'date-time' });

Released under the MIT License.