Page Contents

Home > @loopback/context > intercept

intercept() function

Decorator function @intercept for classes/methods to apply interceptors. It can be applied on a class and its public methods. Multiple occurrences of @intercept are allowed on the same target class or method. The decorator takes a list of interceptor functions or binding keys.

Signature:

export declare function intercept(...interceptorOrKeys: InterceptorOrKey[]): (target: any, method?: string, methodDescriptor?: TypedPropertyDescriptor<any>) => any;

Parameters

Parameter Type Description
interceptorOrKeys InterceptorOrKey[] One or more interceptors or binding keys that are resolved to be interceptors

Returns:

(target: any, method?: string, methodDescriptor?: TypedPropertyDescriptor<any>) => any

Example

@intercept(log, metrics)
class MyController {
  @intercept('caching-interceptor')
  @intercept('name-validation-interceptor')
  greet(name: string) {
    return `Hello, ${name}`;
  }
}