UNPKG

@cuba-platform/front-generator

Version:
75 lines 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrowFunc = exports.cubaAppCallFunc = exports.createService = exports.generateServices = void 0; const ts = require("typescript"); const ts_helpers_1 = require("../../../common/ts-helpers"); const method_params_type_1 = require("./method-params-type"); const import_utils_1 = require("../import-utils"); const constants_1 = require("../../../common/constants"); const ts_shorthands_1 = require("../../../common/ts-shorthands"); const REST_SERVICES_VAR_NAME = 'restServices'; function generateServices(services, ctx) { const importDec = (0, import_utils_1.importDeclaration)([constants_1.CUBA_APP_TYPE, constants_1.FETCH_OPTIONS_TYPE], constants_1.CUBA_APP_MODULE_SPEC); const servicesResult = createServices(services, ctx); const includes = (0, import_utils_1.createIncludes)(servicesResult.importInfos); return (0, ts_helpers_1.renderTSNodes)([importDec, ...includes, ...servicesResult.paramTypes, servicesResult.servicesNode], '\n\n'); } exports.generateServices = generateServices; function createServices(services, ctx) { const assignmentList = []; const paramTypes = []; const importInfos = []; services .reduce((acc, curr) => { if (!acc.find(restService => restService.name === curr.name)) acc.push(curr); return acc; }, []) .forEach(srv => { const createItemResult = createService(srv, ctx); assignmentList.push(createItemResult.node); createItemResult.methodParamsTypes.forEach(mpt => paramTypes.push(mpt)); importInfos.push(...createItemResult.imports); }); const variableDeclaration = ts.createVariableDeclaration(REST_SERVICES_VAR_NAME, undefined, ts.createObjectLiteral(assignmentList, true)); const servicesNode = ts.createVariableStatement([(0, ts_shorthands_1.exportModifier)()], [variableDeclaration]); return { servicesNode, paramTypes, importInfos }; } function createService(service, ctx) { const methodAssignments = []; const methodParamsTypes = []; const imports = []; const serviceName = service.name; (0, method_params_type_1.collectMethods)(service.methods).forEach((mwo) => { let paramTypeName = undefined; //if any of overloads has params - create special type for it if (mwo.methods.some(m => m.params.length > 0)) { const { paramTypeNode, importInfos, name } = (0, method_params_type_1.createMethodParamsType)(mwo.methods, serviceName, ctx); imports.push(...importInfos); methodParamsTypes.push(paramTypeNode); paramTypeName = name; } const cubaCallFunc = cubaAppCallFunc('invokeService', paramTypeName, undefined, [serviceName, mwo.methodName], []); methodAssignments.push(ts.createPropertyAssignment(mwo.methodName, cubaCallFunc)); }); const node = ts.createPropertyAssignment(serviceName, ts.createObjectLiteral(methodAssignments, true)); return { node, methodParamsTypes, imports }; } exports.createService = createService; function cubaAppCallFunc(method, paramTypeName, functionType, cubaAppCallParams, typeArguments) { const methodSubBody = arrowFunc(paramTypeName ? [(0, ts_shorthands_1.param)('params', paramTypeName)] : [], functionType, cubaAppMethodCall(method, cubaAppCallParams, paramTypeName != null, typeArguments)); return arrowFunc([(0, ts_shorthands_1.param)(constants_1.CUBA_APP_NAME, constants_1.CUBA_APP_TYPE), (0, ts_shorthands_1.param)(constants_1.FETCH_OPTIONS_NAME + '?', constants_1.FETCH_OPTIONS_TYPE)], undefined, methodSubBody); } exports.cubaAppCallFunc = cubaAppCallFunc; function cubaAppMethodCall(methodName, signature, withParams, typeArguments) { const argumentsArray = signature.map(s => (0, ts_shorthands_1.str)(s)); argumentsArray.push(withParams ? (0, ts_shorthands_1.idn)('params') : (0, ts_shorthands_1.idn)('{}')); argumentsArray.push((0, ts_shorthands_1.idn)(constants_1.FETCH_OPTIONS_NAME)); return ts.createBlock([ts.createReturn(ts.createCall(ts.createPropertyAccess((0, ts_shorthands_1.idn)(constants_1.CUBA_APP_NAME), (0, ts_shorthands_1.idn)(methodName)), typeArguments, argumentsArray)) ], true); } function arrowFunc(parameters, functionType, body) { return ts.createArrowFunction(undefined, undefined, parameters, functionType, undefined, body); } exports.arrowFunc = arrowFunc; //# sourceMappingURL=services-generation.js.map