@cosmology/ast
Version:
Cosmos TypeScript AST generation
161 lines (160 loc) • 7.57 kB
JavaScript
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.createScopedRpcTmFactory = exports.createScopedRpcFactory = exports.rpcTmNestedImportObject = exports.rpcNestedImportObject = exports.rpcRecursiveObjectProps = exports.rpcNewTmAwaitImport = exports.rpcNewAwaitImport = exports.rpcClassArguments = exports.rpcFuncArguments = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("../../../utils");
const utils_2 = require("@cosmology/utils");
const rpcFuncArguments = () => {
return [
(0, utils_1.objectPattern)([
t.objectProperty(t.identifier("rpc"), t.identifier("rpc"), false, true),
], t.tsTypeAnnotation(t.tsTypeLiteral([
t.tsPropertySignature(t.identifier("rpc"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("Rpc")))),
]))),
];
};
exports.rpcFuncArguments = rpcFuncArguments;
const rpcClassArguments = () => {
return [
t.objectExpression([
t.objectProperty(t.identifier("rpc"), t.identifier("rpc"), false, true),
]),
];
};
exports.rpcClassArguments = rpcClassArguments;
const rpcNewAwaitImport = (path, className) => {
return t.newExpression(t.memberExpression(t.awaitExpression(t.callExpression(t.import(), [t.stringLiteral(path)])), t.identifier(className), false), [t.identifier("rpc")]);
};
exports.rpcNewAwaitImport = rpcNewAwaitImport;
const rpcNewTmAwaitImport = (path, className, clientName = "client", isNew = false) => {
const calleeExpr = t.memberExpression(t.awaitExpression(t.callExpression(t.import(), [t.stringLiteral(path)])), t.identifier(className), false);
const argsExpr = [t.identifier(clientName)];
return isNew
? t.newExpression(calleeExpr, argsExpr)
: t.callExpression(calleeExpr, argsExpr);
};
exports.rpcNewTmAwaitImport = rpcNewTmAwaitImport;
const rpcRecursiveObjectProps = (names, leaf) => {
const [name, ...rest] = names;
let baseComponent;
if (names.length === 1) {
baseComponent = leaf ? leaf : t.identifier(name);
}
else {
baseComponent = (0, exports.rpcRecursiveObjectProps)(rest, leaf);
}
return t.objectExpression([
t.objectProperty(t.identifier(name), baseComponent),
]);
};
exports.rpcRecursiveObjectProps = rpcRecursiveObjectProps;
const rpcNestedImportObject = (obj, className, options) => {
if (typeof obj === "string") {
const path = (0, utils_2.restoreExtension)(obj, options?.restoreImportExtension);
return (0, exports.rpcNewAwaitImport)(path, className);
}
const keys = Object.keys(obj);
return t.objectExpression(keys.map((name) => {
return t.objectProperty(t.identifier(name), (0, exports.rpcNestedImportObject)(obj[name], className, options));
}));
};
exports.rpcNestedImportObject = rpcNestedImportObject;
const rpcTmNestedImportObject = (obj, className, clientName, options) => {
if (typeof obj === "string") {
const path = (0, utils_2.restoreExtension)(obj, options?.restoreImportExtension);
return (0, exports.rpcNewTmAwaitImport)(path, className, clientName, options?.isNew);
}
const keys = Object.keys(obj);
return t.objectExpression(keys.map((name) => {
return t.objectProperty(t.identifier(name), (0, exports.rpcTmNestedImportObject)(obj[name], className, clientName, options));
}));
};
exports.rpcTmNestedImportObject = rpcTmNestedImportObject;
const createScopedRpcFactory = (obj, identifier, className, options) => {
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(identifier), t.arrowFunctionExpression((0, exports.rpcFuncArguments)(),
//
(0, exports.rpcNestedImportObject)(obj, className, options), true)),
]));
};
exports.createScopedRpcFactory = createScopedRpcFactory;
const createScopedRpcTmFactory = (context, obj, identifier) => {
const newClientType = context.pluginValue("rpcClients.useConnectComet");
const extensions = context.pluginValue("rpcClients.extensions");
let functionParams;
const returnStatement = t.returnStatement((0, exports.rpcTmNestedImportObject)(obj, extensions ? "createRpcQueryExtension" : "createClientImpl", extensions ? "client" : "rpc", {
restoreImportExtension: context.options.restoreImportExtension,
}));
let functionStatements;
let awaitClientCreation;
if (extensions) {
// TODO: remove tendermint34client and options if not needed
if (newClientType) {
// use connectComet for dynamic client
context.addUtil("connectComet");
awaitClientCreation = t.callExpression(t.identifier("connectComet"), [t.identifier("rpcEndpoint")]);
}
else {
// use tendermint34 client
context.addUtil("Tendermint34Client");
awaitClientCreation = t.callExpression(t.memberExpression(t.identifier("Tendermint34Client"), t.identifier("connect")), [t.identifier("rpcEndpoint")]);
}
context.addUtil("HttpEndpoint");
context.addUtil("QueryClient");
functionParams = [
(0, utils_1.objectPattern)([
t.objectProperty(t.identifier("rpcEndpoint"), t.identifier("rpcEndpoint"), false, true),
], t.tsTypeAnnotation(t.tsTypeLiteral([
t.tsPropertySignature(t.identifier("rpcEndpoint"), t.tsTypeAnnotation(t.tsUnionType([
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("HttpEndpoint")),
]))),
]))),
];
functionStatements = [
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("tmClient"), t.awaitExpression(awaitClientCreation)),
]),
/////
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("client"), t.newExpression(t.identifier("QueryClient"), [
t.identifier("tmClient"),
])),
]),
returnStatement,
];
}
else {
functionParams = (0, exports.rpcFuncArguments)();
functionStatements = [returnStatement];
}
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(
// createRPCQueryClient
t.identifier(identifier), t.arrowFunctionExpression(functionParams, t.blockStatement(functionStatements), true)),
]));
};
exports.createScopedRpcTmFactory = createScopedRpcTmFactory;
;