UNPKG

@cosmology/ast

Version:
125 lines (124 loc) 5.63 kB
import * as ast from "@babel/types"; import { CommentBlockBuilder } from "../../utils"; /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @returns */ export function createQueryHelperCreator(context, service, svcKey, methodKey, helperCreatorName) { const pkgImportName = context.ref.proto.package + "." + svcKey; context.addUtil("EndpointOrRpc"); context.addUtil("buildQuery"); const useGlobalDecoderRegistry = context.pluginValue("interfaces.enabled") && context.pluginValue("interfaces.useGlobalDecoderRegistry") && context.pluginValue("helperFunctions.enabled") && context.pluginValue("helperFunctions.useGlobalDecoderRegistry"); const callExpression = ast.callExpression(ast.identifier("buildQuery"), [ ast.objectExpression([ ast.objectProperty(ast.identifier("encode"), ast.memberExpression(ast.identifier(service.requestType), ast.identifier("encode"))), ast.objectProperty(ast.identifier("decode"), ast.memberExpression(ast.identifier(service.responseType), ast.identifier("decode"))), ast.objectProperty(ast.identifier("service"), ast.stringLiteral(pkgImportName)), ast.objectProperty(ast.identifier("method"), ast.stringLiteral(methodKey)), useGlobalDecoderRegistry && ast.objectProperty(ast.identifier("deps"), ast.arrayExpression([ ast.identifier(service.requestType), ast.identifier(service.responseType), ])), ].filter(Boolean)), ]); callExpression.typeParameters = ast.tsTypeParameterInstantiation([ ast.tsTypeReference(ast.identifier(service.requestType)), ast.tsTypeReference(ast.identifier(service.responseType)), ]); const exportDeclaration = ast.exportNamedDeclaration(ast.variableDeclaration("const", [ ast.variableDeclarator(ast.identifier(helperCreatorName), callExpression), ])); const commentBlock = new CommentBlockBuilder() .addLine(service.comment) .addLine(`@name ${helperCreatorName}`) .addLine(`@package ${context.ref.proto.package}`) .addLine(`@see proto service: ${context.ref.proto.package}.${methodKey}`) .addLine(service.options?.deprecated ? '@deprecated' : null) .build(); if (commentBlock) { exportDeclaration.leadingComments = [commentBlock]; } return exportDeclaration; } /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ export function createQueryHooks(context, service, methodKey, helperCreatorName, hookName) { context.addUtil("buildUseQuery"); const callExpression = ast.callExpression(ast.identifier("buildUseQuery"), [ ast.objectExpression([ ast.objectProperty(ast.identifier("builderQueryFn"), ast.identifier(helperCreatorName)), ast.objectProperty(ast.identifier("queryKeyPrefix"), ast.stringLiteral(`${methodKey}Query`)), ]), ]); callExpression.typeParameters = ast.tsTypeParameterInstantiation([ ast.tsTypeReference(ast.identifier(service.requestType)), ast.tsTypeReference(ast.identifier(service.responseType)), ]); const exportDeclaration = ast.exportNamedDeclaration(ast.variableDeclaration("const", [ ast.variableDeclarator(ast.identifier(hookName), callExpression), ])); // Add comments if service has a comment const commentBlock = new CommentBlockBuilder() .addLine(service.comment) .addLine(`@name ${hookName}`) .addLine(`@package ${context.ref.proto.package}`) .addLine(`@see proto service: ${context.ref.proto.package}.${methodKey}`) .addLine(service.options?.deprecated ? '@deprecated' : null) .build(); if (commentBlock) { exportDeclaration.leadingComments = [commentBlock]; } return exportDeclaration; } /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ export function createVueQueryHooks(context, service, methodKey, helperCreatorName, hookName) { context.addUtil("buildUseVueQuery"); const callExpression = ast.callExpression(ast.identifier("buildUseVueQuery"), [ ast.objectExpression([ ast.objectProperty(ast.identifier("builderQueryFn"), ast.identifier(helperCreatorName)), ast.objectProperty(ast.identifier("queryKeyPrefix"), ast.stringLiteral(`${methodKey}Query`)), ]), ]); callExpression.typeParameters = ast.tsTypeParameterInstantiation([ ast.tsTypeReference(ast.identifier(service.requestType)), ast.tsTypeReference(ast.identifier(service.responseType)), ]); const exportDeclaration = ast.exportNamedDeclaration(ast.variableDeclaration("const", [ ast.variableDeclarator(ast.identifier(hookName), callExpression), ])); // Add comments if service has a comment const commentBlock = new CommentBlockBuilder() .addLine(service.comment) .addLine(`@name ${hookName}`) .addLine(`@package ${context.ref.proto.package}`) .addLine(`@see proto service: ${context.ref.proto.package}.${methodKey}`) .addLine(service.options?.deprecated ? '@deprecated' : null) .build(); if (commentBlock) { exportDeclaration.leadingComments = [commentBlock]; } return exportDeclaration; }