From 620568c0e5d44b96d39c88a6293b16507a42ab9f Mon Sep 17 00:00:00 2001 From: j-yw <32629001+j-yw@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:17:46 +0800 Subject: [PATCH] fix: pass in ProtoServiceMethod type, use SigningClientResolver type, remove type aliases. --- .../ast/src/clients/helper-funcs/msg-funcs.ts | 13 +--- .../src/clients/helper-funcs/query-funcs.ts | 65 ++++--------------- .../src/generators/create-query-funcs.ts | 15 +---- packages/telescope/src/utils/index.ts | 1 + 4 files changed, 18 insertions(+), 76 deletions(-) diff --git a/packages/ast/src/clients/helper-funcs/msg-funcs.ts b/packages/ast/src/clients/helper-funcs/msg-funcs.ts index 196c21c07..d3d003b08 100644 --- a/packages/ast/src/clients/helper-funcs/msg-funcs.ts +++ b/packages/ast/src/clients/helper-funcs/msg-funcs.ts @@ -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 @@ -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( @@ -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; diff --git a/packages/ast/src/clients/helper-funcs/query-funcs.ts b/packages/ast/src/clients/helper-funcs/query-funcs.ts index aa487e796..9da13bb09 100644 --- a/packages/ast/src/clients/helper-funcs/query-funcs.ts +++ b/packages/ast/src/clients/helper-funcs/query-funcs.ts @@ -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 @@ -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( @@ -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"); @@ -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( @@ -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", [ diff --git a/packages/telescope/src/generators/create-query-funcs.ts b/packages/telescope/src/generators/create-query-funcs.ts index 32fa779da..1200af97b 100644 --- a/packages/telescope/src/generators/create-query-funcs.ts +++ b/packages/telescope/src/generators/create-query-funcs.ts @@ -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"; @@ -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 ) @@ -124,7 +115,7 @@ export const plugin = (builder: TelescopeBuilder, bundler: Bundler) => { asts.push( createQueryHooks( ctx.generic, - svc, + svc.methods[methodKey], methodKey, helperCreatorName, hookName diff --git a/packages/telescope/src/utils/index.ts b/packages/telescope/src/utils/index.ts index 4b9fd5e09..ad7ad0ff3 100644 --- a/packages/telescope/src/utils/index.ts +++ b/packages/telescope/src/utils/index.ts @@ -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__',