Docs/Getting Started
Quick Start
A minimal API: a controller, the server, and the DTO that validates the body and documents it.
src/modules/user/user.controller.ts
import { post, defineRequest, defineResponse, Request } from 'alado';
import { CredentialsDto } from './dto/credentials.dto';
export class UserController {
@post('/user', { tags: ['User'] })
@defineResponse({
statusCode: 201,
title: 'Created',
headers: { 'Content-Type': 'application/json' },
body: { id: '123', email: 'user@example.com' },
})
@defineRequest({ body: CredentialsDto })
async create(req: Request) {
// validated already; the password is not sent back
const { email } = req.body as CredentialsDto;
return {
statusCode: 201,
headers: { 'Content-Type': 'application/json' },
body: { id: '123', email },
};
}
}src/main.ts
import { initializeApplication } from 'alado';
import { UserController } from './modules/user/user.controller';
const app = initializeApplication({
serverOptions: {
port: 3000,
// Swagger UI at http://localhost:3000/docs, built from the same decorators and DTO
openApiDoc: { enable: true, route: '/docs', info: { title: 'User API', version: '1.0.0', description: '' } },
},
controllers: [UserController],
});
app.start(() => console.log('Running on http://localhost:3000'));src/modules/user/dto/credentials.dto.ts
import { documentProperty, validateProperty, validate } from 'alado';
export class CredentialsDto {
@validateProperty({
handler: validate.string().email().required().build(),
error: { statusCode: 400, message: 'Invalid email' },
})
@documentProperty({ example: 'user@example.com' })
email: string = '';
@validateProperty({
handler: validate.string().min(8).required().build(),
error: { statusCode: 400, message: 'Password must be at least 8 characters' },
})
@documentProperty({ example: 'securePassword' })
password: string = '';
}A request without a valid email or with a shorter password gets the field's 400 before create runs; fields the DTO does not declare are removed from req.body. The validators also give the OpenAPI schema ({ type: 'string', format: 'email' }, { type: 'string', minLength: 8 }, both required), so @documentProperty only adds the examples (3.9.22).
Example project
A complete decorator-based API built with Alado: databikers/alado-decorators-api-example.