MiddlewareBase
class MiddlewareBase extends FlareBaseBase class for HTTP middleware registered on HttpArc.before, HttpArc.after, or HttpArc.finally.
Optional hooks run at different pipeline stages. MiddlewareBase.provides declares state tokens written for downstream handlers in the same request.
How you get one
Section titled “How you get one”new MiddlewareBase(container, ctx)
Constructor
Section titled “Constructor”new MiddlewareBase(container: Container, ctx: FlareHttpContext): MiddlewareBasecontainer Container
ctx FlareHttpContext
Properties
Section titled “Properties”config
Section titled “config”config: readonly ConfigToken<unknown>[]Declares the config tokens this class requires.
Parallel to static deps. When declared, this.config(token) validates that
the token is in this array before resolving, identical to how this.inject(token)
validates static deps.
ctx: FlareHttpContextdeps: ServiceToken<FlareService>[]Declares the service tokens this class is allowed to inject().
Tokens not listed here cause inject() to throw at the call site, naming both
the class and the offending token.
provides
Section titled “provides”provides: StateToken[]State tokens this middleware writes for later handlers on the same request.
state: StateToken[]Methods
Section titled “Methods”after()
Section titled “after()”after(result: HandlerResult): MiddlewareOverride | Promise<MiddlewareOverride>Post-handler hook. Receives the handler result; may return a replacement response.
result HandlerResult
returns MiddlewareOverride | Promise<MiddlewareOverride>
badRequest()
Section titled “badRequest()”badRequest(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
before()
Section titled “before()”before(): MiddlewareOverride | Promise<MiddlewareOverride>Pre-handler hook. Return a MiddlewareOverride to short-circuit the route handler.
returns MiddlewareOverride | Promise<MiddlewareOverride>
config()
Section titled “config()”config<T>(token: ConfigToken<T>): TResolves a typed config section by token.
Mirrors inject() + static deps: the class must declare a static config
array, and the requested token must appear in it. Both checks throw a
developer-facing error when violated.
token ConfigToken<T>
returns T
Example:
class DbService extends FlareService { static config = [DbConfig];
async onStart() { const { url } = this.config(DbConfig); }}Throws:
When the class has no static config declaration.
Throws:
When the token is not present in static config.
error()
Section titled “error()”error(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
finally()
Section titled “finally()”finally(result: HandlerResult): MiddlewareOverride | Promise<MiddlewareOverride>Always-runs hook: called after every request regardless of whether it was short-circuited
by a before() hook, completed normally, or threw an error that was caught by an error handler.
Runs in LIFO order (last middleware first). May return a replacement handler result.
result HandlerResult
returns MiddlewareOverride | Promise<MiddlewareOverride>
forbidden()
Section titled “forbidden()”forbidden(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
inject()
Section titled “inject()”inject<T extends FlareService>(token: ServiceToken<T>): Injected<T>Resolves a dependency declared on static deps, returning the service with framework
members hidden from its static type.
token ServiceToken<T>
returns Injected<T>
Throws:
When the token is not present in this class’s static deps array.
notFound()
Section titled “notFound()”notFound(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
tooManyRequests()
Section titled “tooManyRequests()”tooManyRequests(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
unauthorized()
Section titled “unauthorized()”unauthorized(body: JsonValue): ResponseLikebody JsonValue
returns ResponseLike
Learn: MiddlewareBase classes