Skip to main content

HttpApi

Package: @opra/common

HttpApi is the runtime class that represents the HTTP transport layer of an OPRA document. It holds the controller registry, the base URL, and provides lookup methods for controllers and operations. You obtain it from an ApiDocument via document.httpApi or document.getHttpApi().


Inheritance

DocumentElement
└── ApiBase
└── HttpApi

Properties

Properties inherited from ApiBase:

PropertyTypeDescription
transport'http'Always 'http'.
namestringAPI name (e.g. 'CustomerApi').
descriptionstring | undefinedHuman-readable description.
ownerApiDocumentThe owning document.

Properties on HttpApi:

PropertyTypeDescription
urlstring | undefinedBase URL prefix for all routes (e.g. '/api').
controllersResponsiveMap<HttpController>Registry of top-level controllers, keyed by name.

Methods

findController(arg)

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

const ctrl = httpApi.findController('/customers');
const ctrl = httpApi.findController(CustomersController);
findController(controller: Type): HttpController | undefined
findController(resourcePath: string): HttpController | undefined

findOperation(arg, operationName)

Finds an operation on a controller located by class or path.

const op = httpApi.findOperation('/customers', 'get');
const op = httpApi.findOperation(CustomersController, 'create');
findOperation(controller: Type, operationName: string): HttpOperation | undefined
findOperation(resourcePath: string, operationName: string): HttpOperation | undefined

toJSON(options?)

Returns an OpraSchema.HttpApi plain object for schema export.

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

Obtaining an HttpApi instance

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

const httpApi = document.httpApi; // throws if not an HttpApi document
const httpApi = document.getHttpApi(); // same, explicit getter

if (httpApi instanceof HttpApi) {
console.log(httpApi.transport); // 'http'
console.log(httpApi.url); // '/api'

for (const ctrl of httpApi.controllers.values()) {
console.log(ctrl.name);
}
}

ApiDocument · HttpController