Skip to content
GitHub

Observer

function in meocord/decorator Since 4.1.0

Observer(
  options?: ObserverOptions,
): (
  target: new (...args: any[]) => DispatchObserver,
  propertyKey?: string | symbol,
) => void

Marks a class as a dispatch observer, told about every call once it has settled, for metrics and audit logs.

Use it on a class that implements DispatchObserver, listed in @MeoCord({ observers }), to record how calls end and how long they take. To change or stop a call, use an interceptor or a guard: an observer cannot.

Where it runs

Parameters

NameTypeDefaultDescription
options?ObserverOptions

types, the context types the observer is told about; every type unless given.

options.types?readonly ExecutionContextType[]every type

The context types the observer is told about, as ExecutionContext.getType() reports them; calls of any other type pass it by. A subclass inherits them unless it declares its own.

Returns

Returns a property decorator.

(
  target: new (...args: any[]) => DispatchObserver,
  propertyKey?: string | symbol,
) => void

Throws

  • Error when the class has no onSettled method, or types is empty, as the decorator applies.

Examples

TypeScript
@Observer()
export class SlowCallObserver implements DispatchObserver {
  private readonly logger = new Logger(SlowCallObserver.name)

  onSettled(context: ExecutionContext, { outcome, durationMs }: DispatchResult) {
    if (durationMs > 1000) this.logger.warn(`${context.getHandlerName()} ${outcome} in ${durationMs} ms`)
  }
}