UNPKG

@opra/common

Version:
68 lines (67 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RpcControllerDecoratorFactory = RpcControllerDecoratorFactory; const objects_1 = require("@jsopen/objects"); const index_js_1 = require("../../schema/index.js"); const constants_js_1 = require("../constants.js"); const CLASS_NAME_PATTERN = /^(.*)(Controller)$/; const augmentationRegistry = []; function RpcControllerDecoratorFactory(options) { const decoratorChain = []; /** * */ const decorator = function (target) { let name = options?.name; if (!name) name = CLASS_NAME_PATTERN.exec(target.name)?.[1] || target.name; const metadata = {}; const baseMetadata = Reflect.getOwnMetadata(constants_js_1.RPC_CONTROLLER_METADATA, Object.getPrototypeOf(target)); if (baseMetadata) (0, objects_1.merge)(metadata, baseMetadata, { deep: true }); const oldMetadata = Reflect.getOwnMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target); if (oldMetadata) (0, objects_1.merge)(metadata, oldMetadata, { deep: true }); (0, objects_1.merge)(metadata, { ...options, kind: index_js_1.OpraSchema.RpcController.Kind, name, path: name, }, { deep: true }); Reflect.defineMetadata(constants_js_1.RPC_CONTROLLER_METADATA, metadata, target); for (const fn of decoratorChain) fn(metadata, target); Reflect.defineMetadata(constants_js_1.RPC_CONTROLLER_METADATA, metadata, target); }; /** * */ decorator.Header = (name, arg1) => { decoratorChain.push((meta) => { const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function' ? { name, type: arg1, } : { ...arg1, name }; meta.headers = meta.headers || []; meta.headers.push(paramMeta); }); return decorator; }; /** * */ 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; } RpcControllerDecoratorFactory.augment = function (fn) { augmentationRegistry.push(fn); };