diff --git a/packages/common/pipes/file/parse-file.pipe.ts b/packages/common/pipes/file/parse-file.pipe.ts index d2ea60e505b..c73fabf54ce 100644 --- a/packages/common/pipes/file/parse-file.pipe.ts +++ b/packages/common/pipes/file/parse-file.pipe.ts @@ -2,7 +2,7 @@ import { Injectable, Optional } from '../../decorators/core'; import { HttpStatus } from '../../enums'; import { PipeTransform } from '../../interfaces/features/pipe-transform.interface'; import { HttpErrorByCode } from '../../utils/http-error-by-code.util'; -import { isEmpty, isObject, isUndefined } from '../../utils/shared.utils'; +import { isEmptyArray, isObject, isUndefined } from '../../utils/shared.utils'; import { FileValidator } from './file-validator.interface'; import { ParseFileOptions } from './parse-file-options.interface'; @@ -60,9 +60,8 @@ export class ParseFilePipe implements PipeTransform { } private thereAreNoFilesIn(value: any): boolean { - const isEmptyArray = Array.isArray(value) && isEmpty(value); - const isEmptyObject = isObject(value) && isEmpty(Object.keys(value)); - return isUndefined(value) || isEmptyArray || isEmptyObject; + const isEmptyObject = isObject(value) && isEmptyArray(Object.keys(value)); + return isUndefined(value) || isEmptyArray(value) || isEmptyObject; } protected async validate(file: any): Promise { diff --git a/packages/common/test/utils/shared.utils.spec.ts b/packages/common/test/utils/shared.utils.spec.ts index d7c0de0eac3..d9e81b2f2cc 100644 --- a/packages/common/test/utils/shared.utils.spec.ts +++ b/packages/common/test/utils/shared.utils.spec.ts @@ -3,6 +3,7 @@ import { addLeadingSlash, isConstructor, isEmpty, + isEmptyArray, isFunction, isNil, isNumber, @@ -27,7 +28,13 @@ describe('Shared utils', () => { it('should return false when object is not undefined', () => { expect(isUndefined({})).to.be.false; }); + it('should return false for falsy values like false, 0, or empty string', () => { + expect(isUndefined(false)).to.be.false; + expect(isUndefined(0)).to.be.false; + expect(isUndefined('')).to.be.false; + }); }); + describe('isFunction', () => { it('should return true when obj is function', () => { expect(isFunction(() => ({}))).to.be.true; @@ -37,16 +44,19 @@ describe('Shared utils', () => { expect(isFunction(undefined)).to.be.false; }); }); + describe('isObject', () => { it('should return true when obj is object', () => { expect(isObject({})).to.be.true; }); + it('should return false when object is not object', () => { expect(isObject(3)).to.be.false; expect(isObject(null)).to.be.false; expect(isObject(undefined)).to.be.false; }); }); + describe('isPlainObject', () => { it('should return true when obj is plain object', () => { expect(isPlainObject({})).to.be.true; @@ -66,7 +76,12 @@ describe('Shared utils', () => { expect(isPlainObject(new Date())).to.be.false; expect(isPlainObject(new Foo(1))).to.be.false; }); + it('should return false for objects with custom prototypes', () => { + function CustomObject() {} + expect(isPlainObject(new CustomObject())).to.be.false; + }); }); + describe('isString', () => { it('should return true when val is a string', () => { expect(isString('true')).to.be.true; @@ -78,6 +93,7 @@ describe('Shared utils', () => { expect(isString(undefined)).to.be.false; }); }); + describe('isSymbol', () => { it('should return true when val is a Symbol', () => { expect(isSymbol(Symbol())).to.be.true; @@ -88,7 +104,11 @@ describe('Shared utils', () => { expect(isSymbol(null)).to.be.false; expect(isSymbol(undefined)).to.be.false; }); + it('should return false for invalid Symbol objects', () => { + expect(isSymbol(Object(Symbol()))).to.be.false; + }); }); + describe('isNumber', () => { it('should return true when val is a number or NaN', () => { expect(isNumber(1)).to.be.true; @@ -98,14 +118,18 @@ describe('Shared utils', () => { expect(isNumber(0b1)).to.be.true; // binary notation expect(isNumber(0x1)).to.be.true; // hexadecimal notation expect(isNumber(NaN)).to.be.true; + expect(isNumber(Infinity)).to.be.true; + expect(isNumber(-Infinity)).to.be.true; }); it('should return false when val is not a number', () => { // expect(isNumber(1n)).to.be.false; // big int (available on ES2020) expect(isNumber('1')).to.be.false; // string expect(isNumber(undefined)).to.be.false; // nullish expect(isNumber(null)).to.be.false; // nullish + expect(isNumber(new Number(123))).to.be.false; // number }); }); + describe('isConstructor', () => { it('should return true when string is equal to constructor', () => { expect(isConstructor('constructor')).to.be.true; @@ -113,7 +137,13 @@ describe('Shared utils', () => { it('should return false when string is not equal to constructor', () => { expect(isConstructor('nope')).to.be.false; }); + it('should return false for non-string values', () => { + expect(isConstructor(null)).to.be.false; + expect(isConstructor(undefined)).to.be.false; + expect(isConstructor(123)).to.be.false; + }); }); + describe('addLeadingSlash', () => { it('should return the validated path ("add / if not exists")', () => { expect(addLeadingSlash('nope')).to.be.eql('/nope'); @@ -128,7 +158,13 @@ describe('Shared utils', () => { expect(addLeadingSlash(null!)).to.be.eql(''); expect(addLeadingSlash(undefined)).to.be.eql(''); }); + it('should handle paths with special characters', () => { + expect(addLeadingSlash('path-with-special-chars!@#$%^&*()')).to.eql( + '/path-with-special-chars!@#$%^&*()', + ); + }); }); + describe('normalizePath', () => { it('should remove all trailing slashes at the end of the path', () => { expect(normalizePath('path/')).to.be.eql('/path'); @@ -146,6 +182,7 @@ describe('Shared utils', () => { expect(normalizePath(undefined)).to.be.eql('/'); }); }); + describe('isNil', () => { it('should return true when obj is undefined or null', () => { expect(isNil(undefined)).to.be.true; @@ -154,17 +191,64 @@ describe('Shared utils', () => { it('should return false when object is not undefined and null', () => { expect(isNil('3')).to.be.false; }); + it('should return false for falsy values like false, 0, or empty string', () => { + expect(isNil(false)).to.be.false; + expect(isNil(0)).to.be.false; + expect(isNil('')).to.be.false; + }); }); + describe('isEmpty', () => { it('should return true when array is empty or not exists', () => { expect(isEmpty([])).to.be.true; - expect(isEmpty(null)).to.be.true; - expect(isEmpty(undefined)).to.be.true; }); + it('should return false when array is not empty', () => { expect(isEmpty([1, 2])).to.be.false; }); + it('should return false for non-array values', () => { + expect(isEmpty({})).to.be.false; + expect(isEmpty('')).to.be.false; + expect(isEmpty(0)).to.be.false; + expect(isEmpty(false)).to.be.false; + expect(isEmpty(Symbol())).to.be.false; + expect(isEmpty(() => {})).to.be.false; + }); }); + + describe('isEmptyArray', () => { + it('should return true when array is empty or not exists', () => { + expect(isEmptyArray([])).to.be.true; + expect(isEmptyArray(null)).to.be.true; + expect(isEmptyArray(undefined)).to.be.true; + }); + + it('should return false when array is not empty', () => { + expect(isEmptyArray([1, 2])).to.be.false; + expect(isEmptyArray(['a', 'b', 'c'])).to.be.false; + expect(isEmptyArray([{}])).to.be.false; + }); + + it('should return false for non-array values', () => { + expect(isEmptyArray({})).to.be.false; + expect(isEmptyArray('')).to.be.false; + expect(isEmptyArray(0)).to.be.false; + expect(isEmptyArray(false)).to.be.false; + expect(isEmptyArray(Symbol())).to.be.false; + expect(isEmptyArray(() => {})).to.be.false; + }); + + it('should return false for array-like objects', () => { + expect(isEmptyArray({ length: 0 })).to.be.false; + expect(isEmptyArray({ length: 1 })).to.be.false; + }); + + it('should return false for sparse arrays', () => { + const sparseArray = new Array(3); + expect(isEmptyArray(sparseArray)).to.be.false; + }); + }); + describe('stripEndSlash', () => { it('should strip end slash if present', () => { expect(stripEndSlash('/cats/')).to.equal('/cats'); diff --git a/packages/common/utils/shared.utils.ts b/packages/common/utils/shared.utils.ts index e936c8d2448..5925ade577e 100644 --- a/packages/common/utils/shared.utils.ts +++ b/packages/common/utils/shared.utils.ts @@ -1,10 +1,10 @@ -export const isUndefined = (obj: any): obj is undefined => +export const isUndefined = (obj: unknown): obj is undefined => typeof obj === 'undefined'; -export const isObject = (fn: any): fn is object => +export const isObject = (fn: unknown): fn is object => !isNil(fn) && typeof fn === 'object'; -export const isPlainObject = (fn: any): fn is object => { +export const isPlainObject = (fn: unknown): fn is object => { if (!isObject(fn)) { return false; } @@ -37,15 +37,39 @@ export const normalizePath = (path?: string): string => : '/' + path.replace(/\/+$/, '') : '/'; -export const stripEndSlash = (path: string) => - path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path; +export const stripEndSlash = (path: string): string => + path.endsWith('/') ? path.slice(0, -1) : path; -export const isFunction = (val: any): val is Function => +export const isFunction = (val: unknown): val is Function => typeof val === 'function'; -export const isString = (val: any): val is string => typeof val === 'string'; -export const isNumber = (val: any): val is number => typeof val === 'number'; -export const isConstructor = (val: any): boolean => val === 'constructor'; -export const isNil = (val: any): val is null | undefined => + +export const isString = (val: unknown): val is string => + typeof val === 'string'; + +export const isNumber = (val: unknown): val is number => + typeof val === 'number'; + +export const isConstructor = (val: unknown): boolean => val === 'constructor'; + +export const isNil = (val: unknown): val is null | undefined => isUndefined(val) || val === null; -export const isEmpty = (array: any): boolean => !(array && array.length > 0); -export const isSymbol = (val: any): val is symbol => typeof val === 'symbol'; + +export const isEmpty = (value: unknown): boolean => { + if (isNil(value)) { + return true; + } + if (Array.isArray(value)) { + return value.length === 0; + } + return false; +}; + +export const isEmptyArray = (array: unknown): boolean => { + if (isNil(array)) { + return true; + } + return Array.isArray(array) && array.length === 0; +}; + +export const isSymbol = (val: unknown): val is symbol => + typeof val === 'symbol'; diff --git a/packages/core/exceptions/base-exception-filter-context.ts b/packages/core/exceptions/base-exception-filter-context.ts index 332cbb736d5..942f492b17e 100644 --- a/packages/core/exceptions/base-exception-filter-context.ts +++ b/packages/core/exceptions/base-exception-filter-context.ts @@ -1,7 +1,7 @@ import { FILTER_CATCH_EXCEPTIONS } from '@nestjs/common/constants'; import { Type } from '@nestjs/common/interfaces'; import { ExceptionFilter } from '@nestjs/common/interfaces/exceptions/exception-filter.interface'; -import { isEmpty, isFunction } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isFunction } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ContextCreator } from '../helpers/context-creator'; import { STATIC_CONTEXT } from '../injector/constants'; @@ -20,7 +20,7 @@ export class BaseExceptionFilterContext extends ContextCreator { contextId = STATIC_CONTEXT, inquirerId?: string, ): R { - if (isEmpty(metadata)) { + if (isEmptyArray(metadata)) { return [] as any[] as R; } return iterate(metadata) diff --git a/packages/core/exceptions/exceptions-handler.ts b/packages/core/exceptions/exceptions-handler.ts index 36d1b7f22da..15779667e94 100644 --- a/packages/core/exceptions/exceptions-handler.ts +++ b/packages/core/exceptions/exceptions-handler.ts @@ -2,7 +2,7 @@ import { HttpException } from '@nestjs/common'; import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions/exception-filter-metadata.interface'; import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface'; import { selectExceptionFilterMetadata } from '@nestjs/common/utils/select-exception-filter-metadata.util'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { InvalidExceptionFilterException } from '../errors/exceptions/invalid-exception-filter.exception'; import { BaseExceptionFilter } from './base-exception-filter'; @@ -27,7 +27,7 @@ export class ExceptionsHandler extends BaseExceptionFilter { exception: T, ctx: ArgumentsHost, ): boolean { - if (isEmpty(this.filters)) { + if (isEmptyArray(this.filters)) { return false; } diff --git a/packages/core/exceptions/external-exception-filter-context.ts b/packages/core/exceptions/external-exception-filter-context.ts index d763dfe7349..7bf84aafeaf 100644 --- a/packages/core/exceptions/external-exception-filter-context.ts +++ b/packages/core/exceptions/external-exception-filter-context.ts @@ -1,7 +1,7 @@ import { EXCEPTION_FILTERS_METADATA } from '@nestjs/common/constants'; import { Controller } from '@nestjs/common/interfaces'; import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; import { STATIC_CONTEXT } from '../injector/constants'; @@ -36,7 +36,7 @@ export class ExternalExceptionFilterContext extends BaseExceptionFilterContext { contextId, inquirerId, ); - if (isEmpty(filters)) { + if (isEmptyArray(filters)) { return exceptionHandler; } exceptionHandler.setCustomFilters(filters.reverse()); diff --git a/packages/core/exceptions/external-exceptions-handler.ts b/packages/core/exceptions/external-exceptions-handler.ts index 80cb9dd48a5..bc797b842d1 100644 --- a/packages/core/exceptions/external-exceptions-handler.ts +++ b/packages/core/exceptions/external-exceptions-handler.ts @@ -1,7 +1,7 @@ import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions'; import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface'; import { selectExceptionFilterMetadata } from '@nestjs/common/utils/select-exception-filter-metadata.util'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { InvalidExceptionFilterException } from '../errors/exceptions/invalid-exception-filter.exception'; import { ExternalExceptionFilter } from './external-exception-filter'; @@ -27,7 +27,7 @@ export class ExternalExceptionsHandler extends ExternalExceptionFilter { exception: T, host: ArgumentsHost, ): Promise | null { - if (isEmpty(this.filters)) { + if (isEmptyArray(this.filters)) { return null; } diff --git a/packages/core/guards/guards-consumer.ts b/packages/core/guards/guards-consumer.ts index 7f3c717e2de..ed79c4be6b6 100644 --- a/packages/core/guards/guards-consumer.ts +++ b/packages/core/guards/guards-consumer.ts @@ -1,6 +1,6 @@ import { CanActivate } from '@nestjs/common'; import { ContextType, Controller } from '@nestjs/common/interfaces'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { lastValueFrom, Observable } from 'rxjs'; import { ExecutionContextHost } from '../helpers/execution-context-host'; @@ -12,7 +12,7 @@ export class GuardsConsumer { callback: (...args: unknown[]) => unknown, type?: TContext, ): Promise { - if (!guards || isEmpty(guards)) { + if (!guards || isEmptyArray(guards)) { return true; } const context = this.createContext(args, instance, callback); diff --git a/packages/core/guards/guards-context-creator.ts b/packages/core/guards/guards-context-creator.ts index 536a4cc0ea4..cd13a8cebb2 100644 --- a/packages/core/guards/guards-context-creator.ts +++ b/packages/core/guards/guards-context-creator.ts @@ -1,7 +1,7 @@ import { CanActivate } from '@nestjs/common'; import { GUARDS_METADATA } from '@nestjs/common/constants'; import { Controller, Type } from '@nestjs/common/interfaces'; -import { isEmpty, isFunction } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isFunction } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; import { ContextCreator } from '../helpers/context-creator'; @@ -41,7 +41,7 @@ export class GuardsContextCreator extends ContextCreator { contextId = STATIC_CONTEXT, inquirerId?: string, ): R { - if (isEmpty(metadata)) { + if (isEmptyArray(metadata)) { return [] as unknown[] as R; } return iterate(metadata) diff --git a/packages/core/helpers/external-context-creator.ts b/packages/core/helpers/external-context-creator.ts index bacc125ba60..ceadf687a30 100644 --- a/packages/core/helpers/external-context-creator.ts +++ b/packages/core/helpers/external-context-creator.ts @@ -5,7 +5,7 @@ import { Controller, PipeTransform, } from '@nestjs/common/interfaces'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { isObservable, lastValueFrom } from 'rxjs'; import { ExternalExceptionFilterContext } from '../exceptions/external-exception-filter-context'; import { GuardsConsumer, GuardsContextCreator } from '../guards'; @@ -323,7 +323,7 @@ export class ExternalContextCreator { { metatype, type, data }: { metatype: any; type: any; data: any }, pipes: PipeTransform[], ): Promise { - return isEmpty(pipes) + return isEmptyArray(pipes) ? value : this.pipesConsumer.apply(value, { metatype, type, data }, pipes); } diff --git a/packages/core/interceptors/interceptors-consumer.ts b/packages/core/interceptors/interceptors-consumer.ts index 5032e93235a..4d4b9b6ed2e 100644 --- a/packages/core/interceptors/interceptors-consumer.ts +++ b/packages/core/interceptors/interceptors-consumer.ts @@ -4,7 +4,7 @@ import { ContextType, Controller, } from '@nestjs/common/interfaces'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { AsyncResource } from 'async_hooks'; import { Observable, defer, from as fromPromise } from 'rxjs'; import { mergeAll, switchMap } from 'rxjs/operators'; @@ -19,7 +19,7 @@ export class InterceptorsConsumer { next: () => Promise, type?: TContext, ): Promise { - if (isEmpty(interceptors)) { + if (isEmptyArray(interceptors)) { return next(); } const context = this.createContext(args, instance, callback); diff --git a/packages/core/interceptors/interceptors-context-creator.ts b/packages/core/interceptors/interceptors-context-creator.ts index a7bebc04498..d97bf44ec38 100644 --- a/packages/core/interceptors/interceptors-context-creator.ts +++ b/packages/core/interceptors/interceptors-context-creator.ts @@ -1,6 +1,6 @@ import { INTERCEPTORS_METADATA } from '@nestjs/common/constants'; import { Controller, NestInterceptor, Type } from '@nestjs/common/interfaces'; -import { isEmpty, isFunction } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isFunction } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; import { ContextCreator } from '../helpers/context-creator'; @@ -40,7 +40,7 @@ export class InterceptorsContextCreator extends ContextCreator { contextId = STATIC_CONTEXT, inquirerId?: string, ): R { - if (isEmpty(metadata)) { + if (isEmptyArray(metadata)) { return [] as any[] as R; } return iterate(metadata) diff --git a/packages/core/nest-application-context.ts b/packages/core/nest-application-context.ts index 8d9eb92e04b..842881de3cb 100644 --- a/packages/core/nest-application-context.ts +++ b/packages/core/nest-application-context.ts @@ -13,7 +13,7 @@ import { Type, } from '@nestjs/common/interfaces'; import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { MESSAGES } from './constants'; import { UnknownModuleException } from './errors/exceptions'; @@ -320,7 +320,7 @@ export class NestApplicationContext< * @returns {this} The Nest application context instance */ public enableShutdownHooks(signals: (ShutdownSignal | string)[] = []): this { - if (isEmpty(signals)) { + if (isEmptyArray(signals)) { signals = Object.keys(ShutdownSignal).map( (key: string) => ShutdownSignal[key], ); diff --git a/packages/core/pipes/pipes-context-creator.ts b/packages/core/pipes/pipes-context-creator.ts index 0ea6568cd83..86ad0643309 100644 --- a/packages/core/pipes/pipes-context-creator.ts +++ b/packages/core/pipes/pipes-context-creator.ts @@ -1,6 +1,6 @@ import { PIPES_METADATA } from '@nestjs/common/constants'; import { Controller, PipeTransform, Type } from '@nestjs/common/interfaces'; -import { isEmpty, isFunction } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isFunction } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; import { ContextCreator } from '../helpers/context-creator'; @@ -40,7 +40,7 @@ export class PipesContextCreator extends ContextCreator { contextId = STATIC_CONTEXT, inquirerId?: string, ): R { - if (isEmpty(metadata)) { + if (isEmptyArray(metadata)) { return [] as any[] as R; } return iterate(metadata) diff --git a/packages/core/router/router-exception-filters.ts b/packages/core/router/router-exception-filters.ts index 67769fa22da..95bc224d986 100644 --- a/packages/core/router/router-exception-filters.ts +++ b/packages/core/router/router-exception-filters.ts @@ -1,7 +1,7 @@ import { HttpServer } from '@nestjs/common'; import { EXCEPTION_FILTERS_METADATA } from '@nestjs/common/constants'; import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; import { BaseExceptionFilterContext } from '../exceptions/base-exception-filter-context'; @@ -37,7 +37,7 @@ export class RouterExceptionFilters extends BaseExceptionFilterContext { contextId, inquirerId, ); - if (isEmpty(filters)) { + if (isEmptyArray(filters)) { return exceptionHandler; } exceptionHandler.setCustomFilters(filters.reverse()); diff --git a/packages/core/router/router-execution-context.ts b/packages/core/router/router-execution-context.ts index d1e98383402..0a978e27e0f 100644 --- a/packages/core/router/router-execution-context.ts +++ b/packages/core/router/router-execution-context.ts @@ -18,7 +18,7 @@ import { import { RouteParamMetadata } from '@nestjs/common/decorators'; import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum'; import { ContextType, Controller } from '@nestjs/common/interfaces'; -import { isEmpty, isString } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isString } from '@nestjs/common/utils/shared.utils'; import { IncomingMessage } from 'http'; import { Observable } from 'rxjs'; import { @@ -238,7 +238,7 @@ export class RouterExecutionContext { : this.responseController.getStatusByMethod(requestMethod); const responseHeaders = this.reflectResponseHeaders(callback); - const hasCustomHeaders = !isEmpty(responseHeaders); + const hasCustomHeaders = !isEmptyArray(responseHeaders); const handlerMetadata: HandlerMetadata = { argsLength, fnHandleResponse, @@ -332,7 +332,7 @@ export class RouterExecutionContext { }: { metatype: unknown; type: RouteParamtypes; data: unknown }, pipes: PipeTransform[], ): Promise { - if (!isEmpty(pipes)) { + if (!isEmptyArray(pipes)) { return this.pipesConsumer.apply( value, { metatype, type, data } as any, diff --git a/packages/core/services/reflector.service.ts b/packages/core/services/reflector.service.ts index be666f9a58c..bea27b9e0c4 100644 --- a/packages/core/services/reflector.service.ts +++ b/packages/core/services/reflector.service.ts @@ -1,5 +1,5 @@ import { CustomDecorator, SetMetadata, Type } from '@nestjs/common'; -import { isEmpty, isObject } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray, isObject } from '@nestjs/common/utils/shared.utils'; import { uid } from 'uid'; /** @@ -200,7 +200,7 @@ export class Reflector { targets, ).filter(item => item !== undefined); - if (isEmpty(metadataCollection)) { + if (isEmptyArray(metadataCollection)) { return metadataCollection as TResult; } if (metadataCollection.length === 1) { diff --git a/packages/microservices/context/exception-filters-context.ts b/packages/microservices/context/exception-filters-context.ts index 14d42da915e..2cba605bdba 100644 --- a/packages/microservices/context/exception-filters-context.ts +++ b/packages/microservices/context/exception-filters-context.ts @@ -1,6 +1,6 @@ import { EXCEPTION_FILTERS_METADATA } from '@nestjs/common/constants'; import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { ApplicationConfig } from '@nestjs/core/application-config'; import { BaseExceptionFilterContext } from '@nestjs/core/exceptions/base-exception-filter-context'; import { STATIC_CONTEXT } from '@nestjs/core/injector/constants'; @@ -38,7 +38,7 @@ export class ExceptionFiltersContext extends BaseExceptionFilterContext { contextId, inquirerId, ); - if (isEmpty(filters)) { + if (isEmptyArray(filters)) { return exceptionHandler; } exceptionHandler.setCustomFilters(filters.reverse()); diff --git a/packages/microservices/context/rpc-context-creator.ts b/packages/microservices/context/rpc-context-creator.ts index a7ee74e025a..02e1d7d5e4d 100644 --- a/packages/microservices/context/rpc-context-creator.ts +++ b/packages/microservices/context/rpc-context-creator.ts @@ -7,7 +7,7 @@ import { Controller, PipeTransform, } from '@nestjs/common/interfaces'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { FORBIDDEN_MESSAGE } from '@nestjs/core/guards/constants'; import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer'; import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator'; @@ -276,7 +276,7 @@ export class RpcContextCreator { { metatype, type, data }: { metatype: any; type: any; data: any }, pipes: PipeTransform[], ): Promise { - return isEmpty(pipes) + return isEmptyArray(pipes) ? value : this.pipesConsumer.apply(value, { metatype, type, data }, pipes); } diff --git a/packages/microservices/exceptions/rpc-exceptions-handler.ts b/packages/microservices/exceptions/rpc-exceptions-handler.ts index f551bd953fa..af1639d5415 100644 --- a/packages/microservices/exceptions/rpc-exceptions-handler.ts +++ b/packages/microservices/exceptions/rpc-exceptions-handler.ts @@ -1,7 +1,7 @@ import { RpcExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions'; import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface'; import { selectExceptionFilterMetadata } from '@nestjs/common/utils/select-exception-filter-metadata.util'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { InvalidExceptionFilterException } from '@nestjs/core/errors/exceptions/invalid-exception-filter.exception'; import { Observable } from 'rxjs'; import { BaseRpcExceptionFilter } from './base-rpc-exception-filter'; @@ -35,7 +35,7 @@ export class RpcExceptionsHandler extends BaseRpcExceptionFilter { exception: T, host: ArgumentsHost, ): Observable | null { - if (isEmpty(this.filters)) { + if (isEmptyArray(this.filters)) { return null; } diff --git a/packages/websockets/context/exception-filters-context.ts b/packages/websockets/context/exception-filters-context.ts index 7d630ad1cc6..25b9f23ebca 100644 --- a/packages/websockets/context/exception-filters-context.ts +++ b/packages/websockets/context/exception-filters-context.ts @@ -1,5 +1,5 @@ import { EXCEPTION_FILTERS_METADATA } from '@nestjs/common/constants'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { BaseExceptionFilterContext } from '@nestjs/core/exceptions/base-exception-filter-context'; import { NestContainer } from '@nestjs/core/injector/container'; import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler'; @@ -25,7 +25,7 @@ export class ExceptionFiltersContext extends BaseExceptionFilterContext { callback, EXCEPTION_FILTERS_METADATA, ); - if (isEmpty(filters)) { + if (isEmptyArray(filters)) { return exceptionHandler; } exceptionHandler.setCustomFilters(filters.reverse()); diff --git a/packages/websockets/context/ws-context-creator.ts b/packages/websockets/context/ws-context-creator.ts index c699612b6f7..4b791a36975 100644 --- a/packages/websockets/context/ws-context-creator.ts +++ b/packages/websockets/context/ws-context-creator.ts @@ -7,7 +7,7 @@ import { Controller, PipeTransform, } from '@nestjs/common/interfaces'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { FORBIDDEN_MESSAGE } from '@nestjs/core/guards/constants'; import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer'; import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator'; @@ -271,7 +271,7 @@ export class WsContextCreator { { metatype, type, data }: { metatype: any; type: any; data: any }, pipes: PipeTransform[], ): Promise { - return isEmpty(pipes) + return isEmptyArray(pipes) ? value : this.pipesConsumer.apply(value, { metatype, type, data }, pipes); } diff --git a/packages/websockets/exceptions/ws-exceptions-handler.ts b/packages/websockets/exceptions/ws-exceptions-handler.ts index 3efbbbfad58..2f23c849caf 100644 --- a/packages/websockets/exceptions/ws-exceptions-handler.ts +++ b/packages/websockets/exceptions/ws-exceptions-handler.ts @@ -1,7 +1,7 @@ import { ArgumentsHost } from '@nestjs/common'; import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions/exception-filter-metadata.interface'; import { selectExceptionFilterMetadata } from '@nestjs/common/utils/select-exception-filter-metadata.util'; -import { isEmpty } from '@nestjs/common/utils/shared.utils'; +import { isEmptyArray } from '@nestjs/common/utils/shared.utils'; import { InvalidExceptionFilterException } from '@nestjs/core/errors/exceptions/invalid-exception-filter.exception'; import { WsException } from '../errors/ws-exception'; import { BaseWsExceptionFilter } from './base-ws-exception-filter'; @@ -31,7 +31,7 @@ export class WsExceptionsHandler extends BaseWsExceptionFilter { exception: T, args: ArgumentsHost, ): boolean { - if (isEmpty(this.filters)) return false; + if (isEmptyArray(this.filters)) return false; const filter = selectExceptionFilterMetadata(this.filters, exception); filter && filter.func(exception, args);