1 | 'use strict';
|
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 | module.exports = function (methods) {
|
30 | var Route = this.router().constructor.prototype;
|
31 |
|
32 | if (null == methods) {
|
33 | methods = ['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'ANY'];
|
34 | }
|
35 |
|
36 | if (!Array.isArray(methods)) {
|
37 | throw new Error('Array expected');
|
38 | }
|
39 |
|
40 | Route.via = function () {
|
41 | var argv = [].slice.call(arguments);
|
42 |
|
43 | if (!argv.length) {
|
44 | throw new Error('Method expected');
|
45 | }
|
46 |
|
47 | if (Array.isArray(argv[0])) {
|
48 | if (argv.length > 1) {
|
49 | throw new Error('Unexpected arguments');
|
50 | }
|
51 |
|
52 | argv = argv[0];
|
53 |
|
54 | if (!argv.length) {
|
55 | argv = undefined;
|
56 | }
|
57 | }
|
58 |
|
59 | this.set('methods', argv);
|
60 |
|
61 | return this;
|
62 | };
|
63 |
|
64 | methods.forEach(function (method) {
|
65 | method = String(method).toLowerCase();
|
66 |
|
67 | Route[method] = function (options, callback) {
|
68 | var argv = [].slice.call(arguments);
|
69 |
|
70 |
|
71 | switch (typeof argv[0]) {
|
72 | case 'object':
|
73 | if (!(argv[0] instanceof RegExp)) {
|
74 | break;
|
75 | }
|
76 | case 'string':
|
77 | this.from(argv.shift());
|
78 | break;
|
79 | }
|
80 |
|
81 |
|
82 | if (typeof argv[0] === 'object') {
|
83 | this.set(argv.shift());
|
84 | }
|
85 |
|
86 |
|
87 | if (method === 'any') {
|
88 | this.unset('methods');
|
89 | } else {
|
90 | this.via(method.toUpperCase());
|
91 | }
|
92 |
|
93 |
|
94 | if (typeof argv[0] === 'function') {
|
95 | this.to(argv.shift());
|
96 | }
|
97 |
|
98 |
|
99 | if (argv.length) {
|
100 | throw new Error('Unexpected arguments');
|
101 | }
|
102 |
|
103 | return this;
|
104 | };
|
105 | });
|
106 | };
|