import { Transform , TransformationType } from 'class-transformer' ; import { parsePhoneNumber } from 'libphonenumber-js' ; import { castArray , isArray , isNil , map , trim } from 'lodash' ; import { GeneratorProvider } from '../providers' ; description example ApiProperty IsString Trim returns constructor export function Trim ( ) { return Transform ( ( params ) => { const value = params . value as string [ ] | string ; if ( isArray ( value ) ) { return map ( value , ( v ) => trim ( v ) . replace ( /\s\s+/g , ' ' ) ) ; } return trim ( value ) . replace ( /\s\s+/g , ' ' ) ; } ) ; } export function ToBoolean ( ) { return Transform ( ( params ) => { switch ( params . value ) { case 'true' : return true ; case 'false' : return false ; default : return params . value ; } } , { toClassOnly : true } ) ; } description example IsNumber ToInt returns constructor export function ToInt ( ) { return Transform ( ( params ) => { const value = params . value as string ; return Number . parseInt ( value , 10 ) ; } , { toClassOnly : true } ) ; } description example IsNumber ToArray constructor export function ToArray ( ) { return Transform ( ( params ) => { const value = params . value ; if ( isNil ( value ) ) { return [ ] ; } return castArray ( value ) ; } , { toClassOnly : true } ) ; } export function ToLowerCase ( ) { return Transform ( ( params ) => { const value = params . value ; if ( ! value ) { return ; } if ( ! Array . isArray ( value ) ) { return value . toLowerCase ( ) ; } return value . map ( ( v ) => v . toLowerCase ( ) ) ; } , { toClassOnly : true , } ) ; } export function ToUpperCase ( ) { return Transform ( ( params ) => { const value = params . value ; if ( ! value ) { return ; } if ( ! Array . isArray ( value ) ) { return value . toUpperCase ( ) ; } return value . map ( ( v ) => v . toUpperCase ( ) ) ; } , { toClassOnly : true , } ) ; } export function S3UrlParser ( ) { return Transform ( ( params ) => { const key = params . value as string ; switch ( params . type ) { case TransformationType . CLASS_TO_PLAIN : return GeneratorProvider . getS3PublicUrl ( key ) ; case TransformationType . PLAIN_TO_CLASS : return GeneratorProvider . getS3Key ( key ) ; default : return key ; } } ) ; } export function PhoneNumberSerializer ( ) { return Transform ( ( params ) => parsePhoneNumber ( params . value as string ) . number ) ; }