Skip to content

ControllerBase

class ControllerBase extends FlareBase

Base class for class-based HTTP controllers registered on HttpArc.

Subclasses use the protected response helpers to return ResponseLike values from route handlers.

new ControllerBase(container, ctx)

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

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.

contract: ContractToken
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.

isolated: boolean

Runs this controller’s routes with NO global middleware (the class-form spelling of the isolated route option).

state: StateToken[]
badRequest(body: JsonValue): ResponseLike

Returns HTTP 400 with a JSON or text body.

body JsonValue

returns ResponseLike

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.

created(body: JsonValue): ResponseLike

Returns HTTP 201 with a JSON or text body.

body JsonValue

returns ResponseLike

error(body: JsonValue): ResponseLike

Returns HTTP 500 with a JSON or text body.

body JsonValue

returns ResponseLike

forbidden(body: JsonValue): ResponseLike

Returns HTTP 403 with a JSON or text body.

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.

noContent(): ResponseLike

Returns HTTP 204 with no body.

returns ResponseLike

notFound(body: JsonValue): ResponseLike

Returns HTTP 404 with a JSON or text body.

body JsonValue

returns ResponseLike

ok(body: JsonValue): ResponseLike

Returns HTTP 200 with a JSON or text body.

body JsonValue

returns ResponseLike

redirect(location: string, options?: RedirectOptions): ResponseLike

Returns an HTTP redirect (302 by default; 301/307/308 when permanent / preserveMethod are set).

location string

options? RedirectOptions

returns ResponseLike

tooManyRequests(body: JsonValue): ResponseLike

Returns HTTP 429 with a JSON or text body.

body JsonValue

returns ResponseLike

unauthorized(body: JsonValue): ResponseLike

Returns HTTP 401 with a JSON or text body.

body JsonValue

returns ResponseLike

Learn: Controller classes

Last updated: