UNPKG

denwa-nest-shared

Version:

Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client

223 lines • 10.5 kB
import { applyDecorators, createParamDecorator, SetMetadata, } from '@nestjs/common'; import { Field, Float, Int } from '@nestjs/graphql'; import { ApiProperty as SwaggerApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { ArrayMaxSize, ArrayMinSize, ArrayNotEmpty, ArrayUnique, IsArray, IsBoolean, IsDate, IsEmail, IsEnum, IsInt, IsJSON, IsJWT, IsLatitude, IsLongitude, IsMobilePhone, IsNegative, IsNumber, IsOptional, IsPhoneNumber, IsPositive, IsString, IsStrongPassword, IsUrl, IsUUID, Max, MaxLength, Min, MinLength, IsDivisibleBy, IsDateString, ValidateNested, } from 'class-validator'; import { Type } from 'class-transformer'; import { ROLES_KEY } from './const'; import { prepareRequestContext } from './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; }; export 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(ApiPropertyOptional(swaggerData)); } else { decorators.push(SwaggerApiProperty(swaggerData)); } const nestedType = resolveObjectType(objectType, swaggerType); if (nestedType) { decorators.push(Field(() => (isArray ? [nestedType] : nestedType), { nullable: isOptional })); if (isOptional) decorators.push(IsOptional()); if (isArray) { decorators.push(IsArray()); if (isArrayNotEmpty) decorators.push(ArrayNotEmpty()); if (isArrayUnique) decorators.push(ArrayUnique()); if (arrayMinSize !== undefined) decorators.push(ArrayMinSize(arrayMinSize)); if (arrayMaxSize !== undefined) decorators.push(ArrayMaxSize(arrayMaxSize)); } decorators.push(ValidateNested({ each: isArray })); decorators.push(Type(() => nestedType)); return 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(Field(() => (isArray ? [Int] : Int), { nullable: isOptional })); if (isOptional) decorators.push(IsOptional()); if (isArray) { decorators.push(IsArray()); if (isArrayNotEmpty) decorators.push(ArrayNotEmpty()); if (isArrayUnique) decorators.push(ArrayUnique()); if (arrayMinSize !== undefined) decorators.push(ArrayMinSize(arrayMinSize)); if (arrayMaxSize !== undefined) decorators.push(ArrayMaxSize(arrayMaxSize)); } decorators.push(IsNumber({}, { each: isArray })); decorators.push(IsInt({ each: isArray })); if (min !== undefined) decorators.push(Min(min, { each: isArray })); if (max !== undefined) decorators.push(Max(max, { each: isArray })); if (divisibleBy !== undefined) decorators.push(IsDivisibleBy(divisibleBy, { each: isArray })); break; case 'float': decorators.push(Field(() => (isArray ? [Float] : Float), { nullable: isOptional })); if (isOptional) decorators.push(IsOptional()); if (isArray) { decorators.push(IsArray()); if (isArrayNotEmpty) decorators.push(ArrayNotEmpty()); if (isArrayUnique) decorators.push(ArrayUnique()); if (arrayMinSize !== undefined) decorators.push(ArrayMinSize(arrayMinSize)); if (arrayMaxSize !== undefined) decorators.push(ArrayMaxSize(arrayMaxSize)); } decorators.push(IsNumber({}, { each: isArray })); if (min !== undefined) decorators.push(Min(min, { each: isArray })); if (max !== undefined) decorators.push(Max(max, { each: isArray })); if (divisibleBy !== undefined) decorators.push(IsDivisibleBy(divisibleBy, { each: isArray })); break; case 'date': decorators.push(Field(() => (isArray ? [Date] : Date), { nullable: isOptional })); if (isOptional) decorators.push(IsOptional()); if (isArray) { decorators.push(IsArray()); if (isArrayNotEmpty) decorators.push(ArrayNotEmpty()); if (isArrayUnique) decorators.push(ArrayUnique()); if (arrayMinSize !== undefined) decorators.push(ArrayMinSize(arrayMinSize)); if (arrayMaxSize !== undefined) decorators.push(ArrayMaxSize(arrayMaxSize)); } decorators.push(IsDate({ each: isArray })); decorators.push(Type(() => Date)); break; case 'boolean': decorators.push(Field({ nullable: isOptional })); if (isOptional) decorators.push(IsOptional()); decorators.push(IsBoolean()); break; case 'string': default: if (enumObject) { decorators.push(Field(() => (isArray ? [enumObject] : enumObject), { nullable: isOptional })); } else { decorators.push(Field(() => (isArray ? [String] : String), { nullable: isOptional })); } if (isOptional) decorators.push(IsOptional()); if (isArray) { decorators.push(IsArray()); if (isArrayNotEmpty) decorators.push(ArrayNotEmpty()); if (isArrayUnique) decorators.push(ArrayUnique()); if (arrayMinSize !== undefined) decorators.push(ArrayMinSize(arrayMinSize)); if (arrayMaxSize !== undefined) decorators.push(ArrayMaxSize(arrayMaxSize)); } decorators.push(IsString({ each: isArray })); if (isUUID) decorators.push(IsUUID(undefined, { each: isArray })); if (isEmail) decorators.push(IsEmail(undefined, { each: isArray })); if (isMobilePhone) decorators.push(IsMobilePhone(undefined, undefined, { each: isArray })); if (isPhoneNumber) decorators.push(IsPhoneNumber(undefined, { each: isArray })); if (isPositive) decorators.push(IsPositive({ each: isArray })); if (isNegative) decorators.push(IsNegative({ each: isArray })); if (min !== undefined) decorators.push(MinLength(min, { each: isArray })); if (max !== undefined) decorators.push(MaxLength(max, { each: isArray })); if (enumObject) decorators.push(IsEnum(enumObject, { each: isArray })); if (isJson) decorators.push(IsJSON({ each: isArray })); if (isJWT) decorators.push(IsJWT({ each: isArray })); if (isUrl) decorators.push(IsUrl(undefined, { each: isArray })); if (isLatitude) decorators.push(IsLatitude({ each: isArray })); if (isLongitude) decorators.push(IsLongitude({ each: isArray })); if (isStrongPassword) decorators.push(IsStrongPassword(undefined, { each: isArray })); if (isDateString) decorators.push(IsDateString(undefined, { each: isArray })); } return applyDecorators(...decorators); }; export const Roles = (roles) => SetMetadata(ROLES_KEY, roles); export const GetCurrentUserId = createParamDecorator((_data, context) => { const request = prepareRequestContext(context); return request?.user?.['id']; }); export const GetCurrentRoles = createParamDecorator((_data, context) => { const request = prepareRequestContext(context); return (request?.user?.['roles']?.map((item) => { if (typeof item === 'string') return item; return item.value; }) ?? []); }); export const GetCurrentUser = createParamDecorator((data, context) => { const request = prepareRequestContext(context); if (!data) return request?.user; return request?.user?.[data]; }); export const Public = () => SetMetadata('isPublic', true); export const CHECK_FIELDS_ROLES_KEY = 'checkFieldsRoles'; export const CheckFieldsRoles = (fieldsRolesMap) => SetMetadata(CHECK_FIELDS_ROLES_KEY, fieldsRolesMap); export const MUTABLE_BY_KEY = 'mutableBy'; export function MutableBy(roles) { return (target, propertyKey) => { const proto = (target.constructor?.prototype ?? target); const existing = Reflect.getMetadata(MUTABLE_BY_KEY, proto) ?? {}; const updated = { ...existing, [String(propertyKey)]: roles }; Reflect.defineMetadata(MUTABLE_BY_KEY, updated, proto); }; } //# sourceMappingURL=decorators.js.map