UNPKG

@opra/common

Version:
40 lines (39 loc) 1.5 kB
import { omit } from '@jsopen/objects'; import { OpraSchema } from '../../schema/index.js'; import { WS_CONTROLLER_METADATA } from '../constants.js'; const augmentationRegistry = []; export function WSOperationDecoratorFactory(decoratorChain, options) { /** * */ const decorator = ((target, propertyKey) => { if (typeof propertyKey !== 'string') throw new TypeError(`Symbol properties can not be decorated`); const operationMetadata = { kind: OpraSchema.WSOperation.Kind, event: propertyKey, ...omit(options, ['kind']), }; const controllerMetadata = (Reflect.getOwnMetadata(WS_CONTROLLER_METADATA, target.constructor) || {}); controllerMetadata.operations = controllerMetadata.operations || {}; controllerMetadata.operations[propertyKey] = operationMetadata; for (const fn of decoratorChain) fn(operationMetadata, target, propertyKey); Reflect.defineMetadata(WS_CONTROLLER_METADATA, controllerMetadata, target.constructor); }); /** * */ decorator.UseType = (...type) => { decoratorChain.push((meta) => { meta.types = meta.types || []; meta.types.push(...type); }); return decorator; }; augmentationRegistry.forEach(fn => fn(decorator, decoratorChain, options)); return decorator; } WSOperationDecoratorFactory.augment = function (fn) { augmentationRegistry.push(fn); };