mikudos-socketio-app
Version:
mikudos-socketio-app for connection and call methods and sync events on mikudos-socketio-app server, which is provided only for socket.io connection.
33 lines (31 loc) • 1.04 kB
text/typescript
import _ from 'lodash';
import Debug from 'debug';
const debug = Debug('mikudos:json-rpc');
export class RpcServiceMethods {
hooks: any;
service: any;
constructor(hooks: { before: any; after: any; error: any }, service: any) {
this.hooks = hooks;
this.service = service;
}
async handle(method: string, request: any) {
let result: any = {
jsonrpc: '2.0',
id: request.id
};
const handleFunc = async (request: any, result: any) => {
result.result = await this.service[method](request, request.params);
};
const passList = _.compact([
...(this.hooks.before?.all || []),
...(this.hooks.before?.[method] || []),
handleFunc,
...(this.hooks.after?.[method] || []),
...(this.hooks.after?.all || [])
]);
for await (const hook of passList) {
await hook.call(this, request, result);
if (result.result) return result;
}
}
}