Cheat Sheet โ
๐ Server & Configuration โ
ts
import {Configuration, Inject} from "@tsed/di";
import {PlatformApplication} from "@tsed/platform-http";
import * as controllers from "./controllers/index.js";
@Configuration({
port: 3000,
logger: true,
mount: [...Object.values(controllers)]
})
export class Server {
@Inject()
protected app: PlatformApplication;
}
๐ฏ Simple Controller โ
ts
import {Controller} from "@tsed/di";
import {Get} from "@tsed/schema";
@Controller("/hello")
export class HelloController {
@Get("/")
get() {
return {message: "Hello world"};
}
}
๐งฉ Service Injection โ
ts
import {Injectable} from "@tsed/di";
@Injectable()
export class MyService {
greet(name: string): string {
return `Hello, ${name}!`;
}
}
ts
import {Get} from "@tsed/schema";
import {Controller, Inject} from "@tsed/di";
@Controller("/hello")
export class HelloController {
@Inject()
protected myService: MyService;
@Get("/")
get() {
return {message: this.myService.greet("World")};
}
}
๐ฅ Retrieving Parameters โ
ts
import {Controller} from "@tsed/di";
import {Get, Post} from "@tsed/schema";
import {PathParams, QueryParams, BodyParams, HeaderParams} from "@tsed/platform-params";
@Controller("/users")
export class UserController {
@Get("/:id")
getById(@PathParams("id") id: string, @QueryParams("expand") expand?: boolean) {
// ...
}
@Post("/")
create(@BodyParams() user: any, @HeaderParams("x-api-key") apiKey: string) {
// ...
}
}
๐ก๏ธ Custom middleware โ
ts
import {Middleware} from "@tsed/platform-middlewares";
import {Context} from "@tsed/platform-params";
import {Unauthorized} from "@tsed/exceptions";
@Middleware()
export class AuthMiddleware {
async use(@Context() ctx: Context): Promise<void> {
const token = ctx.request.headers["authorization"];
if (!token) {
throw new Unauthorized("Token required");
}
}
}
๐งช Validating with @tsed/schema โ
Using decorators โ
ts
import {Property, Required} from "@tsed/schema";
class UserModel {
@Required()
@Property()
name: string;
@Property()
email?: string;
}
@Controller("/")
class MyController {
@Post("/")
create(@BodyParams() user: UserModel) {
// validation automatique
}
}
Using a schema โ
ts
import {object, string} from "@tsed/schema";
const UserSchema = object({
name: string().required().description("User's name"),
email: string().description("User's email")
}).label("UserSchema");
interface User {
name: string;
email?: string;
}
@Controller("/")
class MyController {
@Post("/")
create(@BodyParams() @Schema(UserSchema) user: User) {
// validation automatique
}
}
Run validation manually โ
ts
import {validate} from "@tsed/ajv";
// Shortcut to inject(AjvService).validate()
await validate(
{
name: "John Doe",
email: "email@tsed.dev"
},
{type: UserModel}
);
or:
ts
import {validate} from "@tsed/ajv";
await validate(
{
name: "John Doe",
email: "email@tsed.dev"
},
UserSchema
);
๐ Lifecycle hooks in services โ
Listen to service lifecycle events โ
ts
import {Injectable, OnInit, OnDestroy} from "@tsed/di";
@Injectable()
export class MyService implements OnInit, OnDestroy {
$onInit() {
console.log("Service initialized");
}
$onDestroy() {
console.log("Service destroyed");
}
}
Emit custom events โ
ts
import {$emit} from "@tsed/di";
@Injectable()
export class MyService {
async doSomething() {
// Do some work...
// Emit a custom event
$emit("myCustomEvent", {data: "Hello World"});
}
}
Method | Description | Sync/Async | Example usage |
---|---|---|---|
$emit | Emits an event synchronously to all listeners. | Sync | $emit("eventName", data); |
$asyncEmit | Emits an event asynchronously, waits for all listeners to complete. | Async | await $asyncEmit("eventName", data); |
$alter | Passes a value through all listeners, each can alter the value (sync). | Sync | const result = $alter("eventName", value, ...args); |
$asyncAlter | Like $alter , but listeners can be async and the result is awaited. | Async | const result = await $asyncAlter("eventName", value, ...args); |
๐ Documentation Swagger โ
ts
import {Returns, Summary} from "@tsed/schema";
class MyController {
@Get("/")
@Summary("Return all users")
@(Returns(200, Array).Of(UserModel))
getAll() {
return [];
}
}
Enable in the server config:
ts
import "@tsed/swagger";
@Configuration({
swagger: [
{
path: "/docs"
}
]
})
๐งโ๐ป Use context โ
In controllers:
ts
import {Context} from "@tsed/platform-params";
@Controller("/users")
export class UserController {
@Get("/:id")
getById(@PathParams("id") id: string, @Context() ctx: Context) {
// Access request/response via ctx.request and ctx.response
return {id, userAgent: ctx.request.headers["user-agent"]};
}
}
In services:
ts
import {Injectable, InjectContext} from "@tsed/di";
import type {PlatformContext} from "@tsed/platform-http";
@Injectable()
export class MyService {
@InjectContext()
protected ctx: PlatformContext;
async doSomething() {
const userId = this.ctx.request.headers["user-id"];
this.ctx.logger.info(`User ID: ${userId}`);
// Use userId for some logic
return `User ID is ${userId}`;
}
}
๐งช Unit tests with PlatformTest โ
ts
import {PlatformTest} from "@tsed/platform-http/testing";
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());
it("should inject service", () => {
const service = PlatformTest.get<MyService>(MyService);
expect(service).toBeInstanceOf(MyService);
});
๐ฆ Packages Overview โ
Package | Main purpose | Node.js/Bun.js | Browser |
---|---|---|---|
@tsed/core | Provides utilities, helpers, and internal decorators used by other packages. | โ๏ธ | โ๏ธ |
@tsed/di | Dependency injection (IoC), service lifecycle management, injection decorators. | โ๏ธ | โ๏ธ |
@tsed/hooks | Adds hooks/callbacks on the lifecycle of services and middlewares. | โ๏ธ | โ๏ธ |
@tsed/json-mapper | Serialization/deserialization of JSON objects, data transformation. | โ๏ธ | โ๏ธ |
@tsed/exceptions | Provides custom HTTP exception classes for error handling. | โ๏ธ | โ๏ธ |
@tsed/schema | Schema declaration, validation, and model documentation (OpenAPI/Swagger). | โ๏ธ | โ๏ธ |
@tsed/ajv | Consume json-schema to validate payload (based on Ajv) | โ๏ธ | โ๏ธ |
@tsed/platform-http | Generic HTTP implementation, platform abstraction (Express, Koa, etc.). | โ๏ธ | โ |
@tsed/platform-express | Express.js specific integration to start the server and handle requests. | โ๏ธ | โ |
@tsed/platform-koa | Koa.js specific integration to start the server and handle requests. | โ๏ธ | โ |
@tsed/platform-fastify | Fastify.js specific integration to start the server and handle requests. | โ๏ธ | โ |
@tsed/platform-http/testing | Utilities for unit and integration testing with PlatformTest. | โ๏ธ | โ |
@tsed/platform-params | Decorators and logic to extract request parameters (body, query, path, headers, etc.). | โ๏ธ | โ |
@tsed/platform-router | Routing management, controller mounting, and route resolution. | โ๏ธ | โ |
@tsed/platform-middlewares | Management, registration, and execution of middlewares in the request lifecycle. | โ๏ธ | โ |
@tsed/engines | Integrates template engines (EJS, Pug, Handlebarsโฆ) for server-side rendering. | โ๏ธ | โ |
@tsed/platform-views | Adds support for view rendering in controllers/responses via configured engines. | โ๏ธ | โ |
๐ Notes โ
TIP
- In TS.ED v8, always prefer
@tsed/di
for injection,@tsed/schema
for schema declaration and HTTP decorators. @tsed/common
is being deprecated in v8.- Asynchronous middleware without
next()
. Ts.ED automatically handles promises. - Use
@tsed/platform-express
(or another platform) to start the server.