Skip to main content

WSApi

Package: @opra/common

WSApi is the runtime class that represents the WebSocket transport layer of an OPRA document. When you access document.api and the transport is 'ws', the returned value is a WSApi instance. It holds all controllers and provides lookup helpers for controllers and operations.


Inheritance

DocumentElement
└── ApiBase
└── WSApi

Properties

PropertyTypeDescription
transport'ws'Always 'ws'.
controllersResponsiveMap<WSController>All top-level controllers registered on this API.

Methods

findController(typeOrName)

Returns the WSController matching the given constructor or string name, or undefined if not found.

const ctrl = wsApi.findController('ChatController');
const ctrl = wsApi.findController(ChatController);
findController(typeOrName: Type | string): WSController | undefined

findOperation(controllerTypeOrName, operationName)

Locates an operation by its controller and operation name. Returns undefined if either the controller or operation does not exist.

const op = wsApi.findOperation('ChatController', 'sendMessage');
const op = wsApi.findOperation(ChatController, 'sendMessage');
findOperation(
controllerTypeOrName: Type | string,
operationName: string,
): WSOperation | undefined

toJSON()

Returns a plain OpraSchema.WSApi object for schema export.

toJSON(): OpraSchema.WSApi

Obtaining a WSApi instance

WSApi is constructed by ApiDocumentFactory — you do not create it directly.

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

const document = await ApiDocumentFactory.createDocument({ ... });

if (document.api instanceof WSApi) {
console.log(document.api.transport); // 'ws'

for (const [name, ctrl] of document.api.controllers) {
console.log(name, ctrl.kind); // 'WSController'
}
}

ApiDocument · WSController · WSOperation