@hapist/json-rpc
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@hapist/json-rpc

An implementation of the JSON-RPC 2.0 specification for building RPCs with hapi.js

Latest Version Node Engine Build Status Codecov License: MIT

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.

Credits

License

MIT © Brian Faust

Readme

Keywords

none

Package Sidebar

Install

npm i @hapist/json-rpc

Weekly Downloads

2

Version

0.2.0

License

MIT

Unpacked Size

135 kB

Total Files

17

Last publish

Collaborators

  • faustbrian