Skip to content

gh-efforts/filmeta-client-rpc.js

 
 

Repository files navigation

@bitrainforest/lotus-client-rpc.js

standard-readme compliant standard-readme compliant

FilMeta 2.0 RPC api js client

Table of Contents

Install

CNPM

cnpm i @bitrainforest/lotus-client-rpc.js --save

NPM

npm i @bitrainforest/lotus-client-rpc.js --save

YARN

yarn add @bitrainforest/lotus-client-rpc.js

PNPM

pnpm add @bitrainforest/lotus-client-rpc.js

Usage

Introduce

本库为调用 filMeta RPC 的接口库,本库必须配合 filmeta-client-protocols.js 库使用。

在使用本库前需使用 filmeta-client-rpc.js 库初始化 HTTPWebSocket 协议。

Export

  • LotusRPC: 接口基类,通过此类进行接口初始化。

    Param Type Description Sample
    provider filmeta-client-rpc.jsProtocol 协议 new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{})
    options any 初始化选项,目前选项只有 schema {schema: schema.chain}
  • schema: 模式枚举类型,通过此类来确定具体调用哪个 schema,用来传入 options

  • 其余的暴露内容都为数据类型声明。

LotusRPC

初始化

import { Protocol, ProtocolType, JsonRpcV2Error } from "@bitrainforest/filmeta-client-protocols.js";
import { LotusRPC, schema,  } from "../src";

const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"

const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})

const chainRpc = new LotusRPC(protocol,{schema: schema.chain})

初始化传参可参考上面的 [ Usage - Export - LotusRPC ]

在初始化的时候,需要根据不同的 schema 来初始化不同的 RPC Client ,并使用初始化后的 Client 来调用相关接口。

发送请求并接受响应

chainRpc.chainHead().then(
  (res: any)=>{
    console.log(res)
  },
  (err: JsonRpcV2Error)=>{
    console.log(err)
  },
)

直接使用相应 schema 初始化的 Client 来调用相关的接口,参数则直接传入调用的接口中即可。

发送订阅事件及取消订阅

订阅事件调用后会自动订阅,返回值与协议的订阅返回值相同。

关闭长链接

chainRpc.destroy()

如你使用的协议是长链接,则可以使用 destroy 来进行关闭长链接。

Example

Javascript

const { Protocol, ProtocolType } = require("@bitrainforest/filmeta-client-protocols.js") ;
const { LotusRPC, schema }  = require("@bitrainforest/filmeta-client-rpc.js");

const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"

const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})

const chainRpc = new LotusRPC(protocol,{schema: schema.chain})

chainRpc.chainHead().then(
  (res)=>{
    console.log(res)
  },
  (err)=>{
    console.log(err)
  },
)

Typescript

import { Protocol, ProtocolType, JsonRpcV2Error } from "@bitrainforest/filmeta-client-protocols.js";
import { LotusRPC, schema,  } from "@bitrainforest/filmeta-client-rpc.js";

const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"

const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})

const chainRpc = new LotusRPC(protocol,{schema: schema.chain})

chainRpc.chainHead().then(
  (res: any)=>{
    console.log(res)
  },
  (err: JsonRpcV2Error)=>{
    console.log(err)
  },
)

Maintainers

@eliassama

About

FilMeta 2.0 RPC api js client

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 100.0%