@opra/common
Version:
Opra common package
79 lines (78 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RpcOperationDecoratorFactory = RpcOperationDecoratorFactory;
const objects_1 = require("@jsopen/objects");
const index_js_1 = require("../../schema/index.js");
const constants_js_1 = require("../constants.js");
const augmentationRegistry = [];
function RpcOperationDecoratorFactory(decoratorChain, payloadType, options) {
let inResponse = false;
/**
*
*/
const decorator = ((target, propertyKey) => {
if (typeof propertyKey !== 'string')
throw new TypeError(`Symbol properties can not be decorated`);
const operationMetadata = {
kind: index_js_1.OpraSchema.RpcOperation.Kind,
channel: propertyKey,
payloadType,
...(0, objects_1.omit)(options, ['kind', 'payloadType']),
};
const controllerMetadata = (Reflect.getOwnMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target.constructor) || {});
controllerMetadata.operations = controllerMetadata.operations || {};
controllerMetadata.operations[propertyKey] = operationMetadata;
for (const fn of decoratorChain)
fn(operationMetadata, target, propertyKey);
Reflect.defineMetadata(constants_js_1.RPC_CONTROLLER_METADATA, controllerMetadata, target.constructor);
});
/**
*
*/
decorator.UseType = (...type) => {
decoratorChain.push((meta) => {
meta.types = meta.types || [];
meta.types.push(...type);
});
return decorator;
};
/**
*
*/
decorator.Header = (name, arg1) => {
decoratorChain.push((meta) => {
const headerMetadata = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
type: arg1,
}
: { ...arg1, name };
const subMeta = inResponse ? meta.response : meta;
if (subMeta.headers) {
subMeta.headers = subMeta.headers.filter(p => String(p.name) !== String(name));
}
else
subMeta.headers = [];
subMeta.headers.push(headerMetadata);
});
return decorator;
};
/**
*
*/
decorator.Response = (_payloadType, _options) => {
decoratorChain.push((meta) => {
inResponse = true;
meta.response = {
..._options,
payloadType: _payloadType,
};
});
return decorator;
};
augmentationRegistry.forEach(fn => fn(decorator, decoratorChain, payloadType, options));
return decorator;
}
RpcOperationDecoratorFactory.augment = function (fn) {
augmentationRegistry.push(fn);
};