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

1.0.13 • Public • Published

json-rpc-request

Request service for executing requests in Json RPC format

Json RPC uses just POST method for processing requests in

Installation

yarn add @budarin/json-rpc-request

Usage

import { createRequest } from '@budarin/json-rpc-request';

const baseApiUrl = 'http://domain/api';
const apiRequest = createRequest(baseApiUrl);

type Params = {
    multiplier: number;
};
type Result = {
    your_money: number;
};
type ErrorData = {
    your_money: number;
};

const result = await apiRequest<Params, Result, ErrorData>({
    body: {
        id: 1,
        method: 'multiply_my_money',
        params: {
            multiplier: 200,
        },
    },
    headers: {
        'Content-Type': 'application/json',
    },
});

console.log(result);

// response:
// {
//     id: 1,
//     result: {
//         your_money: 4000000000000000000, 😁
//     }
// }
//
// or maybe:
// {
//     id: 1,
//     error: {
//         code: 500,
//         message: 'you really want a lot',
//         data: {
//             your_money: 5,
//         },
//     }
// }

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @budarin/json-rpc-request

Weekly Downloads

2

Version

1.0.13

License

MIT

Unpacked Size

6.61 kB

Total Files

5

Last publish

Collaborators

  • budarin