demo-protobuf: Improve registry types
This commit is contained in:
parent
8e83996a3b
commit
78a96cf06c
@ -65,7 +65,7 @@ describe("registry demo", () => {
|
||||
}) as unknown) as MsgDemo;
|
||||
const msgDemoBytes = MsgDemo.encode(msgDemo).finish();
|
||||
const msgDemoWrapped = Any.create({
|
||||
type_url: "/demo.MsgDemo",
|
||||
type_url: typeUrl,
|
||||
value: msgDemoBytes,
|
||||
});
|
||||
const txBody = TxBody.create({
|
||||
|
||||
@ -2,20 +2,29 @@ import protobuf from "protobufjs";
|
||||
|
||||
import { cosmos_sdk as cosmosSdk, google } from "./generated/codecimpl";
|
||||
|
||||
export class Registry {
|
||||
private readonly types: Map<string, protobuf.Type>;
|
||||
export type GeneratedType = {
|
||||
readonly create: (properties?: { [k: string]: any }) => protobuf.Message<{}>;
|
||||
readonly encode: (
|
||||
message: protobuf.Message<{}> | { [k: string]: any },
|
||||
writer?: protobuf.Writer,
|
||||
) => protobuf.Writer;
|
||||
readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => protobuf.Message<{}>;
|
||||
};
|
||||
|
||||
constructor(customTypes: readonly [string, protobuf.Type][] = []) {
|
||||
this.types = new Map<string, protobuf.Type>([
|
||||
["/cosmos.Coin", (cosmosSdk.v1.Coin as unknown) as protobuf.Type],
|
||||
["/cosmos.bank.MsgSend", (cosmosSdk.x.bank.v1.MsgSend as unknown) as protobuf.Type],
|
||||
["/cosmos.tx.TxBody", (cosmosSdk.tx.v1.TxBody as unknown) as protobuf.Type],
|
||||
["/google.protobuf.Any", (google.protobuf.Any as unknown) as protobuf.Type],
|
||||
export class Registry {
|
||||
private readonly types: Map<string, GeneratedType>;
|
||||
|
||||
constructor(customTypes: Iterable<[string, GeneratedType]> = []) {
|
||||
this.types = new Map<string, GeneratedType>([
|
||||
["/cosmos.Coin", (cosmosSdk.v1.Coin as unknown) as GeneratedType],
|
||||
["/cosmos.bank.MsgSend", (cosmosSdk.x.bank.v1.MsgSend as unknown) as GeneratedType],
|
||||
["/cosmos.tx.TxBody", (cosmosSdk.tx.v1.TxBody as unknown) as GeneratedType],
|
||||
["/google.protobuf.Any", (google.protobuf.Any as unknown) as GeneratedType],
|
||||
...customTypes,
|
||||
]);
|
||||
}
|
||||
|
||||
public lookupType(name: string): protobuf.Type | undefined {
|
||||
public lookupType(name: string): GeneratedType | undefined {
|
||||
return this.types.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
16
packages/demo-protobuf/types/registry.d.ts
vendored
16
packages/demo-protobuf/types/registry.d.ts
vendored
@ -1,6 +1,18 @@
|
||||
import protobuf from "protobufjs";
|
||||
export declare type GeneratedType = {
|
||||
readonly create: (properties?: { [k: string]: any }) => protobuf.Message<{}>;
|
||||
readonly encode: (
|
||||
message:
|
||||
| protobuf.Message<{}>
|
||||
| {
|
||||
[k: string]: any;
|
||||
},
|
||||
writer?: protobuf.Writer,
|
||||
) => protobuf.Writer;
|
||||
readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => protobuf.Message<{}>;
|
||||
};
|
||||
export declare class Registry {
|
||||
private readonly types;
|
||||
constructor(customTypes?: readonly [string, protobuf.Type][]);
|
||||
lookupType(name: string): protobuf.Type | undefined;
|
||||
constructor(customTypes?: Iterable<[string, GeneratedType]>);
|
||||
lookupType(name: string): GeneratedType | undefined;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user