UNPKG

@cosmology/ast

Version:
154 lines (153 loc) 7 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.createVueQueryHooks = exports.createQueryHooks = exports.createQueryHelperCreator = 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 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 utils_1.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; } exports.createQueryHelperCreator = createQueryHelperCreator; /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ 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 utils_1.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; } exports.createQueryHooks = createQueryHooks; /** * * @param context * @param service * @param methodKey e.g. "balance" * @param helperCreatorName e.g. "createGetBalance" * @param hookName e.g. "useGetBalance" * @returns */ 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 utils_1.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; } exports.createVueQueryHooks = createVueQueryHooks;