UNPKG

@cosmology/ast

Version:
138 lines (137 loc) 7.23 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.createScopedGrpcWebMsgFactory = exports.createScopedGrpcWebFactory = exports.grpcNestedImportObject = exports.grpcWebNewAwaitImport = exports.grpcFuncArguments = exports.grpcWebClientScaffold = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("../../../utils"); const utils_2 = require("@cosmology/utils"); const grpcWebClientScaffold = () => { return [ t.expressionStatement(t.assignmentExpression("=", t.identifier('endpoint'), t.callExpression(t.memberExpression(t.identifier('endpoint'), t.identifier('replace')), [ t.regExpLiteral("\\/*$"), t.stringLiteral("") ]))), t.variableDeclaration("const", [ t.variableDeclarator(t.objectPattern([ t.objectProperty(t.identifier("GrpcWebImpl"), t.identifier("GrpcWebImpl"), false, true) ]), t.awaitExpression(t.callExpression(t.import(), [ t.stringLiteral("../cosmos/app/v1alpha1/query.rpc.Query") ]))) ]), t.variableDeclaration("let", [ t.variableDeclarator(t.identifier('grpcWeb')), ]), t.ifStatement(t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier('document'), true), t.stringLiteral("undefined")), t.blockStatement([ t.expressionStatement(t.assignmentExpression("=", t.identifier("grpcWeb"), t.newExpression(t.identifier("GrpcWebImpl"), [ t.identifier("endpoint"), t.objectExpression([ t.objectProperty(t.identifier("transport"), t.callExpression(t.memberExpression(t.identifier("grpc"), t.identifier("CrossBrowserHttpTransport")), [ t.objectExpression([ t.objectProperty(t.identifier("withCredentials"), t.booleanLiteral(false)) ]) ])) ]) ]))) ]), t.ifStatement(t.logicalExpression("&&", t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("navigator"), true), t.stringLiteral("undefined")), t.binaryExpression("===", t.memberExpression(t.identifier("navigator"), t.identifier("product")), t.stringLiteral("ReactNative"))), t.blockStatement([ t.expressionStatement(t.assignmentExpression("=", t.identifier("grpcWeb"), t.newExpression(t.identifier("GrpcWebImpl"), [ t.identifier("endpoint"), t.objectExpression([ t.objectProperty(t.identifier("transport"), t.callExpression(t.identifier("NodeHttpTransport"), [])) ]) ]))) ]), t.blockStatement([ t.expressionStatement(t.assignmentExpression("=", t.identifier("grpcWeb"), t.newExpression(t.identifier("GrpcWebImpl"), [ t.identifier("endpoint"), t.objectExpression([ t.objectProperty(t.identifier("transport"), t.callExpression(t.identifier("NodeHttpTransport"), [])) ]) ]))) ]))), ]; }; exports.grpcWebClientScaffold = grpcWebClientScaffold; const grpcFuncArguments = () => { return [ (0, utils_1.objectPattern)([ t.objectProperty(t.identifier('endpoint'), t.identifier('endpoint'), false, true) ], t.tsTypeAnnotation(t.tsTypeLiteral([ t.tsPropertySignature(t.identifier('endpoint'), t.tsTypeAnnotation(t.tsStringKeyword())) ]))) ]; }; exports.grpcFuncArguments = grpcFuncArguments; const grpcWebNewAwaitImport = (path, className, options) => { return t.newExpression(t.memberExpression(t.awaitExpression(t.callExpression(t.import(), [ t.stringLiteral((0, utils_2.restoreExtension)(path, options?.restoreImportExtension)) ])), t.identifier(className), false), [ t.identifier('grpcWeb') ]); }; exports.grpcWebNewAwaitImport = grpcWebNewAwaitImport; const grpcNestedImportObject = (obj, className, options) => { //make className dynamic based on object if (typeof obj === 'string') { const serviceType = obj.split(".").pop(); switch (serviceType) { case "Query": // console.log("This is a Query RPC."); break; case "Service": className = 'ServiceClientImpl'; // console.log("This is a Service RPC."); break; case "msg": className = 'MsgClientImpl'; // console.log("This is a Message RPC."); break; default: console.log("grpc service error!! This should not happend. Undefined service type"); } return (0, exports.grpcWebNewAwaitImport)(obj, className, options); } const keys = Object.keys(obj); return t.objectExpression(keys.map(name => { return t.objectProperty(t.identifier(name), (0, exports.grpcNestedImportObject)(obj[name], className, options)); })); }; exports.grpcNestedImportObject = grpcNestedImportObject; const createScopedGrpcWebFactory = (context, obj, identifier) => { context.addUtil('grpc'); context.addUtil('NodeHttpTransport'); return t.exportNamedDeclaration(t.variableDeclaration('const', [ t.variableDeclarator( // createGrpcWebQueryClient t.identifier(identifier), t.arrowFunctionExpression((0, exports.grpcFuncArguments)(), t.blockStatement((0, exports.grpcWebClientScaffold)().concat(t.returnStatement((0, exports.grpcNestedImportObject)(obj, 'QueryClientImpl', context.options)))), true)) ])); }; exports.createScopedGrpcWebFactory = createScopedGrpcWebFactory; const createScopedGrpcWebMsgFactory = (obj, identifier, className, options) => { return t.exportNamedDeclaration(t.variableDeclaration('const', [ t.variableDeclarator(t.identifier(identifier), t.arrowFunctionExpression((0, exports.grpcFuncArguments)(), // t.blockStatement((0, exports.grpcWebClientScaffold)().concat(t.returnStatement((0, exports.grpcNestedImportObject)(obj, className, options)))), true)) ])); }; exports.createScopedGrpcWebMsgFactory = createScopedGrpcWebMsgFactory;