file module import { HttpStatus , ReflectMetadata } from '@nestjs/common' ; import * as lodash from 'lodash' ; import * as META from '../common/constants/meta.constant' ; import * as TEXT from '../common/constants/text.constant' ; import { TMessage } from '../common/types/interfaces/http.interface' ; interface IBuildDecoratorOption { errCode ? : HttpStatus ; successCode ? : HttpStatus ; errMessage ? : TMessage ; successMessage ? : TMessage ; usePaginate ? : boolean ; } interface IHandleOption { error ? : HttpStatus ; success ? : HttpStatus ; message : TMessage ; usePaginate ? : boolean ; } type THandleOption = TMessage | IHandleOption ; const buildHttpDecorator = ( options ) : MethodDecorator => { const { errMessage , successMessage , errCode , successCode , usePaginate } = options ; return ( _ , __ , descriptor ) => { if ( errCode ) { ReflectMetadata ( META . HTTP_ERROR_CODE , errCode ) ( descriptor . value ) ; } if ( successCode ) { ReflectMetadata ( META . HTTP_SUCCESS_CODE , successCode ) ( descriptor . value ) ; } if ( errMessage ) { ReflectMetadata ( META . HTTP_ERROR_MESSAGE , errMessage ) ( descriptor . value ) ; } if ( successMessage ) { ReflectMetadata ( META . HTTP_SUCCESS_MESSAGE , successMessage ) ( descriptor . value ) ; } if ( usePaginate ) { ReflectMetadata ( META . HTTP_RES_TRANSFORM_PAGINATE , true ) ( descriptor . value ) ; } return descriptor ; } ; } ; exports example HttpProcessor export const error = ( message , statusCode ? ) : MethodDecorator => { return buildHttpDecorator ( { errMessage : message , errCode : statusCode } ) ; } ; exports example HttpProcessor export const success = ( message , statusCode ? ) : MethodDecorator => { return buildHttpDecorator ( { successMessage : message , successCode : statusCode } ) ; } ; function description example HttpProcessor example HttpProcessor export function handle ( args ) ; export function handle ( ... args ) { const option = args [ 0 ] ; const isOption = ( value ) : value is IHandleOption => lodash . isObject ( value ) ; const message = isOption ( option ) ? option . message : '' ; const errMessage = message + TEXT . HTTP_ERROR_SUFFIX ; const successMessage = message + TEXT . HTTP_SUCCESS_SUFFIX ; const errCode = isOption ( option ) ? option . error : HttpStatus . NOT_FOUND ; const successCode = isOption ( option ) ? option . success : HttpStatus . NOT_FOUND ; const usePaginate = isOption ( option ) ? option . usePaginate : false ; return buildHttpDecorator ( { errCode , successCode , errMessage , successMessage , usePaginate } ) ; } exports example HttpProcessor export const paginate = ( ) : MethodDecorator => { return buildHttpDecorator ( { usePaginate : true } ) ; } ; exports description export const HttpProcessor = { error , success , handle , paginate } ;