All files decorators.ts

84.11% Statements 254/302
70.71% Branches 140/198
91.23% Functions 52/57
82.24% Lines 213/259

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086    1x 1x 1x 1x 1x                                                               1x 148x 62x 62x 19x 43x 43x                                                                               6x 9x 5x 5x 3x   5x 3x 2x 2x                                                                 1x 10x 4x 4x 1x 3x 3x                                                     5x 5x 3x 3x 2x 1x 1x                                                     2x 4x 2x 2x 1x 1x 1x                                                   18x 6x 6x 6x 6x                                                     9x 3x 3x 3x 1x 2x 2x                                                 3x 1x 1x 1x   1x 1x                                                 3x 1x 1x 1x   1x 1x                                           6x 2x 2x 2x   2x 2x                                           3x 1x 1x 1x   1x 1x                                                       1x   39x                                                 1x   11x                                                   1x   5x                                                   1x   1x                                                 1x                                                     1x                                                       1x                   1x 2x 2x 2x 2x                                                           1x 18x 6x 6x 6x   6x 6x                                                         1x 3x 1x 1x 1x   1x 1x                                                         1x                                                                           1x 27x 9x 9x 9x   9x 9x                                                   1x 6x 2x 2x 2x 1x 1x 1x                                                   1x 3x 1x 1x 1x   1x 1x                                                     1x 3x 1x 1x 1x   1x 1x                                                       1x 3x 1x 1x 1x   1x 1x                                                   1x 1x 1x             2x 2x               1x 1x 1x               1x 1x               1x 1x 1x               19x 19x 19x                 43x 43x 43x               3x 3x 3x                 2x 2x 2x               1x 1x 1x 1x   1x                 3x 3x 3x 2x   3x                 27x 27x 27x   27x 26x     27x               8x 8x 8x               56x 56x 56x         56x 56x               56x 56x 56x 19x       56x 47x 1x 46x 1x 45x   45x 1x 44x 1x     1x 43x 20x     20x     20x        
'use strict';
 
import * as _ from 'lodash';
import 'reflect-metadata';
import * as metadata from './metadata';
import { InternalServer } from './server-container';
import { HttpMethod, ServicePreProcessor } from './server-types';
 
/**
 * A decorator to tell the [[Server]] that a class or a method
 * should be bound to a given path.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ PUT
 *   @ Path(':id')
 *   savePerson(person:Person) {
 *      // ...
 *   }
 *
 *   @ GET
 *   @ Path(':id')
 *   getPerson():Person {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create services that listen for requests like:
 *
 * ```
 * PUT http://mydomain/people/123 or
 * GET http://mydomain/people/123
 * ```
 */
export function Path(path: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        if (args.length === 1) {
            return PathTypeDecorator.apply(this, [args[0], path]);
        } else Eif (args.length === 3 && typeof args[2] === 'object') {
            return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
        }
 
        throw new Error('Invalid @Path Decorator declaration.');
    };
}
 
/**
 * A decorator to tell the [[Server]] that a class or a method
 * should include a determined role
 * or all authorized users (token) using passport
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * @ Security()
 * class PeopleService {
 *   @ PUT
 *   @ Path(':id', true)
 *   @ Security(['ROLE_ADMIN'])
 *   savePerson(person:Person) {
 *      // ...
 *   }
 *
 *   @ GET
 *   @ Path(':id', true)
 *   getPerson():Person {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create services that listen for requests like:
 *
 * ```
 * PUT http://mydomain/people/123 (Only for ADMIN roles) or
 * GET http://mydomain/people/123 (For all authorized users)
 * ```
 */
export function Security(roles: string | Array<string> = ['*']) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        if (typeof roles !== 'object') {
            roles = [roles];
        }
        if (args.length === 1) {
            return SecurityTypeDecorator.apply(this, [args[0], roles]);
        } else Eif (args.length === 3 && typeof args[2] === 'object') {
            return SecurityMethodDecorator.apply(this, [args[0], args[1], args[2], roles]);
        }
 
        throw new Error('Invalid @Security Decorator declaration.');
    };
}
 
/**
 * A decorator to tell the [[Server]] that a class or a method
 * should include a pre-processor in its request pipelines.
 *
 * For example:
 * ```
 * function validator(req: express.Request): express.Request {
 *   if (!req.body.userId) {
 *      throw new Errors.BadRequestError("userId not present");
 *   } 
 * }
 * ```
 * And:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ PUT
 *   @ Path(':id')
 *   @ Preprocessor(validator)
 *   savePerson(person:Person) {
 *      // ...
 *   }
 * }
 * ```
 */
export function Preprocessor(preprocessor: ServicePreProcessor) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        if (args.length === 1) {
            return PreprocessorTypeDecorator.apply(this, [args[0], preprocessor]);
        } else Eif (args.length === 3 && typeof args[2] === 'object') {
            return PreprocessorMethodDecorator.apply(this, [args[0], args[1], args[2], preprocessor]);
        }
 
        throw new Error('Invalid @Preprocessor Decorator declaration.');
    };
}
 
/**
 * A decorator to tell the [[Server]] that a class or a method
 * should only accept requests from clients that accepts one of
 * the supported languages.
 *
 * For example:
 *
 * ```
 * @ Path('accept')
 * @ AcceptLanguage('en', 'pt-BR')
 * class TestAcceptService {
 *      // ...
 * }
 * ```
 *
 * Will reject requests that only accepts languages that are not
 * English or Brazilian portuguese
 *
 * If the language requested is not supported, a status code 406 returned
 */
export function AcceptLanguage(...languages: Array<string>) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        if (args.length === 1) {
            return AcceptLanguageTypeDecorator.apply(this, [args[0], languages]);
        } else Eif (args.length === 3 && typeof args[2] === 'object') {
            return AcceptLanguageMethodDecorator.apply(this, [args[0], args[1], args[2], languages]);
        }
 
        throw new Error('Invalid @AcceptLanguage Decorator declaration.');
    };
}
 
/**
 * A decorator to tell the [[Server]] that a class or a method
 * should only accept requests from clients that accepts one of
 * the supported mime types.
 *
 * For example:
 *
 * ```
 * @ Path('accept')
 * @ Accept('application/json')
 * class TestAcceptService {
 *      // ...
 * }
 * ```
 *
 * Will reject requests that only accepts mime types that are not
 * 'application/json'
 *
 * If the mime type requested is not supported, a status code 406 returned
 */
export function Accept(...accepts: Array<string>) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        if (args.length === 1) {
            return AcceptTypeDecorator.apply(this, [args[0], accepts]);
        } else Eif (args.length === 3 && typeof args[2] === 'object') {
            return AcceptMethodDecorator.apply(this, [args[0], args[1], args[2], accepts]);
        }
 
        throw new Error('Invalid @Accept Decorator declaration.');
    };
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * [[ServiceContext]] object associated to the current request.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ Context
 *   context: ServiceContext;
 *   // ...
 * }
 * ```
 *
 * The field context on the above class will point to the current
 * [[ServiceContext]] instance.
 */
export function Context(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context, null]);
    Eif (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else if (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @Context Decorator declaration.');
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * the current request.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ ContextRequest
 *   request: express.Request;
 *   // ...
 * }
 * ```
 *
 * The field request on the above class will point to the current
 * request.
 */
export function ContextRequest(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context_request, null]);
    if (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else Eif (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @ContextRequest Decorator declaration.');
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * the current response object.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ ContextResponse
 *   response: express.Response;
 *   // ...
 * }
 * ```
 *
 * The field response on the above class will point to the current
 * response object.
 */
export function ContextResponse(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context_response, null]);
    Iif (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else Eif (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @ContextResponse Decorator declaration.');
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * the next function.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ ContextNext
 *   next: express.NextFunction
 *       // ...
 * }
 * ```
 *
 * The next function can be used to delegate to the next registered
 * middleware the current request processing.
 */
export function ContextNext(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context_next, null]);
    Iif (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else Eif (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @ContextNext Decorator declaration.');
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * the current context language.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ ContextLanguage
 *   language: string
 *       // ...
 * }
 * ```
 */
export function ContextLanguage(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context_accept_language, null]);
    Iif (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else Eif (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @ContextLanguage Decorator declaration.');
}
 
/**
 * A decorator to be used on class properties or on service method arguments
 * to inform that the decorated property or argument should be bound to the
 * the preferred media type for the current request.
 *
 * For example:
 *
 * ```
 * @ Path('context')
 * class TestService {
 *   @ ContextAccept
 *   media: string
 *       // ...
 * }
 * ```
 */
export function ContextAccept(...args: Array<any>) {
    args = _.without(args, undefined);
    const newArgs = args.concat([metadata.ParamType.context_accept, null]);
    Iif (args.length < 3 || typeof args[2] === 'undefined') {
        return processDecoratedProperty.apply(this, newArgs);
    } else Eif (args.length === 3 && typeof args[2] === 'number') {
        return processDecoratedParameter.apply(this, newArgs);
    }
 
    throw new Error('Invalid @ContextAccept Decorator declaration.');
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP GET requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople() {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * GET http://mydomain/people
 * ```
 */
export function GET(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.GET);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP POST requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ POST
 *   addPerson() {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * POST http://mydomain/people
 * ```
 */
export function POST(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.POST);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP PUT requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ PUT
 *   @ Path(':id')
 *   savePerson(person: Person) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * PUT http://mydomain/people/123
 * ```
 */
export function PUT(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.PUT);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP DELETE requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ DELETE
 *   @ Path(':id')
 *   removePerson(@ PathParam('id')id: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * PUT http://mydomain/people/123
 * ```
 */
export function DELETE(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.DELETE);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP HEAD requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ HEAD
 *   headPerson() {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * HEAD http://mydomain/people/123
 * ```
 */
export function HEAD(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.HEAD);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP OPTIONS requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ OPTIONS
 *   optionsPerson() {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * OPTIONS http://mydomain/people/123
 * ```
 */
export function OPTIONS(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.OPTIONS);
}
 
/**
 * A decorator to tell the [[Server]] that a method
 * should be called to process HTTP PATCH requests.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ PATCH
 *   @ Path(':id')
 *   savePerson(person: Person) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * PATCH http://mydomain/people/123
 * ```
 */
export function PATCH(target: any, propertyKey: string,
    descriptor: PropertyDescriptor) {
    processHttpVerb(target, propertyKey, HttpMethod.PATCH);
}
 
/**
 * A decorator to inform options to pe passed to bodyParser.
 * You can inform any property accepted by
 * [[bodyParser]](https://www.npmjs.com/package/body-parser)
 */
export function BodyOptions(options: any) {
    return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
        Eif (serviceMethod) { // does not intercept constructor
            serviceMethod.bodyParserOptions = options;
        }
    };
}
 
/**
 * Creates a mapping between a fragment of the requested path and
 * a method argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   @ Path(':id')
 *   getPerson(@ PathParam('id') id: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * GET http://mydomain/people/123
 * ```
 *
 * And pass 123 as the id argument on getPerson method's call.
 */
export function PathParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.path, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @PathParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a file on a multipart request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ POST
 *   @ Path('id')
 *   addAvatar(@ PathParam('id') id: string,
 *             @ FileParam('avatar') file: Express.Multer.File) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * file with name 'avatar' on the requested form to the file
 * argument on addAvatar method's call.
 */
export function FileParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.file, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @FileParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a list of files on a multipart request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ POST
 *   @ Path('id')
 *   addAvatar(@ PathParam('id') id: string,
 *             @ FilesParam('avatar[]') files: Array<Express.Multer.File>) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * files with name 'avatar' on the request form to the file
 * argument on addAvatar method's call.
 */
export function FilesParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.files, name]);
        if (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else if (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @FilesParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a query parameter on request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople(@ QueryParam('name') name: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests like:
 *
 * ```
 * GET http://mydomain/people?name=joe
 * ```
 *
 * And pass 'joe' as the name argument on getPerson method's call.
 */
export function QueryParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.query, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @QueryParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a header on request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople(@ HeaderParam('header') header: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * header called 'header' to the header argument on getPerson method's call.
 */
export function HeaderParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.header, name]);
        if (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @HeaderParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a cookie on request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople(@ CookieParam('cookie') cookie: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * cookie called 'cookie' to the cookie argument on getPerson method's call.
 */
export function CookieParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.cookie, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @CookieParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a form parameter on request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople(@ FormParam('name') name: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * request paramenter called 'name' to the name argument on getPerson
 * method's call.
 */
export function FormParam(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.form, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @FormParam Decorator declaration.');
    };
}
 
/**
 * Creates a mapping between a parameter on request and a method
 * argument.
 *
 * For example:
 *
 * ```
 * @ Path('people')
 * class PeopleService {
 *   @ GET
 *   getPeople(@ Param('name') name: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * Will create a service that listen for requests and bind the
 * request paramenter called 'name' to the name argument on getPerson
 * method's call. It will work to query parameters or form parameters
 * received in the current request.
 */
export function Param(name: string) {
    return function (...args: Array<any>) {
        args = _.without(args, undefined);
        const newArgs = args.concat([metadata.ParamType.param, name]);
        Iif (args.length < 3 || typeof args[2] === 'undefined') {
            return processDecoratedProperty.apply(this, newArgs);
        } else Eif (args.length === 3 && typeof args[2] === 'number') {
            return processDecoratedParameter.apply(this, newArgs);
        }
 
        throw new Error('Invalid @Param Decorator declaration.');
    };
}
 
/**
 * Mark the annotated service class as an abstract service. Abstract services has none of its
 * methods exposed as rest enpoints, even if the class is in the services list to be exposed.
 *
 * For example:
 *
 * ```
 * @ Abstract
 * abstract class PeopleService {
 *   @ GET
 *   getPeople(@ Param('name') name: string) {
 *      // ...
 *   }
 * }
 * ```
 *
 * No endpoint will be registered for PeopleService. It is useful if you only plain that subclasses of
 * PeopleService exposes the getPeople method.
 */
export function Abstract(target: Function) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    classData.isAbstract = true;
}
 
/**
 * Decorator processor for [[AcceptLanguage]] decorator on classes
 */
function AcceptLanguageTypeDecorator(target: Function, languages: Array<string>) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    classData.languages = _.union(classData.languages, languages);
}
 
/**
 * Decorator processor for [[AcceptLanguage]] decorator on methods
 */
function AcceptLanguageMethodDecorator(target: any, propertyKey: string,
    descriptor: PropertyDescriptor, languages: Array<string>) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        serviceMethod.languages = languages;
    }
}
 
/**
 * Decorator processor for [[Accept]] decorator on classes
 */
function AcceptTypeDecorator(target: Function, accepts: Array<string>) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    classData.accepts = _.union(classData.accepts, accepts);
}
 
/**
 * Decorator processor for [[Accept]] decorator on methods
 */
function AcceptMethodDecorator(target: any, propertyKey: string,
    descriptor: PropertyDescriptor, accepts: Array<string>) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        serviceMethod.accepts = accepts;
    }
}
 
/**
 * Decorator processor for [[Path]] decorator on classes
 */
function PathTypeDecorator(target: Function, path: string) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    Eif (classData) {
        classData.path = path;
    }
}
 
/**
 * Decorator processor for [[Path]] decorator on methods
 */
function PathMethodDecorator(target: any, propertyKey: string,
    descriptor: PropertyDescriptor, path: string) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        serviceMethod.path = path;
    }
}
 
/**
 * Decorator processor for [[Security]] decorator on classes
 */
function SecurityTypeDecorator(target: Function, roles: Array<string>) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    Eif (classData) {
        classData.roles = roles;
    }
}
 
/**
 * Decorator processor for [[Security]] decorator on methods
 */
function SecurityMethodDecorator(target: any, propertyKey: string,
    descriptor: PropertyDescriptor, roles: Array<string>) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        serviceMethod.roles = roles;
    }
}
 
/**
 * Decorator processor for [[Preprocessor]] decorator on classes
 */
function PreprocessorTypeDecorator(target: Function, preprocessor: metadata.PreprocessorFunction) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
    Eif (classData) {
        Eif (!classData.processors) {
            classData.processors = [];
        }
        classData.processors.unshift(preprocessor);
    }
}
 
/**
 * Decorator processor for [[Preprocessor]] decorator on methods
 */
function PreprocessorMethodDecorator(target: any, propertyKey: string,
    descriptor: PropertyDescriptor, preprocessor: metadata.PreprocessorFunction) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) {
        if (!serviceMethod.processors) {
            serviceMethod.processors = [];
        }
        serviceMethod.processors.unshift(preprocessor);
    }
}
 
/**
 * Decorator processor for parameter annotations on methods
 */
function processDecoratedParameter(target: Object, propertyKey: string, parameterIndex: number,
    paramType: metadata.ParamType, name: string) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        const paramTypes = Reflect.getOwnMetadata('design:paramtypes', target, propertyKey);
 
        while (paramTypes && serviceMethod.parameters.length < paramTypes.length) {
            serviceMethod.parameters.push(new metadata.MethodParam(null,
                paramTypes[serviceMethod.parameters.length], metadata.ParamType.body));
        }
        serviceMethod.parameters[parameterIndex] = new metadata.MethodParam(name, paramTypes[parameterIndex], paramType);
    }
}
 
/**
 * Decorator processor for annotations on properties
 */
function processDecoratedProperty(target: Function, key: string, paramType: metadata.ParamType, paramName: string) {
    const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target.constructor);
    const propertyType = Reflect.getMetadata('design:type', target, key);
    classData.addProperty(key, paramType, paramName, propertyType);
}
 
/**
 * Decorator processor for HTTP verb annotations on methods
 */
function processHttpVerb(target: any, propertyKey: string,
    httpMethod: HttpMethod) {
    const serviceMethod: metadata.ServiceMethod = InternalServer.registerServiceMethod(target.constructor, propertyKey);
    Eif (serviceMethod) { // does not intercept constructor
        Iif (serviceMethod.httpMethod && serviceMethod.httpMethod !== httpMethod) {
            throw new Error('Method is already annotated with @' +
                HttpMethod[serviceMethod.httpMethod] +
                '. You can only map a method to one HTTP verb.');
        }
        serviceMethod.httpMethod = httpMethod;
        processServiceMethod(target, propertyKey, serviceMethod);
    }
}
 
/**
 * Extract metadata for rest methods
 */
function processServiceMethod(target: any, propertyKey: string, serviceMethod: metadata.ServiceMethod) {
    serviceMethod.name = propertyKey;
    const paramTypes = Reflect.getOwnMetadata('design:paramtypes', target, propertyKey);
    while (paramTypes && paramTypes.length > serviceMethod.parameters.length) {
        serviceMethod.parameters.push(new metadata.MethodParam(null,
            paramTypes[serviceMethod.parameters.length], metadata.ParamType.body));
    }
 
    serviceMethod.parameters.forEach(param => {
        if (param.paramType === metadata.ParamType.cookie) {
            serviceMethod.mustParseCookies = true;
        } else if (param.paramType === metadata.ParamType.file) {
            serviceMethod.files.push(new metadata.FileParam(param.name, true));
        } else Iif (param.paramType === metadata.ParamType.files) {
            serviceMethod.files.push(new metadata.FileParam(param.name, false));
        } else if (param.paramType === metadata.ParamType.param) {
            serviceMethod.acceptMultiTypedParam = true;
        } else if (param.paramType === metadata.ParamType.form) {
            Iif (serviceMethod.mustParseBody) {
                throw Error('Can not use form parameters with a body parameter on the same method.');
            }
            serviceMethod.mustParseForms = true;
        } else if (param.paramType === metadata.ParamType.body) {
            Iif (serviceMethod.mustParseForms) {
                throw Error('Can not use form parameters with a body parameter on the same method.');
            }
            Iif (serviceMethod.mustParseBody) {
                throw Error('Can not use more than one body parameter on the same method.');
            }
            serviceMethod.mustParseBody = true;
        }
    });
}