Skip to content
An implementation of the JSON-RPC 2.0 specification for building RPCs with @hapijs
TypeScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
__tests__
src
.editorconfig
.gitignore
.prettierignore
.prettierrc.json
CHANGELOG.md
LICENSE
README.md
package.json
renovate.json
tsconfig.json
tslint-project.json
tslint.json
yarn.lock

README.md

@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: MPL-2.0

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

This project exists thanks to all the people who contribute.

License

Mozilla Public License Version 2.0 (MPL-2.0).

You can’t perform that action at this time.