All files / common/decorators/core use-pipes.decorator.ts

100% Statements 14/14
100% Branches 5/5
100% Functions 3/3
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  1x 1x 1x 1x                       1x 5x 5x 2x 5x 3x 3x   2x 2x 2x      
import { PipeTransform } from '../../interfaces/index';
import { PIPES_METADATA } from '../../constants';
import { extendArrayMetadata } from '../../utils/extend-metadata.util';
import { validateEach } from '../../utils/validate-each.util';
import { isFunction } from '../../utils/shared.utils';
 
/**
 * Binds pipes to the particular context.
 * When the `@UsePipes()` is used on the controller level:
 * - Pipe will be register to each handler (every method)
 *
 * When the `@UsePipes()` is used on the handle level:
 * - Pipe will be registered only to specified method
 *
 * @param  {PipeTransform[]} ...pipes
 */
export function UsePipes(...pipes: (PipeTransform | Function)[]) {
  return (target: any, key?, descriptor?) => {
    const isPipeValid = pipe =>
      pipe && (isFunction(pipe) || isFunction(pipe.transform));
    if (descriptor) {
      extendArrayMetadata(PIPES_METADATA, pipes, descriptor.value);
      return descriptor;
    }
    validateEach(target, pipes, isPipeValid, '@UsePipes', 'pipe');
    extendArrayMetadata(PIPES_METADATA, pipes, target);
    return target;
  };
}