Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return function(target, propertyKey: string, descriptor: PropertyDescriptor) {
// save a reference to the original method this way we keep the values currently in the
// descriptor and don't overwrite what another decorator might have done to the descriptor.
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
}
const originalMethod = descriptor.value;
//editing the descriptor/value parameter
descriptor.value = new Method({
handler: function(args: any, done: any) {
let context = this;
// We cannot check this at compile time due to the class-decorator not being
// fully applied yet while the methods are being defined within the class
if (!target.__isRpcClass) {
logger.error(
`@RPCMethod '${propertyKey}' used outside of @RPCNamespace in '${target.name ||
target.constructor.name}'`,
);
}
metrics.methodCall(target.__rpcNamespace + '.' + propertyKey);
try {
checkOptions(arguments, options);
const helpMap = Object.assign(
{},
...[].concat(
...Object.keys(methodMap).map(k =>
({[k]: methodMap[k].rpcOptions || {}})
)
)
);
// Build Jayson Server with flattened methodMap
// --------------------------------------------
const server = new Server(methodMap);
// Self-Documenting Help response
// ------------------------------
server.method('help', new Method({
handler: (args, callback) => {
if (args && args.namespace) {
callback(null, Object.assign(
{},
...[].concat(
...Object.keys(helpMap).map((k: string) => {
if(k.match(`^${args.namespace}`)) {
return ({[k]: helpMap[k]})
}
})
)
));
} else {
callback(null, helpMap);
}
}
_.forEach(methodNames, fn => {
methods[fn] = jayson.Method((args, cb) => {
applyPromiseWithCallback(fn, cb, args)
}, {collect: true})
})