@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
110 lines • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildUpstreamGetByIdParametersImplementation = BuildUpstreamGetByIdParametersImplementation;
exports.BuiltGetByIdDtoDataMapperImplementation = BuiltGetByIdDtoDataMapperImplementation;
exports.CoerceGetByIdOperation = CoerceGetByIdOperation;
const ts_morph_1 = require("@rxap/ts-morph");
const utilities_1 = require("@rxap/utilities");
const ts_morph_2 = require("ts-morph");
const coerce_get_operation_1 = require("./coerce-get-operation");
function BuildUpstreamGetByIdParametersImplementation(classDeclaration, moduleSourceFile, dto, options) {
return () => {
const { idProperty } = options;
return idProperty ? `{ parameters: { ${idProperty.name} } }` : '';
};
}
function UpstreamMapper(sourceProperty, propertyList, idProperty) {
var _a;
const mapper = {};
if (idProperty) {
mapper[idProperty.name] = idProperty.name;
}
for (const property of propertyList.filter(p => !idProperty || p.name !== idProperty.name).map(p => (0, ts_morph_1.NormalizeDataClassProperty)(p))) {
mapper[property.name] = `${sourceProperty}.${(_a = property.source) !== null && _a !== void 0 ? _a : property.name}`;
if (property.isType) {
// property is a complex object
if (property.isArray) {
mapper[property.name] = w => {
var _a;
w.write(`${sourceProperty}.${(_a = property.source) !== null && _a !== void 0 ? _a : property.name}.map(item => (`);
ts_morph_2.Writers.object(UpstreamMapper('item', property.memberList))(w);
w.write('))');
};
}
else {
mapper[property.name] = ts_morph_2.Writers.object(UpstreamMapper(mapper[property.name], property.memberList));
}
}
}
return mapper;
}
function BuiltGetByIdDtoDataMapperImplementation(classDeclaration, moduleSourceFile, dto, options) {
const { propertyList = [], upstream, isArray, idProperty, } = options;
return () => {
if (upstream) {
if (isArray) {
return w => {
w.write('data.map(item => (');
ts_morph_2.Writers.object(UpstreamMapper('data', propertyList, idProperty))(w);
w.write('))');
};
}
else {
return ts_morph_2.Writers.object(UpstreamMapper('data', propertyList, idProperty));
}
}
else {
if (isArray) {
return '[]';
}
if (idProperty) {
return `{ ${idProperty.name} }`;
}
return '{}';
}
};
}
function CoerceGetByIdOperation(options) {
var _a, _b;
const { controllerName, paramList = [], propertyList = [], idProperty = { name: 'uuid', type: 'string' }, operationName = 'getById', nestModule, buildUpstreamGetParametersImplementation = BuildUpstreamGetByIdParametersImplementation, builtDtoDataMapperImplementation = BuiltGetByIdDtoDataMapperImplementation, } = options;
if (idProperty) {
/**
* If the module is not specified. This controller has an own module. Else the
* module is originated by another controller.
*
* **Example**
* true:
* The controller ReportDetailsController should be extended with getById Operation.
* And the controller is used in the module ReportDetailsModule
*
* name = "report-details"
* module = undefined
*
* false:
* The controller ReportDetailsNotificationController should be extend with getById Operation.
* And the controller ise used in the module ReportDetailsModule
*
* name = "notification"
* module = "report-details"
*/
const isFirstBornSibling = !nestModule || nestModule === controllerName;
if (isFirstBornSibling) {
(0, utilities_1.CoerceArrayItems)(propertyList, [{
name: idProperty.name,
type: (_a = idProperty.type) !== null && _a !== void 0 ? _a : 'string',
isArray: idProperty.isArray,
}], (a, b) => a.name === b.name, true);
}
(0, utilities_1.CoerceArrayItems)(paramList, [{
name: idProperty.name,
type: idProperty.type,
fromParent: (_b = idProperty.fromParent) !== null && _b !== void 0 ? _b : !isFirstBornSibling,
}], (a, b) => a.name === b.name, true);
}
return (0, coerce_get_operation_1.CoerceGetOperation)(Object.assign(Object.assign({}, options), { operationName,
paramList,
propertyList,
buildUpstreamGetParametersImplementation,
builtDtoDataMapperImplementation }));
}
//# sourceMappingURL=coerce-get-by-id-operation.js.map