1.0.4 • Published 8 months ago

asc-rpc v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Implemented JSON RPC specification

Contents

Install

npm i --save asc-rpc

Import module

Import module RpcModule from asc-rpc, 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 'asc-rpc';

@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 'asc-rpc';

@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

fielddecoratordescriptionrequiredother
params@RpcPayload()get payload ( params )falseuse pipes...
jsonrpc@RpcVersion()get rpc versiontrueuse pipes...
method@RpcMethod()get rpc versiontrueuse pipes...
id@RpcId()get client operation idfalseif not send - response not send, RPC notification. use pipes...
1.0.4

8 months ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago