Skip to content

Commit

Permalink
feat(types):: ResponseTo utility
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 9, 2024
1 parent 96c9ff5 commit 981b7bf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"@cosmology/telescope": "^1.5.3",
"ava": "^5.3.1",
"rimraf": "^5.0.0",
"tsd": "^0.30.7",
"tsimp": "^2.0.11",
"typescript": "^5.5.0-beta"
},
Expand Down
42 changes: 32 additions & 10 deletions packages/cosmic-proto/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
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 {
QueryAllBalancesRequest,
QueryAllBalancesResponse,
} from './codegen/cosmos/bank/v1beta1/query.js';
import type {
MsgSend,
MsgSendResponse,
} from './codegen/cosmos/bank/v1beta1/tx.js';
import type {
MsgDelegate,
MsgDelegateResponse,
} from './codegen/cosmos/staking/v1beta1/tx.js';
import { RequestQuery } from './codegen/tendermint/abci/types.js';
import type { Any } from './codegen/google/protobuf/any.js';

Expand All @@ -14,8 +23,18 @@ export type AnyJson = { typeUrl: string; value: string };
// TODO codegen this by modifying Telescope
export type Proto3Shape = {
'/cosmos.bank.v1beta1.MsgSend': MsgSend;
'/cosmos.bank.v1beta1.MsgSendResponse': MsgSendResponse;
'/cosmos.bank.v1beta1.QueryAllBalancesRequest': QueryAllBalancesRequest;
'/cosmos.bank.v1beta1.QueryAllBalancesResponse': QueryAllBalancesResponse;
'/cosmos.staking.v1beta1.MsgDelegate': MsgDelegate;
'/cosmos.staking.v1beta1.MsgDelegateResponse': MsgDelegateResponse;
};

// Often s/Request$/Response/ but not always
type ResponseMap = {
'/cosmos.bank.v1beta1.MsgSend': '/cosmos.bank.v1beta1.MsgSendResponse';
'/cosmos.bank.v1beta1.QueryAllBalancesRequest': '/cosmos.bank.v1beta1.QueryAllBalancesResponse';
'/cosmos.staking.v1beta1.MsgDelegate': '/cosmos.staking.v1beta1.MsgDelegateResponse';
};

/**
Expand All @@ -26,13 +45,16 @@ export type Proto3Shape = {
* type with a string.
*/
export type TypedJson<T extends unknown | keyof Proto3Shape = unknown> =
T extends unknown
? { '@type': string }
: T extends keyof Proto3Shape
? Proto3Shape[T] & {
'@type': T;
}
: never;
T extends keyof Proto3Shape
? Proto3Shape[T] & {
'@type': T;
}
: { '@type': string };

export type ResponseTo<T extends TypedJson> =
T['@type'] extends keyof ResponseMap
? TypedJson<ResponseMap[T['@type']]>
: TypedJson;

export const typedJson = <T extends keyof Proto3Shape>(
typeStr: T,
Expand Down
32 changes: 32 additions & 0 deletions packages/cosmic-proto/test/helpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expectType } from 'tsd';
import { typedJson } from '../src/helpers.js';
import type { ResponseTo } from '../src/helpers.ts';

// MsgSend
{
const request = typedJson('/cosmos.bank.v1beta1.MsgSend', {
fromAddress: 'agoric1from',
toAddress: 'agoric1to',
amount: [{ denom: 'ucosm', amount: '1' }],
});
const response: ResponseTo<typeof request> = null as any;
expectType<'/cosmos.bank.v1beta1.MsgSendResponse'>(response['@type']);
response['@type'] = '/cosmos.bank.v1beta1.MsgSendResponse';
// @ts-expect-error invalid value for response
response['@type'] = '/cosmos.bank.v1beta1.MsgSend';
}

// QueryAllBalances
{
const request = typedJson('/cosmos.bank.v1beta1.QueryAllBalancesRequest', {
address: 'agoric1from',
});
const response: ResponseTo<typeof request> = null as any;
expectType<'/cosmos.bank.v1beta1.QueryAllBalancesResponse'>(
response['@type'],
);
response['@type'] = '/cosmos.bank.v1beta1.QueryAllBalancesResponse';
// @ts-expect-error invalid value for response
response['@type'] = '/cosmos.bank.v1beta1.QueryAllBalancesRequest';
response.balances = [{ denom: 'ucosm', amount: '1' }];
}

0 comments on commit 981b7bf

Please sign in to comment.