Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

filipesmedeiros/nano-rpc-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nano RPC client logo

Nano RPC client

A simple and strongly typed client for Nano's RPC, written in Typescript.

Usage

In node, you have to provide a fetch function

import NanoRpcClient from 'nano-rpc-client'
import nodeFetch from 'node-fetch'

const client = new NanoRpcClient('https://my.nano.node/rpc', nodeFetch)

If you're in the browser, window.fetch is used by default

import NanoRpcClient from 'nano-rpc-client'

const client = new NanoRpcClient('https://my.nano.node/rpc')

RPC command arguments and their results are strongly typed

const info = await client.accountInfo('nano_1pnano1yzoxyk11geczosh1bwh97w5t1kfmokwz8hkgiy55h6a7rz6dyr1tm')

info.balance // this is a bigint
info.confirmedBalance // this throws an error at compile time because it doesn't exist

const confirmedInfo = await client.accountInfo('nano_1pnano1yzoxyk11geczosh1bwh97w5t1kfmokwz8hkgiy55h6a7rz6dyr1tm', { includeConfirmed: true })

confirmedInfo.confirmedBalance // this exists (and is a bigint) at compile time because you added the `includeConfirmed` option

You can pass an AbortSignal to all calls to cancel them

const controller = new AbortController()

const confirmedInfo = await client.accountInfo('nano_1pnano1yzoxyk11geczosh1bwh97w5t1kfmokwz8hkgiy55h6a7rz6dyr1tm', undefined, { abortSignal: controller.signal })

if('someCondition')
    controller.abort()

Errors in the RPC command (normally returned in the body) are thrown as RpcErrors that contain a message

try {
    const info = await client.accountInfo('')
} catch (e) {
    console.log(e.message) // logs "Bad account number"
}

Goals

  • To be the de facto JS RPC client, with strong argument typing and great autocomplete
  • To eventually be included as an official package of the nano ecosystem (meaning, distributed under an easily recognizable name like @nano/rpc or @nano-utils/rpc or something like that)
  • To support the most current version of the RPC spec
  • In the future, validate arguments before the request is made (for example, validate nano address strings or number strings)

Non-goals

  • To support versions of the RPC spec other than latest