Skip to content

MiddlewareBase

class MiddlewareBase extends FlareBase

Base 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.

new MiddlewareBase(container, ctx)

new MiddlewareBase(container: Container, ctx: FlareHttpContext): MiddlewareBase

container Container

ctx FlareHttpContext

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: FlareHttpContext
deps: 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: StateToken[]

State tokens this middleware writes for later handlers on the same request.

state: StateToken[]
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(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

before(): MiddlewareOverride | Promise<MiddlewareOverride>

Pre-handler hook. Return a MiddlewareOverride to short-circuit the route handler.

returns MiddlewareOverride | Promise<MiddlewareOverride>

config<T>(token: ConfigToken<T>): T

Resolves 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(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

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(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

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(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

tooManyRequests(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

unauthorized(body: JsonValue): ResponseLike

body JsonValue

returns ResponseLike

Learn: MiddlewareBase classes

Last updated: