UNPKG

@cosmology/ast

Version:
91 lines (90 loc) 4.28 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.createGRPCGatewayMsgClass = void 0; const utils_1 = require("../../../../utils"); const utils_2 = require("@cosmology/utils"); const rpc_1 = require("../utils/rpc"); const utils_3 = require("./utils"); const utils_4 = require("../utils"); const t = __importStar(require("@babel/types")); // fetchArgs will be used in method body's return statement expression. // Contains arguments to fm.fetchReq const getFetchReqArgs = (name, packageImport) => { const fetchArgs = []; // first argument of fetchReq const argTemplateLiteral = t.templateLiteral([ t.templateElement({ raw: '/' + packageImport + '/' + name, cooked: '/' + packageImport + '/' + name }, true) ], // quasis []); // adds proto path to fetchReq fetchArgs.push(argTemplateLiteral); // initReqProperties (contains information for initReq parameter in fetchReq) arguments: const initReqProperties = (0, utils_3.getInitReqProperties)(); const fetchArgsInitReqObj = t.objectExpression(initReqProperties); // adds initReq parameter to fetchReq fetchArgs.push(fetchArgsInitReqObj); return fetchArgs; }; const grpcGatewayMethodDefinition = (name, svc, packageImport, leadingComments) => { const requestType = svc.requestType; const responseType = svc.responseType; const fieldNames = Object.keys(svc.fields ?? {}); const hasParams = fieldNames.length > 0; const optional = (0, rpc_1.optionalBool)(hasParams, fieldNames); // first parameter in method // ex: static Send(request: MsgSend) // paramRequest is an object representing everything in brackets here const paramRequest = (0, utils_1.identifier)('request', t.tsTypeAnnotation(t.tsTypeReference(t.identifier(requestType))), optional); // fetchArgs will be used in method body's return statement expression. // Contains arguments to fm.fetchReq const fetchArgs = getFetchReqArgs(name, packageImport); // method's body const body = t.blockStatement([ t.returnStatement(t.callExpression(t.memberExpression(t.identifier('fm'), t.identifier('fetchReq')), fetchArgs)) ]); return (0, utils_1.classMethod)('method', t.identifier(name), [paramRequest, utils_3.initRequest], // params body, (0, rpc_1.returnReponseType)(responseType), leadingComments, false, true); }; const createGRPCGatewayMsgClass = (context, service) => { // adds import context.addUtil('fm'); const camelRpcMethods = context.pluginValue('rpcClients.camelCase'); const keys = Object.keys(service.methods ?? {}); const methods = keys .map((key) => { const method = service.methods[key]; const name = camelRpcMethods ? (0, utils_2.camel)(key) : key; const leadingComments = method.comment ? [(0, utils_1.commentBlock)((0, utils_4.processRpcComment)(method))] : []; return grpcGatewayMethodDefinition(name, method, context.ref.proto.package, leadingComments); }); return t.exportNamedDeclaration(t.classDeclaration(t.identifier(service.name), null, t.classBody([ ...methods, ]), [])); }; exports.createGRPCGatewayMsgClass = createGRPCGatewayMsgClass;