@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
108 lines (105 loc) • 4.96 kB
JavaScript
import { __decorate, __metadata } from '../../external/tslib/tslib.es6.js';
import { DATE_CONSTANT } from '../../constant/date.constant.js';
import { EXCEPTION_DTO_CONSTANT } from '../../constant/dto/exception.constant.js';
import { NUMBER_CONSTANT } from '../../constant/number.constant.js';
import { ApiPropertyEnum } from '../../decorator/api/property/enum.decorator.js';
import { ApiPropertyNumber } from '../../decorator/api/property/number.decorator.js';
import { ApiPropertyString } from '../../decorator/api/property/string.decorator.js';
import { ApiPropertyUUID } from '../../decorator/api/property/uuid.decorator.js';
import '../../enum/decorator/api/action.enum.js';
import '../../enum/decorator/api/authentication-type.enum.js';
import '../../enum/decorator/api/controller/load-relations-strategy.enum.js';
import '../../enum/decorator/api/controller/request-transformer-type.enum.js';
import '../../enum/decorator/api/dto-type.enum.js';
import '../../enum/decorator/api/function/type.enum.js';
import '../../enum/decorator/api/on-type.enum.js';
import '../../enum/decorator/api/property/data-type.enum.js';
import '../../enum/decorator/api/property/date/identifier.enum.js';
import '../../enum/decorator/api/property/date/type.enum.js';
import '../../enum/decorator/api/property/desribe-type.enum.js';
import { EApiPropertyNumberType } from '../../enum/decorator/api/property/number-type.enum.js';
import { EApiPropertyStringType } from '../../enum/decorator/api/property/string-type.enum.js';
import '../../enum/decorator/api/route-type.enum.js';
import { HttpStatus } from '@nestjs/common';
import { CamelCaseString } from '../camel-case-string.utility.js';
const exceptionDtoCache = new Map();
/**
* Creates exception DTOs with standardized properties based on HTTP status codes.
* Generates a class with properties like correlationID, error name, message, status code, and timestamp,
* all properly decorated with Swagger and validation decorators.
* @param {HttpStatus} httpStatus - The HTTP status code for the exception
* @returns {Type<unknown>} A generated DTO class for the exception
*/
function DtoGenerateException(httpStatus) {
const cached = exceptionDtoCache.get(httpStatus);
if (cached) {
return cached;
}
const errorName = HttpStatus[httpStatus];
class GeneratedErrorDTO {
correlationID;
error = CamelCaseString(errorName);
message = "Error message";
statusCode = httpStatus;
timestamp;
}
__decorate([
ApiPropertyUUID({ entity: { name: "Correlation" }, isResponse: true }),
__metadata("design:type", String)
], GeneratedErrorDTO.prototype, "correlationID", void 0);
__decorate([
ApiPropertyString({
description: "name",
entity: { name: "Error" },
exampleValue: CamelCaseString(errorName),
format: EApiPropertyStringType.STRING,
isResponse: true,
maxLength: EXCEPTION_DTO_CONSTANT.MAXIMUM_ERROR_LENGTH,
minLength: EXCEPTION_DTO_CONSTANT.MINIMUM_ERROR_LENGTH,
pattern: "/^[a-zA-Z_ ]{3,64}$/",
}),
__metadata("design:type", String)
], GeneratedErrorDTO.prototype, "error", void 0);
__decorate([
ApiPropertyString({
description: "message",
entity: { name: "Error" },
exampleValue: "Error message",
format: EApiPropertyStringType.STRING,
isResponse: true,
maxLength: EXCEPTION_DTO_CONSTANT.MAXIMUM_ERROR_MESSAGE_LENGTH,
minLength: EXCEPTION_DTO_CONSTANT.MINIMUM_ERROR_MESSAGE_LENGTH,
pattern: "/^[a-zA-Z_ ]{3,64}$/",
}),
__metadata("design:type", String)
], GeneratedErrorDTO.prototype, "message", void 0);
__decorate([
ApiPropertyEnum({
description: "status code",
entity: { name: "Error" },
enum: HttpStatus,
enumName: "EHttpStatus",
exampleValue: httpStatus,
isResponse: true,
}),
__metadata("design:type", Number)
], GeneratedErrorDTO.prototype, "statusCode", void 0);
__decorate([
ApiPropertyNumber({
description: "timestamp",
entity: { name: "Error" },
exampleValue: Date.now(),
format: EApiPropertyNumberType.INTEGER,
isResponse: true,
maximum: DATE_CONSTANT.MAXIMUM_UNIX_TIME,
minimum: DATE_CONSTANT.MINIMUM_UNIX_TIME,
multipleOf: NUMBER_CONSTANT.ONE,
}),
__metadata("design:type", Number)
], GeneratedErrorDTO.prototype, "timestamp", void 0);
Object.defineProperty(GeneratedErrorDTO, "name", { value: `Exception${CamelCaseString(errorName)}DTO` });
exceptionDtoCache.set(httpStatus, GeneratedErrorDTO);
return GeneratedErrorDTO;
}
export { DtoGenerateException };
//# sourceMappingURL=generate-exception.utility.js.map