Docs/Controllers
@defineResponse
Documents the expected response shape for OpenAPI output.
@defineResponse({
statusCode: 201,
title: 'Created',
entity: 'User',
description: 'User was created successfully',
headers: { 'Content-Type': 'application/json' },
body: exampleUserDto, // an instance of your DTO class
})The body schema is published under components.schemas with the name in entity; routes naming the same entity share one schema. Without entity the schema is named after the route: POST /users answering 201 gives PostUsers201, GET /users/:id gives GetUsersById200, so two routes never overwrite each other's schema. Routes whose names come out the same (/user-list and /user_list, /users/:id and /users/by-id) get a suffix: GetUserList200, GetUserList200_2. Characters OpenAPI does not allow in a component name are replaced in entity ('User Profile' becomes User_Profile).
description falls back to title, then to "<status> response" (OpenAPI requires one). A Content-Type with parameters (application/json; charset=utf-8) keeps the schema. A 204 or 304 is documented without content, and an object body without a Content-Type as application/json, the type it is sent with.
Errors
Throw an HttpError, or a plain { statusCode, message } object, to answer with that status and message:
import { HttpError } from 'alado';
@get('/orders/:id')
@defineRequest({ path: OrderIdDto })
async getOrder(req: Request) {
const order = await orders.find(req.path.id);
if (!order) {
throw new HttpError(404, 'No such order'); // or: throw { statusCode: 404, message: 'No such order' }
}
return { statusCode: 200, body: order };
}Only errors meant for the client reach it. Any other
Erroris answered with500 Internal Server Errorand passed to the logger, also when it has astatusCodeof its own. Errors from libraries and SDKs often carry one next to a message naming hosts, buckets or roles (AccessDenied: arn:aws:iam::...); before 3.8.8 such a message was sent to the client. To pass one on, catch it and throw anHttpErrorwith a message you chose.