UNPKG

@cosmology/ast

Version:
135 lines (134 loc) 5.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createVueMsgHooks = exports.createMsgHooks = exports.createMsgHelperCreator = void 0; const ast = __importStar(require("@babel/types")); const utils_1 = require("../../utils"); /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @returns */ 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 utils_1.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; } exports.createMsgHelperCreator = createMsgHelperCreator; /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ 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 utils_1.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; } exports.createMsgHooks = createMsgHooks; /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ 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 utils_1.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; } exports.createVueMsgHooks = createVueMsgHooks;