Skip to content
GitHub

ExecutionContext

class in meocord/common Since 4.1.0

abstract class ExecutionContext

Describes 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

TypeScript
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 | undefined

The 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 | undefined

Since 4.1, and removed in the next major version (5.0). Use get(metadata) instead. Its metadata is a decorator made by createMetadata, 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

NameTypeDescription
metadataMetadataDecorator<T>

A decorator made by createMetadata.

Returns

T | undefined

The value, or undefined when neither the method nor the controller declares one.

Parameters

NameTypeDescription
keystring | symbol

Returns

T | undefined

getAll 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. Its metadata is a decorator made by createMetadata, whose values are typed and whose key cannot collide with another.

Reads every declared value for the running handler, method first, then controller.

Parameters

NameTypeDescription
metadataMetadataDecorator<T>

A decorator made by createMetadata.

Returns

T[]

The declared values; empty when none is declared.

Parameters

NameTypeDescription
keystring | 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) | undefined

The 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) | undefined

getHandler

abstract getHandler(): ((...args: any[]) => unknown) | undefined

The 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) | undefined

getHandlerName

abstract getHandlerName(): string | undefined

The name of the handler method, or undefined when no handler was reached.

Returns

string | undefined

getHandlerParams

abstract getHandlerParams<P = Record<string, unknown>>():
  Readonly<P> | undefined

The 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> | undefined

The 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

TypeScript
@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 | undefined

The interaction being handled, or undefined for a message, reaction or event.

Returns

Interaction | undefined

getMessage

abstract getMessage(): Message | undefined

The message being handled, or undefined for anything else.

Returns

Message | undefined

getParams

abstract getParams<
  P extends Record<string, unknown> = Record<string, unknown>,
>(): Readonly<P> | undefined

The 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> | undefined

The params, or undefined for one applied by class alone.

getReaction

abstract getReaction(): MessageReaction | PartialMessageReaction | undefined

The reaction being handled, or undefined for anything else.

Returns

MessageReaction | PartialMessageReaction | undefined

getTheme

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(): ExecutionContextType

What is being handled: an interaction, an autocomplete request, a message, a reaction or an event.

Returns

ExecutionContextType

See also