NestJS JSON-RPC is an open-source project that seamlessly integrates JSON-RPC (Remote Procedure Call) functionality into NestJS, a powerful Node.js framework. This project simplifies the development of APIs by providing a structured and efficient communication protocol. With NestJS JSON-RPC, developers can effortlessly build robust and maintainable APIs, enjoying the benefits of JSON-RPC's simplicity and NestJS's modular architecture.
Key Features:
- NestJS Integration: Built specifically for NestJS, ensuring seamless integration with NestJS applications.
- JSON-RPC Protocol: Leverage the simplicity and efficiency of JSON-RPC for communication between client and server.
- Modular Design: Follows the modular design principles of NestJS, allowing for scalability and maintainability.
- Middleware Support: Easily plug into the NestJS middleware system to enhance and customize RPC functionality.
- TypeScript Ready: Fully compatible with TypeScript, providing type safety and enhanced development experience.
Getting Started:
Install
npm i --save nestjs-jrpc
Import module
Import module RpcModule
from nestjs-jrpc
, example
JsonRpcModule.forRoot({
path: '/rpc', // path to RPC
});
How to use simple handler
Create simple RPC handler
Create handler
create RPC handler
import {
RpcId,
RpcPayload,
RpcVersion,
RpcMethod,
IRpcHandler,
RpcHandler,
} from 'nestjs-jrpc';
@RpcHandler({
method: 'test',
})
export class TestHandler implements IRpcHandler<Payload> {
public async invoke(
@RpcPayload() payload: Payload,
@RpcVersion() version: string,
@RpcId() id: number | string,
@RpcMethod() method: string,
) {
return payload;
}
}
Add to providers
Add TestHandler
to providers array
Test with cURL
Every request to RPC is POST method and response status = 200
Test with curl
curl -X POST "http://localhost:3000/rpc" -H "accept: application/json" -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "test", "id": 2}'
How to use multiple handlers in one class
Create multiple RPC handler in one class
Create handlers
Create RPC class handler
import {
RpcId,
RpcPayload,
RpcVersion,
RpcMethod,
RpcMethodHandler,
RpcHandler,
} from 'nestjs-jrpc';
@RpcHandler({
method: 'contact',
})
export class ContactHandler {
@RpcMethodHandler('add')
public async add(
@RpcPayload() payload: Payload,
@RpcVersion() version: string,
@RpcId() id: number | string,
@RpcMethod() method: string,
) {
return payload;
}
@RpcMethodHandler('delete')
public async delete(
@RpcPayload() payload: Payload,
@RpcVersion() version: string,
@RpcId() id: number | string,
@RpcMethod() method: string,
) {
return payload;
}
}
Add to providers
Add ContactHandler
to providers array
Test with cURL
Every request to RPC is POST method and response status = 200
Test with curl contact.add
curl -X POST "http://localhost:3000/rpc" -H "accept: application/json" -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "contact.add", "id": 2}'
Test with curl contact.delete
curl -X POST "http://localhost:3000/rpc" -H "accept: application/json" -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "contact.delete", "id": 2}'
Decorators description
field | decorator | description | required | other |
---|---|---|---|---|
params | @RpcPayload() | get payload ( params ) | false | use pipes... |
jsonrpc | @RpcVersion() | get rpc version | true | use pipes... |
method | @RpcMethod() | get rpc version | true | use pipes... |
id | @RpcId() | get client operation id | false | if not send - response not send, RPC notification. use pipes... |