UNPKG

@elsikora/nestjs-crud-automator

Version:

A library for automating the creation of CRUD operations in NestJS.

41 lines (38 loc) 1.65 kB
import { ErrorException } from '../error/exception.utility.js'; import { IsEntityConstructor } from '../is/entity/constructor.utility.js'; import { IsEntityFactory } from '../is/entity/factory.utility.js'; import { IsEntityLiteral } from '../is/entity/literal.utility.js'; /** * Resolves property decorator entity references immediately or throws if unavailable. * @param {TApiPropertyEntity} entity - Entity reference or resolver. * @param {string} decoratorName - Decorator name for error context. * @returns {IApiBaseEntity | Type<IApiBaseEntity>} Normalized entity reference. */ function ResolvePropertyEntity(entity, decoratorName) { // eslint-disable-next-line @elsikora/sonar/use-type-alias const resolved = TryResolvePropertyEntity(entity); if (!resolved) { throw ErrorException(`Entity for ${decoratorName} could not be resolved. Provide the entity class or a factory that returns it.`); } return resolved; } /** * Attempts to resolve the entity reference without throwing when the resolver returns undefined. * @param {TApiPropertyEntity} entity - Entity reference or factory. * @returns {IApiBaseEntity | Type<IApiBaseEntity> | undefined} Resolved entity or undefined when not ready. */ function TryResolvePropertyEntity(entity) { if (IsEntityConstructor(entity)) { return entity; } if (IsEntityLiteral(entity)) { return entity; } if (IsEntityFactory(entity)) { const factory = entity; return factory(); } return undefined; } export { ResolvePropertyEntity, TryResolvePropertyEntity }; //# sourceMappingURL=property-entity.utility.js.map