Interceptor
function in meocord/decorator Since 4.1.0
Interceptor(
options?: InterceptorOptions,
): (
target: new (...args: any[]) => InterceptorInterface,
propertyKey?: string | symbol,
) => voidMarks 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
@UseInterceptoror@MeoCord({ interceptors })applies it, around everything up to the handler
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
options? | InterceptorOptions |
| |
options.types? | readonly ExecutionContextType[] | every type | The context types the interceptor runs for, as |
Returns
Returns a property decorator.
(
target: new (...args: any[]) => InterceptorInterface,
propertyKey?: string | symbol,
) => voidThrows
Error when
typesis empty or only['autocomplete'], which interceptors never run for.
Examples
@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
}
}