@cosmology/ast
Version:
Cosmos TypeScript AST generation
72 lines (71 loc) • 2.65 kB
JavaScript
import * as ast from "@babel/types";
/**
*
* @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)),
]);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(helperCreatorName), callExpression),
]));
}
/**
*
* @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`)),
]);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
}
/**
*
* @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`)),
]);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
}