Skip to content

Commit

Permalink
Merge pull request #434 from cosmology-tech/instant-hooks
Browse files Browse the repository at this point in the history
Exposing Instant hooks
  • Loading branch information
pyramation authored Aug 11, 2023
2 parents 250952c + e084a6f commit a4d7e88
Show file tree
Hide file tree
Showing 40 changed files with 1,676 additions and 88 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ See [RPC Clients](#rpc-clients) for more info.
| `reactQuery.include.protos` | if set, will create the hooks on matched proto filenames or patterns using minimatch | `[]` |
| `reactQuery.include.packages` | if set, will create the hooks on matched packages files using minimatch | `[]` |
| `reactQuery.include.patterns` | if set, will create the hooks on matched patterns of files using minimatch(deprecated in favor of packages and protos have been supported minimatch) | `[]` |
| `reactQuery.instantExport.include.patterns` | if set, will expose instant hooks on matched patterns of packages + method(e.g. cosmos.bank.v1beta1.useBalance) using minimatch. If there're duplicated method names in multiple packages without setting `reactQuery.instantExport.nameMapping`, one duplicated name will created like: useCosmosBankV1beta1Balance | `[]` |
| `reactQuery.instantExport.nameMapping` | map an alias to a package + method in case of better naming of duplicated method names. (e.g. useBankBalance: cosmos.bank.v1beta1.useBalance) Customized hook name is set in front of pkg+method, by doing this we can prevent duplicate alias. | `{}` |

#### Mobx

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { grpc } from "@improbable-eng/grpc-web";
import { UnaryMethodDefinitionish } from "../../../grpc-web";
import { DeepPartial } from "../../../helpers";
import { BrowserHeaders } from "browser-headers";
import { ReactQueryParams } from "../../../react-query";
import { ProtobufRpcClient } from "@cosmjs/stargate";
import { useQuery } from "@tanstack/react-query";
import { QueryCertificatesRequest, QueryCertificatesResponse } from "./query";
/** Query defines the gRPC querier service */
export interface Query {
Expand Down Expand Up @@ -89,4 +92,32 @@ export class GrpcWebImpl {
});
});
}
}
}
export interface UseCertificatesQuery<TData> extends ReactQueryParams<QueryCertificatesResponse, TData> {
request: QueryCertificatesRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useCertificates = <TData = QueryCertificatesResponse,>({
request,
options
}: UseCertificatesQuery<TData>) => {
return useQuery<QueryCertificatesResponse, Error, TData>(["certificatesQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.certificates(request);
}, options);
};
return {
/** Certificates queries certificates */useCertificates
};
};
149 changes: 148 additions & 1 deletion __fixtures__/v-next/outputv2/cosmos/bank/v1beta1/query.rpc.Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { grpc } from "@improbable-eng/grpc-web";
import { UnaryMethodDefinitionish } from "../../../grpc-web";
import { DeepPartial } from "../../../helpers";
import { BrowserHeaders } from "browser-headers";
import { ReactQueryParams } from "../../../react-query";
import { ProtobufRpcClient } from "@cosmjs/stargate";
import { useQuery } from "@tanstack/react-query";
import { QueryBalanceRequest, QueryBalanceResponse, QueryAllBalancesRequest, QueryAllBalancesResponse, QuerySpendableBalancesRequest, QuerySpendableBalancesResponse, QueryTotalSupplyRequest, QueryTotalSupplyResponse, QuerySupplyOfRequest, QuerySupplyOfResponse, QueryParamsRequest, QueryParamsResponse, QueryDenomMetadataRequest, QueryDenomMetadataResponse, QueryDenomsMetadataRequest, QueryDenomsMetadataResponse, QueryDenomOwnersRequest, QueryDenomOwnersResponse } from "./query";
/** Query defines the gRPC querier service. */
export interface Query {
Expand Down Expand Up @@ -319,4 +322,148 @@ export class GrpcWebImpl {
});
});
}
}
}
export interface UseBalanceQuery<TData> extends ReactQueryParams<QueryBalanceResponse, TData> {
request: QueryBalanceRequest;
}
export interface UseAllBalancesQuery<TData> extends ReactQueryParams<QueryAllBalancesResponse, TData> {
request: QueryAllBalancesRequest;
}
export interface UseSpendableBalancesQuery<TData> extends ReactQueryParams<QuerySpendableBalancesResponse, TData> {
request: QuerySpendableBalancesRequest;
}
export interface UseTotalSupplyQuery<TData> extends ReactQueryParams<QueryTotalSupplyResponse, TData> {
request?: QueryTotalSupplyRequest;
}
export interface UseSupplyOfQuery<TData> extends ReactQueryParams<QuerySupplyOfResponse, TData> {
request: QuerySupplyOfRequest;
}
export interface UseParamsQuery<TData> extends ReactQueryParams<QueryParamsResponse, TData> {
request?: QueryParamsRequest;
}
export interface UseDenomMetadataQuery<TData> extends ReactQueryParams<QueryDenomMetadataResponse, TData> {
request: QueryDenomMetadataRequest;
}
export interface UseDenomsMetadataQuery<TData> extends ReactQueryParams<QueryDenomsMetadataResponse, TData> {
request?: QueryDenomsMetadataRequest;
}
export interface UseDenomOwnersQuery<TData> extends ReactQueryParams<QueryDenomOwnersResponse, TData> {
request: QueryDenomOwnersRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useBalance = <TData = QueryBalanceResponse,>({
request,
options
}: UseBalanceQuery<TData>) => {
return useQuery<QueryBalanceResponse, Error, TData>(["balanceQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.balance(request);
}, options);
};
const useAllBalances = <TData = QueryAllBalancesResponse,>({
request,
options
}: UseAllBalancesQuery<TData>) => {
return useQuery<QueryAllBalancesResponse, Error, TData>(["allBalancesQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.allBalances(request);
}, options);
};
const useSpendableBalances = <TData = QuerySpendableBalancesResponse,>({
request,
options
}: UseSpendableBalancesQuery<TData>) => {
return useQuery<QuerySpendableBalancesResponse, Error, TData>(["spendableBalancesQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.spendableBalances(request);
}, options);
};
const useTotalSupply = <TData = QueryTotalSupplyResponse,>({
request,
options
}: UseTotalSupplyQuery<TData>) => {
return useQuery<QueryTotalSupplyResponse, Error, TData>(["totalSupplyQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.totalSupply(request);
}, options);
};
const useSupplyOf = <TData = QuerySupplyOfResponse,>({
request,
options
}: UseSupplyOfQuery<TData>) => {
return useQuery<QuerySupplyOfResponse, Error, TData>(["supplyOfQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.supplyOf(request);
}, options);
};
const useParams = <TData = QueryParamsResponse,>({
request,
options
}: UseParamsQuery<TData>) => {
return useQuery<QueryParamsResponse, Error, TData>(["paramsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.params(request);
}, options);
};
const useDenomMetadata = <TData = QueryDenomMetadataResponse,>({
request,
options
}: UseDenomMetadataQuery<TData>) => {
return useQuery<QueryDenomMetadataResponse, Error, TData>(["denomMetadataQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.denomMetadata(request);
}, options);
};
const useDenomsMetadata = <TData = QueryDenomsMetadataResponse,>({
request,
options
}: UseDenomsMetadataQuery<TData>) => {
return useQuery<QueryDenomsMetadataResponse, Error, TData>(["denomsMetadataQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.denomsMetadata(request);
}, options);
};
const useDenomOwners = <TData = QueryDenomOwnersResponse,>({
request,
options
}: UseDenomOwnersQuery<TData>) => {
return useQuery<QueryDenomOwnersResponse, Error, TData>(["denomOwnersQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.denomOwners(request);
}, options);
};
return {
/** Balance queries the balance of a single coin for a single account. */useBalance,
/** AllBalances queries the balance of all coins for a single account. */useAllBalances,
/**
* SpendableBalances queries the spenable balance of all coins for a single
* account.
*/
useSpendableBalances,
/** TotalSupply queries the total supply of all coins. */useTotalSupply,
/** SupplyOf queries the supply of a single coin. */useSupplyOf,
/** Params queries the parameters of x/bank module. */useParams,
/** DenomsMetadata queries the client metadata of a given coin denomination. */useDenomMetadata,
/**
* DenomsMetadata queries the client metadata for all registered coin
* denominations.
*/
useDenomsMetadata,
/**
* DenomOwners queries for all account addresses that own a particular token
* denomination.
*/
useDenomOwners
};
};
124 changes: 123 additions & 1 deletion __fixtures__/v-next/outputv2/cosmos/gov/v1beta1/query.rpc.Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { grpc } from "@improbable-eng/grpc-web";
import { UnaryMethodDefinitionish } from "../../../grpc-web";
import { DeepPartial } from "../../../helpers";
import { BrowserHeaders } from "browser-headers";
import { ReactQueryParams } from "../../../react-query";
import { ProtobufRpcClient } from "@cosmjs/stargate";
import { useQuery } from "@tanstack/react-query";
import { QueryProposalRequest, QueryProposalResponse, QueryProposalsRequest, QueryProposalsResponse, QueryVoteRequest, QueryVoteResponse, QueryVotesRequest, QueryVotesResponse, QueryParamsRequest, QueryParamsResponse, QueryDepositRequest, QueryDepositResponse, QueryDepositsRequest, QueryDepositsResponse, QueryTallyResultRequest, QueryTallyResultResponse } from "./query";
/** Query defines the gRPC querier service for gov module */
export interface Query {
Expand Down Expand Up @@ -278,4 +281,123 @@ export class GrpcWebImpl {
});
});
}
}
}
export interface UseProposalQuery<TData> extends ReactQueryParams<QueryProposalResponse, TData> {
request: QueryProposalRequest;
}
export interface UseProposalsQuery<TData> extends ReactQueryParams<QueryProposalsResponse, TData> {
request: QueryProposalsRequest;
}
export interface UseVoteQuery<TData> extends ReactQueryParams<QueryVoteResponse, TData> {
request: QueryVoteRequest;
}
export interface UseVotesQuery<TData> extends ReactQueryParams<QueryVotesResponse, TData> {
request: QueryVotesRequest;
}
export interface UseParamsQuery<TData> extends ReactQueryParams<QueryParamsResponse, TData> {
request: QueryParamsRequest;
}
export interface UseDepositQuery<TData> extends ReactQueryParams<QueryDepositResponse, TData> {
request: QueryDepositRequest;
}
export interface UseDepositsQuery<TData> extends ReactQueryParams<QueryDepositsResponse, TData> {
request: QueryDepositsRequest;
}
export interface UseTallyResultQuery<TData> extends ReactQueryParams<QueryTallyResultResponse, TData> {
request: QueryTallyResultRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useProposal = <TData = QueryProposalResponse,>({
request,
options
}: UseProposalQuery<TData>) => {
return useQuery<QueryProposalResponse, Error, TData>(["proposalQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.proposal(request);
}, options);
};
const useProposals = <TData = QueryProposalsResponse,>({
request,
options
}: UseProposalsQuery<TData>) => {
return useQuery<QueryProposalsResponse, Error, TData>(["proposalsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.proposals(request);
}, options);
};
const useVote = <TData = QueryVoteResponse,>({
request,
options
}: UseVoteQuery<TData>) => {
return useQuery<QueryVoteResponse, Error, TData>(["voteQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.vote(request);
}, options);
};
const useVotes = <TData = QueryVotesResponse,>({
request,
options
}: UseVotesQuery<TData>) => {
return useQuery<QueryVotesResponse, Error, TData>(["votesQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.votes(request);
}, options);
};
const useParams = <TData = QueryParamsResponse,>({
request,
options
}: UseParamsQuery<TData>) => {
return useQuery<QueryParamsResponse, Error, TData>(["paramsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.params(request);
}, options);
};
const useDeposit = <TData = QueryDepositResponse,>({
request,
options
}: UseDepositQuery<TData>) => {
return useQuery<QueryDepositResponse, Error, TData>(["depositQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.deposit(request);
}, options);
};
const useDeposits = <TData = QueryDepositsResponse,>({
request,
options
}: UseDepositsQuery<TData>) => {
return useQuery<QueryDepositsResponse, Error, TData>(["depositsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.deposits(request);
}, options);
};
const useTallyResult = <TData = QueryTallyResultResponse,>({
request,
options
}: UseTallyResultQuery<TData>) => {
return useQuery<QueryTallyResultResponse, Error, TData>(["tallyResultQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.tallyResult(request);
}, options);
};
return {
/** Proposal queries proposal details based on ProposalID. */useProposal,
/** Proposals queries all proposals based on given status. */useProposals,
/** Vote queries voted information based on proposalID, voterAddr. */useVote,
/** Votes queries votes of a given proposal. */useVotes,
/** Params queries all parameters of the gov module. */useParams,
/** Deposit queries single deposit information based proposalID, depositAddr. */useDeposit,
/** Deposits queries all deposits of a single proposal. */useDeposits,
/** TallyResult queries the tally of a proposal vote. */useTallyResult
};
};
Loading

0 comments on commit a4d7e88

Please sign in to comment.