All files / router params.ts

100% Statements 29/29
62.5% Branches 5/8
100% Functions 8/8
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 682x               2x 2x 2x 1x     2x 2x                   2x 1x 1x 1x     1x 1x                   2x 2x 2x 2x     2x 2x                 2x 2x 2x 2x     2x 2x    
import 'reflect-metadata';
import { Validator } from '../validation/validators';
 
/**
 * @decorator Body
 * @description A parameter decorator that marks a parameter as the request body.
 * @returns {ParameterDecorator}
 */
export const Body = (): ParameterDecorator => {
  return (target, propertyKey, parameterIndex) => {
    if (!Reflect.hasMetadata('body', target.constructor, propertyKey as string)) {
      Reflect.defineMetadata('body', [], target.constructor, propertyKey as string);
    }
 
    const bodyParams = Reflect.getMetadata('body', target.constructor, propertyKey as string);
    bodyParams.push(parameterIndex);
  };
};
 
/**
 * @decorator Valid
 * @description A parameter decorator that marks a parameter to be validated.
 * @param {new () => Validator} validator - The validator to use.
 * @returns {ParameterDecorator}
 */
export const Valid = (validator: new () => Validator): ParameterDecorator => {
  return (target, propertyKey, parameterIndex) => {
    Eif (!Reflect.hasMetadata('validators', target.constructor, propertyKey as string)) {
      Reflect.defineMetadata('validators', [], target.constructor, propertyKey as string);
    }
 
    const validators = Reflect.getMetadata('validators', target.constructor, propertyKey as string);
    validators.push({ index: parameterIndex, validator });
  };
};
 
/**
 * @decorator PathParam
 * @description A parameter decorator that marks a parameter as a path parameter.
 * @param {string} name - The name of the path parameter.
 * @returns {ParameterDecorator}
 */
export const PathParam = (name: string): ParameterDecorator => {
  return (target, propertyKey, parameterIndex) => {
    Eif (!Reflect.hasMetadata('pathParams', target.constructor, propertyKey as string)) {
      Reflect.defineMetadata('pathParams', [], target.constructor, propertyKey as string);
    }
 
    const pathParams = Reflect.getMetadata('pathParams', target.constructor, propertyKey as string);
    pathParams.push({ index: parameterIndex, name });
  };
};
 
/**
 * @decorator QueryParams
 * @description A parameter decorator that marks a parameter as the query parameters.
 * @returns {ParameterDecorator}
 */
export const QueryParams = (): ParameterDecorator => {
  return (target, propertyKey, parameterIndex) => {
    Eif (!Reflect.hasMetadata('queryParams', target.constructor, propertyKey as string)) {
      Reflect.defineMetadata('queryParams', [], target.constructor, propertyKey as string);
    }
 
    const queryParams = Reflect.getMetadata('queryParams', target.constructor, propertyKey as string);
    queryParams.push(parameterIndex);
  };
};