@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
43 lines (40 loc) • 2.31 kB
JavaScript
import { __decorate, __metadata } from '../../../external/tslib/tslib.es6.js';
import { ApiPropertyUUID } from '../../../decorator/api/property/uuid.decorator.js';
import { CamelCaseString } from '../../camel-case-string.utility.js';
import { CapitalizeString } from '../../capitalize-string.utility.js';
/**
* Creates a simple relationship DTO containing only an ID field.
* Used for representing related entities in API responses with minimal information.
* @param {IApiEntity<E>} entity - The entity metadata
* @param {EApiRouteType} method - The API route type (CREATE, DELETE, GET, etc.)
* @param {EApiDtoType} dtoType - The type of DTO (REQUEST, RESPONSE, etc.)
* @param {string} propertyName - The name of the relation property
* @param {{ relationDescription?: string }} [options] - Optional relation metadata used for the nested identifier schema
* @param {string} [options.relationDescription] - Relation-aware description to use for the nested id field.
* @returns {Type<unknown>} A generated DTO class for relation representation
* @template E - The entity type
*/
function DtoGenerateRelationResponse(entity, method, dtoType, propertyName, options = {}) {
const entityName = (entity.name ?? "").trim();
const relationDescriptionCandidate = options.relationDescription?.trim();
let relationDescription = propertyName;
if (relationDescriptionCandidate) {
relationDescription = relationDescriptionCandidate;
}
const shouldPrefixOwnerName = !!entityName && !relationDescription.toLocaleLowerCase().startsWith(`${entityName.toLocaleLowerCase()} `);
const identifierEntity = {
...entity,
name: shouldPrefixOwnerName ? `${entityName} ${relationDescription}` : relationDescription,
};
class GeneratedRelationDTO {
id;
}
__decorate([
ApiPropertyUUID({ entity: identifierEntity, isResponse: true }),
__metadata("design:type", String)
], GeneratedRelationDTO.prototype, "id", void 0);
Object.defineProperty(GeneratedRelationDTO, "name", { value: `${String(entity.name)}${CamelCaseString(method)}${CamelCaseString(dtoType)}${CapitalizeString(propertyName)}DTO` });
return GeneratedRelationDTO;
}
export { DtoGenerateRelationResponse };
//# sourceMappingURL=relation-response.utility.js.map