What is @tsoa/runtime?
@tsoa/runtime is a runtime library for the TSOA framework, which is used to build TypeScript-based REST APIs with OpenAPI documentation. It provides decorators and utilities to define API routes, request validation, and response handling.
What are @tsoa/runtime's main functionalities?
Route Definition
Defines a route for the 'users' endpoint and a GET method to retrieve users.
import { Controller, Get, Route } from '@tsoa/runtime';
@Route('users')
export class UserController extends Controller {
@Get()
public async getUsers(): Promise<User[]> {
return [{ id: 1, name: 'John Doe' }];
}
}
Request Validation
Defines a POST method to create a user with request body validation.
import { Controller, Post, Route, Body } from '@tsoa/runtime';
interface CreateUserRequest {
name: string;
}
@Route('users')
export class UserController extends Controller {
@Post()
public async createUser(@Body() request: CreateUserRequest): Promise<User> {
return { id: 1, name: request.name };
}
}
Response Handling
Defines a GET method to retrieve a user by ID with custom response handling for 404 status.
import { Controller, Get, Route, Response } from '@tsoa/runtime';
@Route('users')
export class UserController extends Controller {
@Get('{userId}')
@Response(404, 'User not found')
public async getUser(userId: number): Promise<User> {
if (userId !== 1) {
this.setStatus(404);
return null;
}
return { id: 1, name: 'John Doe' };
}
}
Other packages similar to @tsoa/runtime
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Unlike @tsoa/runtime, Express does not provide built-in support for TypeScript decorators or OpenAPI documentation.
nest
NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications. It uses TypeScript and supports decorators similar to @tsoa/runtime, but it also includes a broader set of features like dependency injection and a modular architecture.
hapi
Hapi is a rich framework for building applications and services in Node.js. It provides powerful configuration options and a plugin-based architecture. Unlike @tsoa/runtime, Hapi does not natively support TypeScript decorators or OpenAPI documentation.