UNPKG

arrest

Version:

OpenAPI v3 compliant REST framework for Node.js, with support for MongoDB and JSON-Schema

134 lines 4.88 kB
import { Operation } from './operation.js'; export function rpc(target, propertyKey) { target[propertyKey].rpc = true; } export class JSONRPCError extends Error { code; data; constructor(code, message, data) { super(message); this.code = code; this.data = data; } } export class JSONRPC extends Operation { getCustomInfo() { return { summary: `Perform a Remote Procedure Call`, requestBody: { required: true, content: { 'application/json': { schema: { type: 'object', required: ['jsonrpc', 'method'], additionalProperties: false, properties: { jsonrpc: { enum: ['2.0'], }, method: { type: 'string', minLength: 1, }, params: { oneOf: [{ type: 'object' }, { type: 'array' }], }, id: { oneOf: [{ type: 'string' }, { type: 'integer' }, { type: 'null' }], }, }, }, }, }, }, responses: { '200': { description: `List of matching ${this.resource.info.namePlural}`, content: { 'application/json': { schema: { type: 'object', required: ['jsonrpc', 'id'], additionalProperties: false, properties: { jsonrpc: { enum: ['2.0'], }, result: {}, error: { type: 'object', required: ['code', 'message'], additionalProperties: false, properties: { code: { type: 'integer', }, message: { type: 'string', minLength: 1, }, data: {}, }, }, id: { oneOf: [{ type: 'string' }, { type: 'integer' }, { type: 'null' }], }, }, }, }, }, }, }, }; } handler(req, res, next) { if (!this[req.body.method] || !this[req.body.method].rpc) { next(new JSONRPCError(-32601, `Unknown method ${req.body.method}`)); } else { (async () => { try { const out = await this[req.body.method](req.body.params, req, res); if (req.body.id) { res.json({ jsonrpc: '2.0', result: out, id: req.body.id, }); } else { res.end(); } } catch (err) { next(err); } })(); } } errorHandler(err, req, res, next) { if (err instanceof JSONRPCError) { res.json({ jsonrpc: '2.0', error: { code: err.code, message: err.message, data: err.data, }, id: req.body.id || null, }); } else { res.json({ jsonrpc: '2.0', error: { code: -32000, message: 'server error', }, id: req.body.id || null, }); } } } //# sourceMappingURL=rpc.js.map