UNPKG

@cosmology/ast

Version:
85 lines (84 loc) 4.13 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.createPiniaStore = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("@cosmology/utils"); const firstLower = (s) => s = s.charAt(0).toLowerCase() + s.slice(1); const firstUpper = (s) => s = s.charAt(0).toUpperCase() + s.slice(1); function buildState(methods) { const fields = methods.map(x => t.objectProperty(t.identifier(x.name), t.tsAsExpression(t.identifier("{}"), t.tsUnionType([ t.tsTypeReference(t.identifier(`${x.method.responseType}SDKType`)), ])))); return t.arrowFunctionExpression([], t.blockStatement([ t.returnStatement(t.objectExpression(fields)) ])); } function buildAction(methods) { const fields = methods.map(x => t.objectMethod('method', t.identifier(`fetch${firstUpper(x.name)}`), [t.identifier(`param : ${x.method.requestType}SDKType`)], t.blockStatement([ t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier(x.name)), t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('lcdClient')), t.identifier(x.name)), [t.identifier('param')])))), t.returnStatement(t.memberExpression(t.thisExpression(), t.identifier(x.name))) ]), false, false, true)); return t.objectExpression(fields); } function buildGetter(method) { return t.objectExpression([t.objectMethod('method', t.identifier('lcdClient'), [], t.blockStatement([ t.variableDeclaration('const', [ t.variableDeclarator(t.identifier('requestClient'), t.memberExpression(t.callExpression(t.identifier('useEndpoint'), []), t.identifier('restClient'))), ]), t.returnStatement(t.newExpression(t.identifier('LCDQueryClient'), [ t.identifier('{ requestClient }') ])) ]))]); } function getStoreName(key) { const names = key.split('/'); return `use${firstUpper(names[0])}${firstUpper(names[1])}`; } const createPiniaStore = (context, service) => { context.addUtil('LCDClient'); context.addUtil('useEndpoint'); const key = context.ref.filename; const storeName = 'usePiniaStore'; // const storeName = getStoreName(key) const methods = Object.keys(service.methods ?? {}) .map(key => { const method = service.methods[key]; // console.log('method:', method) const name = (0, utils_1.camel)(key); return { name, method }; }); return t.exportNamedDeclaration(t.variableDeclaration('const', [ t.variableDeclarator(t.identifier(storeName), t.callExpression(t.identifier('defineStore'), [ t.identifier(`'${key}'`), t.objectExpression([ t.objectProperty(t.identifier('state'), buildState(methods)), t.objectProperty(t.identifier('getters'), buildGetter(methods)), t.objectProperty(t.identifier('actions'), buildAction(methods)), ]), ])) ])); }; exports.createPiniaStore = createPiniaStore;