ExecutionContext
class in meocord/common Since 4.1.0
abstract class ExecutionContextDescribes one handler call: which controller and method run, with which arguments, and the metadata on them.
Use it in a guard, an interceptor, a filter, a pipe or an observer to read the call: its interaction or
message, the handler's params, what the handler is decorated with, and its answer through response.
Examples
export const UnderMaintenance = createMetadata<boolean>('maintenance')
@Guard()
export class MaintenanceGuard implements GuardInterface {
constructor(private readonly context: ExecutionContext) {}
canActivate(): boolean {
if (!this.context.get(UnderMaintenance)) return true
throw new GuardDeniedError(`${this.context.getHandlerName()} is closed for now.`)
}
}Members
constructor
new ExecutionContext()response
get response(): ResponseState | undefinedThe response state of the interaction being handled, the same one respond(interaction) returns,
or undefined for anything that cannot be answered: a message, a reaction, an event or
autocomplete.
get Deprecated
abstract get<T>(metadata: MetadataDecorator<T>): T | undefined
abstract get<T = unknown>(key: string | symbol): T | undefinedSince 4.1, and removed in the next major version (5.0). Use
get(metadata)instead. Itsmetadatais a decorator made bycreateMetadata, whose value is typed and whose key cannot collide with another.
Reads a metadata value for the running handler: the method's value, else the controller's. Values resolve through the prototype chain, so an inherited handler reads its base class's method value before the subclass's class value.
Parameters
| Name | Type | Description |
|---|---|---|
metadata | MetadataDecorator<T> | A decorator made by |
Returns
T | undefinedThe value, or undefined when neither the method nor the controller declares one.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | symbol |
Returns
T | undefinedgetAll Deprecated
abstract getAll<T>(metadata: MetadataDecorator<T>): T[]
abstract getAll<T = unknown>(key: string | symbol): T[]Since 4.1, and removed in the next major version (5.0). Use
getAll(metadata)instead. Itsmetadatais a decorator made bycreateMetadata, whose values are typed and whose key cannot collide with another.
Reads every declared value for the running handler, method first, then controller.
Parameters
| Name | Type | Description |
|---|---|---|
metadata | MetadataDecorator<T> | A decorator made by |
Returns
T[]The declared values; empty when none is declared.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | symbol |
Returns
T[]getArgs
abstract getArgs(): readonly unknown[]The arguments the handler is called with, as they stand when the stage asks: raw in a guard, and
validated and piped once the handler's arguments are prepared, so in an interceptor after
next.handle() and in a filter for an error the handler threw. The second is what
ExecutionContext.getHandlerParams returns.
Returns
readonly unknown[]getController
abstract getController(): (new (...args: any[]) => unknown) | undefinedThe controller class the handler runs on, the subclass for a handler it inherits, or undefined in a filter
handling an error no handler was reached for, such as CommandNotFoundError.
Returns
(new (...args: any[]) => unknown) | undefinedgetHandler
abstract getHandler(): ((...args: any[]) => unknown) | undefinedThe handler method as the controller declares it, with its decorators applied, so the function
returned is the decorated one rather than the source method. undefined when no handler was
reached.
Returns
((...args: any[]) => unknown) | undefinedgetHandlerName
abstract getHandlerName(): string | undefinedThe name of the handler method, or undefined when no handler was reached.
Returns
string | undefinedgetHandlerParams
abstract getHandlerParams<P = Record<string, unknown>>():
Readonly<P> | undefinedThe handler's params, its second argument: a command's options, a component's customId params, a
modal's fields, a select menu's choices or a message pattern's params. They are read as they stand when the
stage asks. A guard sees them raw. An interceptor sees them raw before next.handle() and validated and piped
after it. A filter sees
them as they were when the error was thrown. Not the same as ExecutionContext.getParams,
which is the running stage's own configuration.
Returns
Readonly<P> | undefinedThe params, or undefined where the handler takes none: a message handler without a
pattern, a reaction or gateway event handler, or a call no handler was reached for.
Examples
@Interceptor()
export class AuditInterceptor implements InterceptorInterface {
async intercept(context: ExecutionContext, next: CallHandler) {
const result = await next.handle()
// Validated and piped by now, as the handler received them
audit.record(context.getHandlerName(), context.getHandlerParams<{ uid: string }>()?.uid)
return result
}
}getInteraction
abstract getInteraction(): Interaction | undefinedThe interaction being handled, or undefined for a message, reaction or event.
Returns
Interaction | undefinedgetMessage
abstract getMessage(): Message | undefinedThe message being handled, or undefined for anything else.
Returns
Message | undefinedgetParams
abstract getParams<
P extends Record<string, unknown> = Record<string, unknown>,
>(): Readonly<P> | undefinedThe params of the running guard's, interceptor's, pipe's or filter's own { provide, params } entry: how
that stage was configured. For the call's input, the handler's second argument, see
ExecutionContext.getHandlerParams.
Returns
Readonly<P> | undefinedThe params, or undefined for one applied by class alone.
getReaction
abstract getReaction(): MessageReaction | PartialMessageReaction | undefinedThe reaction being handled, or undefined for anything else.
Returns
MessageReaction | PartialMessageReaction | undefinedgetTheme
getTheme(): DeepReadonly<MeoCordTheme>The theme of the call: the same one useTheme() returns inside it, with the app's theme and each @UseTheme
that applies to the handler merged over MeoCord's defaults. Frozen, since it is shared by every call it applies to.
Returns
DeepReadonly<MeoCordTheme>getType
abstract getType(): ExecutionContextTypeWhat is being handled: an interaction, an autocomplete request, a message, a reaction or an event.
Returns
ExecutionContextType