Middlewares
Reading time:2 min read
Middlewares are objects or callbacks that run before an execution. They can do some pre-processing logic and store it in the metadata, or even prevent the execution.
We will review some concepts before going into the usage.
Priorities
The Priority determines the order in which they are executed:
- Higher priorities are executed first.
- Monitors are specifically meant for monitoring (e.g. metrics), though that's not enforced.
HighMonitoris the first priority to be executed.LowMonitoris the last priority to be executed.
import { } from '@nyx-discord/djs';
.- LowMonitor
- Lowest
- Low
- Normal
- High
- Highest
- HighMonitor
The default priority is Normal, it's changed by overriding the priority property in classes. It can't be changed for callback-based middlewares.
Responses
A middleware returns a MiddlewareResponse that indicates whether it passes, forces to pass, or fails. If a middleware fails, the execution will stop.
Use the utility methods from the abstract middleware class to create the response:
true()- The middleware passes, and the execution will continue.false()- The middleware fails, and the execution will stop.forceTrue()- The middleware forces to pass, even if other later middlewares would fail.
If you're using a callback-based middleware, you'll need to create your response manually.
