@cosmology/ast
Version:
Cosmos TypeScript AST generation
106 lines (105 loc) • 4.19 kB
JavaScript
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 createMsgHelperCreator(context, service, helperCreatorName) {
context.addUtil("buildTx");
const callExpression = ast.callExpression(ast.identifier("buildTx"), [
ast.objectExpression([
ast.objectProperty(ast.identifier("msg"), ast.identifier(service.requestType)),
].filter(Boolean)),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(service.requestType)),
]);
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}.${service.name}`)
.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 createMsgHooks(context, service, helperCreatorName, hookName) {
context.addUtil("buildUseMutation");
const callExpression = ast.callExpression(ast.identifier("buildUseMutation"), [
ast.objectExpression([
ast.objectProperty(ast.identifier("builderMutationFn"), ast.identifier(helperCreatorName)),
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(service.requestType)),
ast.tsTypeReference(ast.identifier(`Error`)),
]);
const exportDeclaration = ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
const commentBlock = new CommentBlockBuilder()
.addLine(service.comment)
.addLine(`@name ${hookName}`)
.addLine(`@package ${context.ref.proto.package}`)
.addLine(`@see proto service: ${context.ref.proto.package}.${service.name}`)
.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 createVueMsgHooks(context, service, helperCreatorName, hookName) {
context.addUtil("buildUseVueMutation");
const callExpression = ast.callExpression(ast.identifier("buildUseVueMutation"), [
ast.objectExpression([
ast.objectProperty(ast.identifier("builderMutationFn"), ast.identifier(helperCreatorName)),
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(service.requestType)),
ast.tsTypeReference(ast.identifier(`Error`)),
]);
const exportDeclaration = ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
const commentBlock = new CommentBlockBuilder()
.addLine(service.comment)
.addLine(`@name ${hookName}`)
.addLine(`@package ${context.ref.proto.package}`)
.addLine(`@see proto service: ${context.ref.proto.package}.${service.name}`)
.addLine(service.options?.deprecated ? '@deprecated' : null)
.build();
if (commentBlock) {
exportDeclaration.leadingComments = [commentBlock];
}
return exportDeclaration;
}