Skip to content
GitHub

Interceptor

function in meocord/decorator Since 4.1.0

Interceptor(
  options?: InterceptorOptions,
): (
  target: new (...args: any[]) => InterceptorInterface,
  propertyKey?: string | symbol,
) => void

Marks a class as an interceptor, which wraps a handler to act before and after it.

Use it on a class that implements InterceptorInterface, then apply the class with UseInterceptor or @MeoCord({ interceptors }): for timing, logging, caching, or turning one error into another. To stop a call before it runs, use a Guard; to handle an error, an exception filter (Catch).

Where it runs

  • Interceptors — where @UseInterceptor or @MeoCord({ interceptors }) applies it, around everything up to the handler

Parameters

NameTypeDefaultDescription
options?InterceptorOptions

types, the context types the interceptor runs for; every type unless given.

options.types?readonly ExecutionContextType[]every type

The context types the interceptor runs for, as ExecutionContext.getType() reports them; it is skipped for any other call. A subclass inherits them unless it declares its own.

Returns

Returns a property decorator.

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

Throws

  • Error when types is empty or only ['autocomplete'], which interceptors never run for.

Examples

TypeScript
@Interceptor({ types: ['interaction'] })
export class AuditInterceptor implements InterceptorInterface {
  async intercept(context: ExecutionContext, next: CallHandler): Promise<unknown> {
    const result = await next.handle()
    console.log(`${context.getInteraction()?.user.id} ran ${context.getHandlerName()}`)
    return result
  }
}