All files / common/decorators/http create-route-param-metadata.decorator.ts

95.45% Statements 21/22
100% Branches 13/13
85.71% Functions 6/7
100% Lines 21/21
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911x 1x 1x             1x     1x             4x                               1x           6x 6x     4x   4x   4x 4x     4x 4x 4x   4x                         4x                 1x           6x     6x    
import * as deprecate from 'deprecate';
import * as uuid from 'uuid/v4';
import {
  CUSTOM_ROUTE_AGRS_METADATA,
  ROUTE_ARGS_METADATA,
} from '../../constants';
import { PipeTransform } from '../../index';
import { Type } from '../../interfaces';
import { CustomParamFactory } from '../../interfaces/features/custom-route-param-factory.interface';
import { isFunction, isNil } from '../../utils/shared.utils';
import { ParamData, RouteParamsMetadata } from './route-params.decorator';
 
const assignCustomMetadata = (
  args: RouteParamsMetadata,
  paramtype: number | string,
  index: number,
  factory: CustomParamFactory,
  data?: ParamData,
  ...pipes: (Type<PipeTransform> | PipeTransform)[]
) => ({
  ...args,
  [`${paramtype}${CUSTOM_ROUTE_AGRS_METADATA}:${index}`]: {
    index,
    factory,
    data,
    pipes,
  },
});
 
export type ParamDecoratorEnhancer = ParameterDecorator;
 
/**
 * Defines HTTP route param decorator
 * @param factory
 */
export function createParamDecorator(
  factory: CustomParamFactory,
  enhancers: ParamDecoratorEnhancer[] = [],
): (
  ...dataOrPipes: (Type<PipeTransform> | PipeTransform | any)[]
) => ParameterDecorator {
  const paramtype = uuid();
  return (
    data?,
    ...pipes: (Type<PipeTransform> | PipeTransform)[]
  ): ParameterDecorator => (target, key, index) => {
    const args =
      Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {};
 
    const isPipe = pipe =>
      pipe &&
      ((isFunction(pipe) && pipe.prototype) || isFunction(pipe.transform));
 
    const hasParamData = isNil(data) || !isPipe(data);
    const paramData = hasParamData ? data : undefined;
    const paramPipes = hasParamData ? pipes : [data, ...pipes];
 
    Reflect.defineMetadata(
      ROUTE_ARGS_METADATA,
      assignCustomMetadata(
        args,
        paramtype,
        index,
        factory,
        paramData,
        ...(paramPipes as PipeTransform[]),
      ),
      target.constructor,
      key,
    );
    enhancers.forEach(fn => fn(target, key, index));
  };
}
 
/**
 * Defines HTTP route param decorator
 * @deprecated
 * @param factory
 */
export function createRouteParamDecorator(
  factory: CustomParamFactory,
): (
  data?: any,
  ...pipes: (Type<PipeTransform> | PipeTransform)[]
) => ParameterDecorator {
  deprecate(
    'The "createRouteParamDecorator" function is deprecated and will be removed within next major release. Use "createParamDecorator" instead.',
  );
  return createParamDecorator(factory);
}