denwa-nest-shared
Version:
Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client
231 lines • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MUTABLE_BY_KEY = exports.CheckFieldsRoles = exports.CHECK_FIELDS_ROLES_KEY = exports.Public = exports.GetCurrentUser = exports.GetCurrentRoles = exports.GetCurrentUserId = exports.Roles = exports.ApiValidationProperty = void 0;
exports.MutableBy = MutableBy;
const common_1 = require("@nestjs/common");
const graphql_1 = require("@nestjs/graphql");
const swagger_1 = require("@nestjs/swagger");
const class_validator_1 = require("class-validator");
const class_transformer_1 = require("class-transformer");
const const_1 = require("./const");
const lib_1 = require("./lib");
const BUILT_IN_TYPES = new Set([String, Number, Boolean, Object, Date, Array, Function]);
const isClassRef = (value) => typeof value === 'function' && !BUILT_IN_TYPES.has(value);
const resolveObjectType = (objectType, swaggerType) => {
const raw = objectType ?? swaggerType;
const candidate = Array.isArray(raw) ? raw[0] : raw;
return isClassRef(candidate) ? candidate : null;
};
const ApiValidationProperty = ({ type, enumObject, swaggerType, objectType, min, max, arrayMinSize, arrayMaxSize, divisibleBy, example, description, isSwaggerArray, isOptional = false, isUUID = false, isArray = false, isJson = false, isJWT = false, isUrl = false, isPositive = false, isNegative = false, isEmail = false, isMobilePhone = false, isPhoneNumber = false, isLatitude = false, isLongitude = false, isStrongPassword = false, isArrayNotEmpty = false, isArrayUnique = false, isDateString = false, }) => {
const decorators = [];
const schemaType = swaggerType ?? (objectType ? (isArray ? [objectType] : objectType) : undefined);
const swaggerData = {
example,
description,
enum: enumObject,
isArray: isSwaggerArray,
type: schemaType,
};
if (!enumObject) {
delete swaggerData.enum;
}
if (!isSwaggerArray) {
delete swaggerData.isArray;
}
if (!schemaType) {
delete swaggerData.type;
}
if (isOptional) {
decorators.push((0, swagger_1.ApiPropertyOptional)(swaggerData));
}
else {
decorators.push((0, swagger_1.ApiProperty)(swaggerData));
}
const nestedType = resolveObjectType(objectType, swaggerType);
if (nestedType) {
decorators.push((0, graphql_1.Field)(() => (isArray ? [nestedType] : nestedType), { nullable: isOptional }));
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
if (isArray) {
decorators.push((0, class_validator_1.IsArray)());
if (isArrayNotEmpty)
decorators.push((0, class_validator_1.ArrayNotEmpty)());
if (isArrayUnique)
decorators.push((0, class_validator_1.ArrayUnique)());
if (arrayMinSize !== undefined)
decorators.push((0, class_validator_1.ArrayMinSize)(arrayMinSize));
if (arrayMaxSize !== undefined)
decorators.push((0, class_validator_1.ArrayMaxSize)(arrayMaxSize));
}
decorators.push((0, class_validator_1.ValidateNested)({ each: isArray }));
decorators.push((0, class_transformer_1.Type)(() => nestedType));
return (0, common_1.applyDecorators)(...decorators);
}
if (type === 'object') {
throw new Error('ApiValidationProperty: `type: "object"` requires a nested DTO class in `objectType` or `swaggerType`');
}
switch (type) {
case 'int':
decorators.push((0, graphql_1.Field)(() => (isArray ? [graphql_1.Int] : graphql_1.Int), { nullable: isOptional }));
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
if (isArray) {
decorators.push((0, class_validator_1.IsArray)());
if (isArrayNotEmpty)
decorators.push((0, class_validator_1.ArrayNotEmpty)());
if (isArrayUnique)
decorators.push((0, class_validator_1.ArrayUnique)());
if (arrayMinSize !== undefined)
decorators.push((0, class_validator_1.ArrayMinSize)(arrayMinSize));
if (arrayMaxSize !== undefined)
decorators.push((0, class_validator_1.ArrayMaxSize)(arrayMaxSize));
}
decorators.push((0, class_validator_1.IsNumber)({}, { each: isArray }));
decorators.push((0, class_validator_1.IsInt)({ each: isArray }));
if (min !== undefined)
decorators.push((0, class_validator_1.Min)(min, { each: isArray }));
if (max !== undefined)
decorators.push((0, class_validator_1.Max)(max, { each: isArray }));
if (divisibleBy !== undefined)
decorators.push((0, class_validator_1.IsDivisibleBy)(divisibleBy, { each: isArray }));
break;
case 'float':
decorators.push((0, graphql_1.Field)(() => (isArray ? [graphql_1.Float] : graphql_1.Float), { nullable: isOptional }));
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
if (isArray) {
decorators.push((0, class_validator_1.IsArray)());
if (isArrayNotEmpty)
decorators.push((0, class_validator_1.ArrayNotEmpty)());
if (isArrayUnique)
decorators.push((0, class_validator_1.ArrayUnique)());
if (arrayMinSize !== undefined)
decorators.push((0, class_validator_1.ArrayMinSize)(arrayMinSize));
if (arrayMaxSize !== undefined)
decorators.push((0, class_validator_1.ArrayMaxSize)(arrayMaxSize));
}
decorators.push((0, class_validator_1.IsNumber)({}, { each: isArray }));
if (min !== undefined)
decorators.push((0, class_validator_1.Min)(min, { each: isArray }));
if (max !== undefined)
decorators.push((0, class_validator_1.Max)(max, { each: isArray }));
if (divisibleBy !== undefined)
decorators.push((0, class_validator_1.IsDivisibleBy)(divisibleBy, { each: isArray }));
break;
case 'date':
decorators.push((0, graphql_1.Field)(() => (isArray ? [Date] : Date), { nullable: isOptional }));
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
if (isArray) {
decorators.push((0, class_validator_1.IsArray)());
if (isArrayNotEmpty)
decorators.push((0, class_validator_1.ArrayNotEmpty)());
if (isArrayUnique)
decorators.push((0, class_validator_1.ArrayUnique)());
if (arrayMinSize !== undefined)
decorators.push((0, class_validator_1.ArrayMinSize)(arrayMinSize));
if (arrayMaxSize !== undefined)
decorators.push((0, class_validator_1.ArrayMaxSize)(arrayMaxSize));
}
decorators.push((0, class_validator_1.IsDate)({ each: isArray }));
decorators.push((0, class_transformer_1.Type)(() => Date));
break;
case 'boolean':
decorators.push((0, graphql_1.Field)({ nullable: isOptional }));
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
decorators.push((0, class_validator_1.IsBoolean)());
break;
case 'string':
default:
if (enumObject) {
decorators.push((0, graphql_1.Field)(() => (isArray ? [enumObject] : enumObject), { nullable: isOptional }));
}
else {
decorators.push((0, graphql_1.Field)(() => (isArray ? [String] : String), { nullable: isOptional }));
}
if (isOptional)
decorators.push((0, class_validator_1.IsOptional)());
if (isArray) {
decorators.push((0, class_validator_1.IsArray)());
if (isArrayNotEmpty)
decorators.push((0, class_validator_1.ArrayNotEmpty)());
if (isArrayUnique)
decorators.push((0, class_validator_1.ArrayUnique)());
if (arrayMinSize !== undefined)
decorators.push((0, class_validator_1.ArrayMinSize)(arrayMinSize));
if (arrayMaxSize !== undefined)
decorators.push((0, class_validator_1.ArrayMaxSize)(arrayMaxSize));
}
decorators.push((0, class_validator_1.IsString)({ each: isArray }));
if (isUUID)
decorators.push((0, class_validator_1.IsUUID)(undefined, { each: isArray }));
if (isEmail)
decorators.push((0, class_validator_1.IsEmail)(undefined, { each: isArray }));
if (isMobilePhone)
decorators.push((0, class_validator_1.IsMobilePhone)(undefined, undefined, { each: isArray }));
if (isPhoneNumber)
decorators.push((0, class_validator_1.IsPhoneNumber)(undefined, { each: isArray }));
if (isPositive)
decorators.push((0, class_validator_1.IsPositive)({ each: isArray }));
if (isNegative)
decorators.push((0, class_validator_1.IsNegative)({ each: isArray }));
if (min !== undefined)
decorators.push((0, class_validator_1.MinLength)(min, { each: isArray }));
if (max !== undefined)
decorators.push((0, class_validator_1.MaxLength)(max, { each: isArray }));
if (enumObject)
decorators.push((0, class_validator_1.IsEnum)(enumObject, { each: isArray }));
if (isJson)
decorators.push((0, class_validator_1.IsJSON)({ each: isArray }));
if (isJWT)
decorators.push((0, class_validator_1.IsJWT)({ each: isArray }));
if (isUrl)
decorators.push((0, class_validator_1.IsUrl)(undefined, { each: isArray }));
if (isLatitude)
decorators.push((0, class_validator_1.IsLatitude)({ each: isArray }));
if (isLongitude)
decorators.push((0, class_validator_1.IsLongitude)({ each: isArray }));
if (isStrongPassword)
decorators.push((0, class_validator_1.IsStrongPassword)(undefined, { each: isArray }));
if (isDateString)
decorators.push((0, class_validator_1.IsDateString)(undefined, { each: isArray }));
}
return (0, common_1.applyDecorators)(...decorators);
};
exports.ApiValidationProperty = ApiValidationProperty;
const Roles = (roles) => (0, common_1.SetMetadata)(const_1.ROLES_KEY, roles);
exports.Roles = Roles;
exports.GetCurrentUserId = (0, common_1.createParamDecorator)((_data, context) => {
const request = (0, lib_1.prepareRequestContext)(context);
return request?.user?.['id'];
});
exports.GetCurrentRoles = (0, common_1.createParamDecorator)((_data, context) => {
const request = (0, lib_1.prepareRequestContext)(context);
return (request?.user?.['roles']?.map((item) => {
if (typeof item === 'string')
return item;
return item.value;
}) ?? []);
});
exports.GetCurrentUser = (0, common_1.createParamDecorator)((data, context) => {
const request = (0, lib_1.prepareRequestContext)(context);
if (!data)
return request?.user;
return request?.user?.[data];
});
const Public = () => (0, common_1.SetMetadata)('isPublic', true);
exports.Public = Public;
exports.CHECK_FIELDS_ROLES_KEY = 'checkFieldsRoles';
const CheckFieldsRoles = (fieldsRolesMap) => (0, common_1.SetMetadata)(exports.CHECK_FIELDS_ROLES_KEY, fieldsRolesMap);
exports.CheckFieldsRoles = CheckFieldsRoles;
exports.MUTABLE_BY_KEY = 'mutableBy';
function MutableBy(roles) {
return (target, propertyKey) => {
const proto = (target.constructor?.prototype ?? target);
const existing = Reflect.getMetadata(exports.MUTABLE_BY_KEY, proto) ?? {};
const updated = { ...existing, [String(propertyKey)]: roles };
Reflect.defineMetadata(exports.MUTABLE_BY_KEY, updated, proto);
};
}
//# sourceMappingURL=decorators.js.map