@cosmology/ast
Version:
Cosmos TypeScript AST generation
61 lines (60 loc) • 2.85 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 createQueryHelperCreator(context, service, svcKey, methodKey, helperCreatorName) {
const pkgImportName = context.ref.proto.package + '.' + svcKey;
context.addUtil("RpcResolver");
context.addUtil("buildQuery");
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)),
ast.objectProperty(ast.identifier("clientResolver"), ast.identifier("clientResolver"), false, true),
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(service.requestType)),
ast.tsTypeReference(ast.identifier(service.responseType)),
]);
const customHookArgumentsType = ast.tsTypeAnnotation(ast.tsTypeReference(ast.identifier("RpcResolver")));
const arg = ast.identifier("clientResolver");
arg.typeAnnotation = customHookArgumentsType;
arg.optional = true;
const arrowFuncExp = ast.arrowFunctionExpression([arg], callExpression);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(helperCreatorName), arrowFuncExp),
]));
}
/**
*
* @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)),
]);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
}