# All files / common/decorators/http request-mapping.decorator.ts

100% Statements 21/21
80% Branches 4/5
100% Functions 4/4
100% Lines 20/20

    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
    1x
    1x

    1x

    1x

    27x
    27x

    27x
    27x
    27x
    27x

    8x

    15x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
     import { RequestMappingMetadata } from '../../interfaces/request-mapping-metadata.interface';
    import { RequestMethod } from '../../enums/request-method.enum';
    import { PATH_METADATA, METHOD_METADATA } from '../../constants';

    const defaultMetadata = {
      [PATH_METADATA]: '/',
      [METHOD_METADATA]: RequestMethod.GET,
    };

    export const RequestMapping = (
      metadata: RequestMappingMetadata = defaultMetadata,
    ): MethodDecorator => {
      const path = metadata[PATH_METADATA] || '/';
      const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET;

      return (target, key, descriptor: PropertyDescriptor) => {
        Reflect.defineMetadata(PATH_METADATA, path, descriptor.value);
        Reflect.defineMetadata(METHOD_METADATA, requestMethod, descriptor.value);
        return descriptor;
      };
    };

    const createMappingDecorator = (method: RequestMethod) => (
      path?: string,
    ): MethodDecorator => {
      return RequestMapping({
        [PATH_METADATA]: path,
        [METHOD_METADATA]: method,
      });
    };

    /**
     * Routes HTTP POST requests to the specified path.
     */
    export const Post = createMappingDecorator(RequestMethod.POST);

    /**
     * Routes HTTP GET requests to the specified path.
     */
    export const Get = createMappingDecorator(RequestMethod.GET);

    /**
     * Routes HTTP DELETE requests to the specified path.
     */
    export const Delete = createMappingDecorator(RequestMethod.DELETE);

    /**
     * Routes HTTP PUT requests to the specified path.
     */
    export const Put = createMappingDecorator(RequestMethod.PUT);

    /**
     * Routes HTTP PATCH requests to the specified path.
     */
    export const Patch = createMappingDecorator(RequestMethod.PATCH);

    /**
     * Routes HTTP OPTIONS requests to the specified path.
     */
    export const Options = createMappingDecorator(RequestMethod.OPTIONS);

    /**
     * Routes HTTP HEAD requests to the specified path.
     */
    export const Head = createMappingDecorator(RequestMethod.HEAD);

    /**
     * Routes all HTTP requests to the specified path.
     */
    export const All = createMappingDecorator(RequestMethod.ALL);