@cosmology/ast
Version:
Cosmos TypeScript AST generation
92 lines (91 loc) • 4 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.createMsgHooks = exports.createMsgHelperCreator = void 0;
const ast = __importStar(require("@babel/types"));
/**
*
* @param context
* @param service
* @param methodKey e.g. "balance"
* @param helperCreatorName e.g. "createGetBalance"
* @returns
*/
function createMsgHelperCreator(context, service, helperCreatorName) {
context.addUtil("buildTx");
context.addUtil("ISigningClient");
context.addUtil("buildUseMutation");
context.addUtil("SigningClientResolver");
context.addUtil("toEncoders");
context.addUtil("toConverters");
const callExpression = ast.callExpression(ast.identifier("buildTx"), [
ast.objectExpression([
ast.objectProperty(ast.identifier("clientResolver"), ast.identifier("clientResolver"), false, true),
ast.objectProperty(ast.identifier("typeUrl"), ast.memberExpression(ast.identifier(service.requestType), ast.identifier("typeUrl"))),
ast.objectProperty(ast.identifier("encoders"), ast.callExpression(ast.identifier("toEncoders"), [
ast.identifier(service.requestType),
])),
ast.objectProperty(ast.identifier("converters"), ast.callExpression(ast.identifier("toConverters"), [
ast.identifier(service.requestType),
])),
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(service.requestType)),
]);
const customHookArgumentsType = ast.tsTypeAnnotation(ast.tsTypeReference(ast.identifier("SigningClientResolver")));
const arg = ast.identifier("clientResolver");
arg.typeAnnotation = customHookArgumentsType;
arg.optional = true;
const arrowFuncExp = ast.arrowFunctionExpression([arg], callExpression);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(helperCreatorName), arrowFuncExp),
]));
}
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) {
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`)),
]);
return ast.exportNamedDeclaration(ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
]));
}
exports.createMsgHooks = createMsgHooks;
;