ControllerBase
class ControllerBase extends FlareBaseBase class for class-based HTTP controllers registered on HttpArc.
Subclasses use the protected response helpers to return ResponseLike values from route handlers.
How you get one
Section titled “How you get one”new ControllerBase(container, ctx)
Constructor
Section titled “Constructor”new ControllerBase(container: Container, ctx: FlareHttpContext): ControllerBasecontainer 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.
contract
Section titled “contract”contract: ContractTokenctx: 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.
isolated
Section titled “isolated”isolated: booleanRuns this controller’s routes with NO global middleware (the class-form spelling of the isolated route option).
state: StateToken[]Methods
Section titled “Methods”badRequest()
Section titled “badRequest()”badRequest(body: JsonValue): ResponseLikeReturns HTTP 400 with a JSON or text body.
body JsonValue
returns ResponseLike
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.
created()
Section titled “created()”created(body: JsonValue): ResponseLikeReturns HTTP 201 with a JSON or text body.
body JsonValue
returns ResponseLike
error()
Section titled “error()”error(body: JsonValue): ResponseLikeReturns HTTP 500 with a JSON or text body.
body JsonValue
returns ResponseLike
forbidden()
Section titled “forbidden()”forbidden(body: JsonValue): ResponseLikeReturns HTTP 403 with a JSON or text body.
body 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.
noContent()
Section titled “noContent()”noContent(): ResponseLikeReturns HTTP 204 with no body.
returns ResponseLike
notFound()
Section titled “notFound()”notFound(body: JsonValue): ResponseLikeReturns HTTP 404 with a JSON or text body.
body JsonValue
returns ResponseLike
ok(body: JsonValue): ResponseLikeReturns HTTP 200 with a JSON or text body.
body JsonValue
returns ResponseLike
redirect()
Section titled “redirect()”redirect(location: string, options?: RedirectOptions): ResponseLikeReturns an HTTP redirect (302 by default; 301/307/308 when permanent / preserveMethod are set).
location string
options? RedirectOptions
returns ResponseLike
tooManyRequests()
Section titled “tooManyRequests()”tooManyRequests(body: JsonValue): ResponseLikeReturns HTTP 429 with a JSON or text body.
body JsonValue
returns ResponseLike
unauthorized()
Section titled “unauthorized()”unauthorized(body: JsonValue): ResponseLikeReturns HTTP 401 with a JSON or text body.
body JsonValue
returns ResponseLike
Learn: Controller classes