Skip to content

Commit

Permalink
fix: pass in ProtoServiceMethod type, use SigningClientResolver type,…
Browse files Browse the repository at this point in the history
… remove type aliases.
  • Loading branch information
j-yw committed Oct 16, 2024
1 parent 1254615 commit 620568c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 76 deletions.
13 changes: 2 additions & 11 deletions packages/ast/src/clients/helper-funcs/msg-funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import * as ast from "@babel/types";
import { ProtoService } from "@cosmology/types";
import { GenericParseContext } from "../../encoding";

// ** The end result of generation should include the following imports
// TODO: I need to add the imports for generation instead of hard coding them, it should comes from the UTILS in telescope
/**
import { buildTx, ISigningClient } from "../../../helper-func-types";
import { MsgSend, MsgMultiSend } from "./tx";
import { toConverters, toEncoders } from "@interchainjs/cosmos/utils";
import { buildUseMutation } from "../../../react-query";
*/

/**
*
* @param context
Expand All @@ -28,6 +19,7 @@ export function createMsgHelperCreator(
context.addUtil("buildTx");
context.addUtil("ISigningClient");
context.addUtil("buildUseMutation");
context.addUtil("SigningClientResolver");
const callExpression = ast.callExpression(ast.identifier("buildTx"), [
ast.objectExpression([
ast.objectProperty(
Expand Down Expand Up @@ -57,8 +49,7 @@ export function createMsgHelperCreator(
ast.tsTypeReference(ast.identifier(methodKey)),
]);
const customHookArgumentsType = ast.tsTypeAnnotation(
//TODO: Improvements, Figure out how to write ast code to generate the function expression below instead of hard coding the strong.
ast.tsTypeReference(ast.identifier(" () => ISigningClient | undefined"))
ast.tsTypeReference(ast.identifier("SigningClientResolver"))
);
const arg = ast.identifier("getSigningClient");
arg.typeAnnotation = customHookArgumentsType;
Expand Down
65 changes: 12 additions & 53 deletions packages/ast/src/clients/helper-funcs/query-funcs.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
import * as ast from "@babel/types";
import { ProtoService } from "@cosmology/types";
import { ProtoServiceMethod } from "@cosmology/types";
import { GenericParseContext } from "../../encoding";

// ** The end result of generation should include the following imports
// TODO: I need to add the imports for generation instead of hard coding them, it should comes from the UTILS in telescope
/**
import { buildQuery } from "../../../helper-func-types";
import { Rpc } from "../../../helpers";
import { UseQueryParams, buildUseQuery } from "../../../react-query";
import { QueryBalanceRequest, QueryBalanceResponse, QueryAllBalancesRequest, QueryAllBalancesResponse, QuerySpendableBalancesRequest, QuerySpendableBalancesResponse, QueryTotalSupplyRequest, QueryTotalSupplyResponse, QuerySupplyOfRequest, QuerySupplyOfResponse, QueryParamsRequest, QueryParamsResponse, QueryDenomMetadataRequest, QueryDenomMetadataResponse, QueryDenomsMetadataRequest, QueryDenomsMetadataResponse, QueryDenomOwnersRequest, QueryDenomOwnersResponse } from "./query";
*/

export function createTypeAliases(
context: GenericParseContext,
service: ProtoService,
methodKey?: string,
helperCreatorName?: string
) {
context.addUtil("buildQuery");
context.addUtil("Rpc");
context.addUtil("UseQueryParams");
context.addUtil("buildUseQuery");

return ast.exportNamedDeclaration(
ast.tsTypeAliasDeclaration(
ast.identifier(`Use${methodKey}Query`),
// No type parameters for type alias
// To add a type parameter, use ast.tsTypeParameterDeclaration([]),
null,
ast.tsTypeReference(
ast.identifier("UseQueryParams"),
ast.tsTypeParameterInstantiation([
ast.tsTypeReference(
ast.identifier(service.methods[methodKey].requestType)
),
ast.tsTypeReference(
ast.identifier(service.methods[methodKey].responseType)
),
])
)
)
);
}
/**
*
* @param context
Expand All @@ -52,29 +12,30 @@ export function createTypeAliases(
*/
export function createQueryHelperCreator(
context: GenericParseContext,
service: ProtoService,
service: ProtoServiceMethod,
methodKey?: string,
helperCreatorName?: string
) {
context.addUtil("SigningClientResolver");
const callExpression = ast.callExpression(ast.identifier("buildQuery"), [
ast.objectExpression([
ast.objectProperty(
ast.identifier("reqEncoderFn"),
ast.memberExpression(
ast.identifier(`Query${methodKey}Request`),
ast.identifier(service.requestType),
ast.identifier("encode")
)
),
ast.objectProperty(
ast.identifier("resDecoderFn"),
ast.memberExpression(
ast.identifier(`Query${methodKey}Response`),
ast.identifier(service.responseType),
ast.identifier("decode")
)
),
ast.objectProperty(
ast.identifier("service"),
// TODO: Does this value needs to change?
// Does this value needs to change?
ast.stringLiteral("cosmos.bank.v1beta1.Query")
),
ast.objectProperty(
Expand All @@ -88,13 +49,12 @@ export function createQueryHelperCreator(
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(`Query${methodKey}Request`)),
ast.tsTypeReference(ast.identifier(`Query${methodKey}Response`)),
ast.tsTypeReference(ast.identifier(service.requestType)),
ast.tsTypeReference(ast.identifier(service.responseType)),
]);

const customHookArgumentsType = ast.tsTypeAnnotation(
//TODO: Improvements, Figure out how to write ast code to generate the function expression below instead of hard coding the string.
ast.tsTypeReference(ast.identifier("() => ISigningClient | undefined"))
ast.tsTypeReference(ast.identifier("SigningClientResolver"))
);
const arg = ast.identifier("getRpcInstance");

Expand Down Expand Up @@ -123,12 +83,11 @@ export function createQueryHelperCreator(
*/
export function createQueryHooks(
context: GenericParseContext,
service: ProtoService,
service: ProtoServiceMethod,
methodKey?: string,
helperCreatorName?: string,
hookName?: string
) {
// return ast.returnStatement(ast.identifier(hookName));
const callExpression = ast.callExpression(ast.identifier("buildUseQuery"), [
ast.objectExpression([
ast.objectProperty(
Expand All @@ -142,8 +101,8 @@ export function createQueryHooks(
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(`Query${methodKey}Request`)),
ast.tsTypeReference(ast.identifier(`Query${methodKey}Response`)),
ast.tsTypeReference(ast.identifier(service.requestType)),
ast.tsTypeReference(ast.identifier(service.responseType)),
]);
return ast.exportNamedDeclaration(
ast.variableDeclaration("const", [
Expand Down
15 changes: 3 additions & 12 deletions packages/telescope/src/generators/create-query-funcs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { buildAllImports, getDepsFromQueries } from "../imports";
import { Bundler } from "../bundler";
import {
createQueryHelperCreator,
createQueryHooks,
createTypeAliases,
} from "@cosmology/ast";
import { createQueryHelperCreator, createQueryHooks } from "@cosmology/ast";
import { getNestedProto, isRefIncluded } from "@cosmology/proto-parser";
import { parse } from "../parse";
import { TelescopeBuilder } from "../builder";
Expand Down Expand Up @@ -100,16 +96,11 @@ export const plugin = (builder: TelescopeBuilder, bundler: Bundler) => {
const { creator: helperCreatorName, hook: hookName } =
getHelperFuncName(bundlerFile.package, methodKey, mapper, "get");

//gen type aliases
asts.push(
createTypeAliases(ctx.generic, svc, methodKey, helperCreatorName)
);

// gen helper funcs
asts.push(
createQueryHelperCreator(
ctx.generic,
svc,
svc.methods[methodKey],
methodKey,
helperCreatorName
)
Expand All @@ -124,7 +115,7 @@ export const plugin = (builder: TelescopeBuilder, bundler: Bundler) => {
asts.push(
createQueryHooks(
ctx.generic,
svc,
svc.methods[methodKey],
methodKey,
helperCreatorName,
hookName
Expand Down
1 change: 1 addition & 0 deletions packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const UTILS: { [key: string]: UtilValue } = {
toBase64: '@cosmjs/encoding',
toDuration: '__helpers__',
toTimestamp: '__helpers__',
SigningClientResolver:'__helpers__',
toUtf8: '@cosmjs/encoding',
useQuery: '@tanstack/react-query',
useRpcEndpoint: '__react-query__',
Expand Down

0 comments on commit 620568c

Please sign in to comment.