# All files / core/middleware utils.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 7/7
100% Lines 14/14

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    1x
    1x

    1x
    10x

    1x
    6x
    2x

    4x

    2x

    1x
    8x

    1x
    5x
    5x

     import { Type } from '@nestjs/common/interfaces';
    import { isFunction } from '@nestjs/common/utils/shared.utils';
    import * as uuid from 'uuid/v4';

    export const filterMiddleware = middleware => {
      return []
        .concat(middleware)
        .filter(isFunction)
        .map(mapToClass);
    };

    export const mapToClass = middleware => {
      if (isClass(middleware)) {
        return middleware;
      }
      return assignToken(
        class {
          resolve = (...args) => (...params) => middleware(...params);
        },
      );
    };

    export function isClass(middleware: any) {
      return middleware.toString().substring(0, 5) === 'class';
    }

    export function assignToken(metatype): Type<any> {
      Object.defineProperty(metatype, 'name', { value: uuid() });
      return metatype;
    }