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 May 3, 2024
1 parent 210104e commit 6fc3011
Showing 1 changed file with 22 additions and 0 deletions.
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 6fc3011

Please sign in to comment.