UNPKG

@rxap/schematics-ts-morph

Version:

This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in

146 lines 8.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceFormTableActionRule = CoerceFormTableActionRule; const schematics_utilities_1 = require("@rxap/schematics-utilities"); const ts_morph_1 = require("@rxap/ts-morph"); const utilities_1 = require("@rxap/utilities"); const ts_morph_2 = require("ts-morph"); const coerce_table_action_1 = require("./coerce-table-action"); const toMappingObjectOptions = { aliasFnc: (key, value) => { if (['rowId', '_rowId', '__rowId'].includes(value)) { return '__rowId'; } return value; }, }; function CoerceFormTableActionRule(options) { const { type, loadFrom, tableName, tsMorphTransform = utilities_1.noop, scope, formInitial, formComponent, } = options; return (0, coerce_table_action_1.CoerceTableActionRule)(Object.assign(Object.assign({}, options), { tsMorphTransform: (project, sourceFile, classDeclaration) => { var _a, _b, _c; const tableInterfaceName = `I${(0, schematics_utilities_1.classify)(tableName)}`; (0, ts_morph_1.CoerceImports)(sourceFile, { moduleSpecifier: '@angular/core', namedImports: ['ChangeDetectorRef', 'Inject', 'INJECTOR', 'Injector'], }); const openFormWindowMethod = `Open${(0, schematics_utilities_1.classify)(formComponent)}WindowMethod`; (0, ts_morph_1.CoerceImports)(sourceFile, { moduleSpecifier: `../../${(0, utilities_1.dasherize)(formComponent)}/open-${(0, utilities_1.dasherize)(formComponent)}-window.method`, namedImports: [openFormWindowMethod], }); (0, ts_morph_1.CoerceImports)(sourceFile, { namedImports: ['firstValueFrom'], moduleSpecifier: 'rxjs', }); if (loadFrom === null || loadFrom === void 0 ? void 0 : loadFrom.operationId) { (0, ts_morph_1.CoerceImports)(sourceFile, { moduleSpecifier: (0, ts_morph_1.OperationIdToClassRemoteMethodImportPath)(loadFrom.operationId, (_a = loadFrom.scope) !== null && _a !== void 0 ? _a : scope), namedImports: [(0, ts_morph_1.OperationIdToRemoteMethodClassName)(loadFrom.operationId)], }); if (formInitial) { (0, ts_morph_1.CoerceImports)(sourceFile, { moduleSpecifier: (0, ts_morph_1.OperationIdToResponseClassImportPath)(loadFrom.operationId, (_b = loadFrom.scope) !== null && _b !== void 0 ? _b : scope), namedImports: [(0, ts_morph_1.OperationIdToResponseClassName)(loadFrom.operationId)], }); } } (0, ts_morph_1.CoerceDependencyInjection)(sourceFile, { injectionToken: openFormWindowMethod, parameterName: 'openFormWindow', scope: ts_morph_2.Scope.Private, module: ts_morph_1.Module.ANGULAR, }); (0, ts_morph_1.CoerceDependencyInjection)(sourceFile, { injectionToken: 'INJECTOR', parameterName: 'injector', type: 'Injector', scope: ts_morph_2.Scope.Private, module: ts_morph_1.Module.ANGULAR, }); (0, ts_morph_1.CoerceDependencyInjection)(sourceFile, { injectionToken: 'ChangeDetectorRef', parameterName: 'cdr', scope: ts_morph_2.Scope.Private, module: ts_morph_1.Module.ANGULAR, }); if (loadFrom === null || loadFrom === void 0 ? void 0 : loadFrom.operationId) { (0, ts_morph_1.CoerceDependencyInjection)(sourceFile, { injectionToken: (0, ts_morph_1.OperationIdToRemoteMethodClassName)(loadFrom.operationId), parameterName: 'getInitial', scope: ts_morph_2.Scope.Private, module: ts_morph_1.Module.ANGULAR, }); } const statements = []; statements.push(`console.log(\`action row type: ${type}\`, parameters);`); statements.push(`const { __rowId: rowId } = parameters;`); if (loadFrom === null || loadFrom === void 0 ? void 0 : loadFrom.operationId) { if (loadFrom.body) { (0, ts_morph_1.CoerceMappingClassMethod)(sourceFile, classDeclaration, { name: 'getBody', parameterType: tableInterfaceName, returnType: (0, ts_morph_1.OperationIdToRequestBodyClassName)(loadFrom.operationId), mapping: loadFrom.body, mappingOptions: toMappingObjectOptions, }); (0, ts_morph_1.CoerceImports)(sourceFile, { namedImports: [(0, ts_morph_1.OperationIdToRequestBodyClassName)(loadFrom.operationId)], moduleSpecifier: (0, ts_morph_1.OperationIdToRequestBodyClassImportPath)(loadFrom.operationId, scope), }); statements.push(`const requestBody = this.getBody(parameters);`); } if (loadFrom.parameters) { (0, ts_morph_1.CoerceMappingClassMethod)(sourceFile, classDeclaration, { name: 'getParameters', parameterType: tableInterfaceName, returnType: (0, ts_morph_1.OperationIdToParameterClassName)(loadFrom.operationId), mapping: loadFrom.parameters, mappingOptions: toMappingObjectOptions, }); (0, ts_morph_1.CoerceImports)(sourceFile, { namedImports: [(0, ts_morph_1.OperationIdToParameterClassName)(loadFrom.operationId)], moduleSpecifier: (0, ts_morph_1.OperationIdToParameterClassImportPath)(loadFrom.operationId, scope), }); statements.push(`const requestParameters = this.getParameters(parameters);`); } if (loadFrom.body && loadFrom.parameters) { statements.push(`const initial = await this.getInitial.call({ parameters: requestParameters, requestBody });`); } else if (loadFrom.body) { statements.push(`const initial = await this.getInitial.call({ requestBody });`); } else if (loadFrom.parameters) { statements.push(`const initial = await this.getInitial.call({ parameters: requestParameters });`); } else { statements.push(`const initial = parameters;`); } } else { if (formInitial) { statements.push(`const initial = parameters;`); } else { statements.push(`const initial = {};`); } } statements.push(`this.cdr.markForCheck();`); if (typeof formInitial === 'object') { (0, ts_morph_1.CoerceMappingClassMethod)(sourceFile, classDeclaration, { name: 'toInitial', parameterType: (loadFrom === null || loadFrom === void 0 ? void 0 : loadFrom.operationId) ? (0, ts_morph_1.OperationIdToResponseClassName)(loadFrom.operationId) : tableInterfaceName, mapping: formInitial, mappingOptions: toMappingObjectOptions, // TODO : use the form interface as return type returnType: 'any', }); statements.push(`return firstValueFrom(this.openFormWindow.call(this.toInitial(initial), {injector: this.injector, context: { rowId }}));`); } else { statements.push(`return firstValueFrom(this.openFormWindow.call(initial, {injector: this.injector, context: { rowId }}));`); } return Object.assign({ statements, isAsync: true, scope: ts_morph_2.Scope.Public, returnType: 'Promise<any>' }, ((_c = tsMorphTransform(project, sourceFile, classDeclaration)) !== null && _c !== void 0 ? _c : {})); } })); } //# sourceMappingURL=coerce-form-table-action.js.map