ic-rocks / agent-pb

An extension of @dfinity/agent to support protobufs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

agent-pb

An extension of @dfinity/agent to support protobufs.

Services

Service methods should include an annotation field set to query or update.

syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

service MyCanister {
  rpc read (google.protobuf.Empty) returns (google.protobuf.UInt64Value) {
    option annotation = query;
  };

  rpc write (google.protobuf.UInt64Value) returns (google.protobuf.Empty) {
    option annotation = update;
  };
}

Usage

First, build protobuf .json files:

IC_PATH=/ic
npx pbjs -t json -p $IC_PATH/**/*.proto my_canister.proto -o bundle.json --sparse
import extendProtobuf from "agent-pb";

const root = protobuf.Root.fromJSON(require("./bundle.json"));
const actor = Actor.createActor(/* regular args */);
extendProtobuf(actor, root.lookupService("MyCanister"));

await actor.read({});
await actor.write(5);

A complete example can be found here.

Building

npm run build

About

An extension of @dfinity/agent to support protobufs


Languages

Language:TypeScript 76.2%Language:JavaScript 23.8%