Skip to content

Intercept

@tsed/di

Usage

typescript
import { Intercept } from "@tsed/di";

See /packages/di/src/common/decorators/intercept.ts.

Overview

ts
function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, options?: any): any;
  • interceptor (Type<T>): The interceptor class token

  • options (any): Optional. configuration passed to the interceptor

Description

Apply an interceptor to a method or all methods of a class.

Wraps method execution with interceptor logic for cross-cutting concerns like logging, validation, caching, or error handling. Can be applied to individual methods or entire classes.

Usage

typescript
import {Injectable, Intercept, Interceptor, InterceptorContext, InterceptorMethods} from "@tsed/di";

@Interceptor()
class LogInterceptor implements InterceptorMethods {
  intercept(context: InterceptorContext) {
    console.log("Before:", context.propertyKey);
    const result = context.next();
    console.log("After:", result);
    return result;
  }
}

@Injectable()
class UserService {
  @Intercept(LogInterceptor)
  async findById(id: string) {
    return {id, name: "User"};
  }
}

// Apply to all methods
@Injectable()
@Intercept(LogInterceptor)
class ProductService {
  getAll() {}
  getById(id: string) {}
}

Released under the MIT License.