@hapist/json-rpc
An implementation of the JSON-RPC 2.0 specification for building RPCs with hapi.js
Last updated 9 months ago by faustbrian .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @hapist/json-rpc 
SYNC missed versions from official npm registry.

@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

Current Tags

  • 0.2.0                                ...           latest (9 months ago)

2 Versions

  • 0.2.0                                ...           9 months ago
  • 0.1.0                                ...           9 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (6)
Dev Dependencies (21)

Copyright 2014 - 2017 © taobao.org |