Skip to content

Commit

Permalink
feat(cosmic-proto): add JsonSafe and RequestQueryJson types
Browse files Browse the repository at this point in the history
- refs: #9072
  • Loading branch information
0xpatrickdev committed Apr 30, 2024
1 parent 0d3d02e commit 6c8690f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
"./cosmos/staking/v1beta1/tx.js": {
"types": "./dist/codegen/cosmos/staking/v1beta1/tx.d.ts",
"default": "./dist/codegen/cosmos/staking/v1beta1/tx.js"
},
"./cosmos/bank/v1beta1/query.js": {
"types": "./dist/codegen/cosmos/bank/v1beta1/query.d.ts",
"default": "./dist/codegen/cosmos/bank/v1beta1/query.js"
},
"./icq/v1/packet.js": {
"types": "./dist/codegen/icq/v1/packet.d.ts",
"default": "./dist/codegen/icq/v1/packet.js"
},
"./tendermint/abci/types.js": {
"types": "./dist/codegen/tendermint/abci/types.d.ts",
"default": "./dist/codegen/tendermint/abci/types.js"
}
},
"main": "dist/index.js",
Expand Down
22 changes: 22 additions & 0 deletions packages/cosmic-proto/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { QueryAllBalancesRequest } from './codegen/cosmos/bank/v1beta1/query.js';
import type { MsgSend } from './codegen/cosmos/bank/v1beta1/tx.js';
import type { MsgDelegate } from './codegen/cosmos/staking/v1beta1/tx.js';
import type { RequestQuery } from './codegen/tendermint/abci/types.js';

/**
* The result of Any.toJSON(). The type in cosms-types says it returns
Expand Down Expand Up @@ -49,3 +50,24 @@ export const typedJson = <T extends keyof Proto3Shape>(
export type Base64Any<T> = {
[Prop in keyof T]: T[Prop] extends Uint8Array ? string : T[Prop];
};

// TODO make codegen toJSON() return these instead of unknown
// https://github.com/cosmology-tech/telescope/issues/605
/**
* Mimics behavor of .toJSON(), converting Uint8Array to base64 strings
* and bigints to strings
*/
export type JsonSafe<T> = {
[Prop in keyof T]: T[Prop] extends Uint8Array
? string
: T[Prop] extends bigint
? string
: T[Prop];
};

/**
* The result of RequestQuery.toJSON(). The type in cosms-types says it returns
* `unknown` but it's actually this. The Uint8Array fields are base64 encoded, while
* bigint fields are strings.
*/
export type RequestQueryJson = JsonSafe<RequestQuery>;

0 comments on commit 6c8690f

Please sign in to comment.