@hapist/json-rpc
An implementation of the JSON-RPC 2.0 specification for building RPCs with hapi.js
Installation
yarn add @hapist/json-rpc
Examples
Joi (Default)
import * as plugin from "@hapist/json-rpc";
import Joi from "@hapi/joi";
await server.register({
plugin,
options: {
methods: [...],
processor: {
schema: Joi.object().keys({
id: Joi.alternatives().try(Joi.number(), Joi.string()).required(),
jsonrpc: Joi.string().allow("2.0").required(),
method: Joi.string().required(),
params: Joi.object(),
}),
validate(data: object, schema: object) {
return Joi.validate(data, schema);
},
},
},
});
AJV
import * as plugin from "@hapist/json-rpc";
import Ajv from "ajv";
await server.register({
plugin,
options: {
methods: [...],
processor: {
schema: {
properties: {
id: {
type: ["number", "string"],
},
jsonrpc: {
pattern: "2.0",
type: "string",
},
method: {
type: "string",
},
params: {
type: "object",
},
},
required: ["jsonrpc", "method", "id"],
type: "object",
},
validate(data: object, schema: object) {
try {
const ajv = new Ajv({
$data: true,
extendRefs: true,
removeAdditional: true,
});
ajv.validate(schema, data);
return { value: data, error: ajv.errors !== null ? ajv.errorsText() : null };
} catch (error) {
return { value: null, error: error.stack };
}
},
},
});
Testing
yarn test
Security
If you discover a security vulnerability within this package, please send an e-mail to hello@basecode.sh. All security vulnerabilities will be promptly addressed.