Skip to main content

HttpController

Package: @opra/common

HttpController is the runtime class that represents a single controller node in the HTTP API tree. It holds a registry of operations, nested child controllers, declared parameters, and locally scoped types. Controllers are organized in a hierarchy — a child controller's URL path is appended to its parent's.

The @HttpController() decorator is documented in the HTTP API schema section. This page covers the runtime class instance.


Inheritance

DocumentElement
└── HttpController

Properties

PropertyTypeDescription
kind'HttpController'Always 'HttpController'.
namestringRegistry name.
pathstringURL path segment for this controller. Defaults to name.
descriptionstring | undefinedHuman-readable description.
operationsResponsiveMap<HttpOperation>Operations registered on this controller, keyed by name.
controllersResponsiveMap<HttpController>Nested child controllers, keyed by name.
parametersHttpParameter[]Parameters declared on the controller (shared by all operations).
typesDataTypeMapTypes scoped to this controller.
ctorType | undefinedThe TypeScript class constructor.
instanceany | undefinedThe controller instance (set by the adapter).
isRootbooleantrue if the controller's parent is HttpApi (not another controller).

Methods

findController(arg)

Finds a nested child controller by its class constructor or by path string. Searches recursively. Returns undefined if not found.

ctrl.findController('/notes');
ctrl.findController(CustomerNotesController);
findController(controller: Type): HttpController | undefined
findController(resourcePath: string): HttpController | undefined

findParameter(name, location?)

Finds a parameter declared on this controller (or walks up to the parent) by name and optional location ('query', 'path', 'header', 'cookie'). Name matching is case-insensitive; RegExp patterns are also supported.

ctrl.findParameter('x-tenant-id', 'header');
ctrl.findParameter('customerId', 'path');
findParameter(
paramName: string,
location?: 'query' | 'path' | 'header' | 'cookie'
): HttpParameter | undefined

getFullUrl()

Returns the fully resolved URL path for this controller (concatenation of all ancestor paths).

ctrl.getFullUrl(); // e.g. '/api/customers/:customerId/notes'
getFullUrl(): string

toJSON(options?)

Returns an OpraSchema.HttpController plain object for schema export.

toJSON(options?: ApiDocument.ExportOptions): OpraSchema.HttpController

Obtaining an HttpController instance

import { HttpController } from '@opra/common';

const ctrl = document.httpApi.findController('/customers');

if (ctrl instanceof HttpController) {
console.log(ctrl.name); // 'Customers'
console.log(ctrl.path); // '/customers'
console.log(ctrl.isRoot); // true

for (const op of ctrl.operations.values()) {
console.log(op.name, op.method);
}
}

HttpApi · HttpOperation · HttpParameter