-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
314 lines (274 loc) · 8.01 KB
/
index.js
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
'use strict';
var debug = require('debug')('base:routes');
var rethrow = require('template-error');
var router = require('en-route');
var utils = require('./utils');
module.exports = function(options) {
return function baseRoutes(app) {
if (!utils.isValid(app)) return;
/**
* The `Router` and `Route` classes are on the `app` instance,
* in case they need to be accessed directly.
*
* ```js
* var router = new app.Router();
* var route = new app.Route();
* ```
* @api public
*/
this.Router = router.Router;
this.Route = router.Route;
/**
* Lazily initalize `router`, to allow options and
* custom methods to be defined after instantiation.
*/
this.define('lazyRouter', function(methods) {
if (this.router == null) {
this.router = new this.Router({methods: utils.methods});
}
if (typeof methods !== 'undefined') {
this.router.method(methods);
}
});
/**
* Handle middleware `method` for the given `file`.
*
* ```js
* app.handle('methodName', file, next);
* ```
* @name .handle
* @param {String} `methodName` Name of the router method to handle.
* @param {Object} `file` View object
* @param {Function} `next` Callback function
* @return {undefined}
* @api public
*/
this.define('handle', function(method, file, next) {
debug('handling "%s" middleware for "%s"', method, file.basename);
this.lazyRouter();
if (typeof next !== 'function') {
next = function(err, file) {
app.handleError(method, file, function() {
throw err;
});
};
}
file.options = file.options || {};
if (!file.options.handled) {
file.options.handled = [];
}
// set router method on file.options
file.options.method = method;
file.options.handled.push(method);
this.emit(method, file);
// create callback
var cb = this.handleError(method, file, next);
// if not an instance of `Templates`, or if we're inside a collection
// or the collection is not specified on file.options just handle the route and return
if (!this.isTemplates || this.isCollection || !file.options.collection) {
this.router.handle(file, cb);
return;
}
// handle the app routes first, then handle the collection routes
var collection = this[file.options.collection];
this.router.handle(file, function(err) {
if (err) {
cb(err);
return;
}
collection.handle(method, file, cb);
});
});
/**
* Run the given middleware handler only if the file has not
* already been handled by `method`.
*
* ```js
* app.handleOnce(method, file, callback);
* // example
* app.handleOnce('onLoad', file, callback);
* ```
* @name .handleOnce
* @param {Object} `method` The name of the handler method to call.
* @param {Object} `file`
* @return {undefined}
* @api public
*/
this.define('handleOnce', function(method, file, next) {
if (!file.options.handled) {
file.options.handled = [];
}
if (typeof next !== 'function') {
next = file.next;
}
if (file.options.handled.indexOf(method) === -1) {
this.handle(method, file, next);
return;
}
next(null, file);
});
/**
* Handle middleware errors.
*/
this.define('handleError', function(method, file, next) {
var app = this;
return function(err) {
next = next || file.next;
if (typeof next !== 'function') {
throw new TypeError('expected a callback function');
}
if (err) {
if (err._handled === true) {
next();
return;
}
err._handled = true;
err.source = err.stack.split('\n')[1].trim();
err.reason = app._name + '#handle("' + method + '"): ' + file.path;
err.file = file;
if (app.hasListeners('error')) {
app.emit('error', err);
}
if (typeof next !== 'function') {
throw err;
}
if (err instanceof ReferenceError) {
try {
rethrow(file.content, file.data);
} catch (e) {
next(e);
return;
}
}
next(err);
return;
}
next(null, file);
};
});
/**
* Create a new Route for the given path. Each route
* contains a separate middleware stack. See the [en-route][]
* API documentation for details on adding handlers and
* middleware to routes.
*
* ```js
* app.create('posts');
* app.route(/blog/)
* .all(function(file, next) {
* // do something with file
* next();
* });
*
* app.post('whatever', {path: 'blog/foo.bar', content: 'bar baz'});
* ```
* @name .route
* @param {String} `path`
* @return {Object} Returns the instance for chaining.
* @api public
*/
this.define('route', function(/*path*/) {
this.lazyRouter();
return this.router.route.apply(this.router, arguments);
});
/**
* Add callback triggers to route parameters, where
* `name` is the name of the parameter and `fn` is the
* callback function.
*
* ```js
* app.param('title', function(view, next, title) {
* //=> title === 'foo.js'
* next();
* });
*
* app.onLoad('/blog/:title', function(view, next) {
* //=> view.path === '/blog/foo.js'
* next();
* });
* ```
* @name .param
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Returns the instance for chaining.
* @api public
*/
this.define('param', function(/*name, fn*/) {
this.lazyRouter();
this.router.param.apply(this.router, arguments);
return this;
});
/**
* Special route method that works just like the
* `router.METHOD()` methods, except that it matches
* all verbs.
*
* ```js
* app.all(/\.hbs$/, function(view, next) {
* // do stuff to view
* next();
* });
* ```
* @name .all
* @param {String} `path`
* @param {Function} `callback`
* @return {Object} `this` for chaining
* @api public
*/
this.define('all', function(path/*, callback*/) {
this.lazyRouter();
var route = this.route(path);
route.all.apply(route, [].slice.call(arguments, 1));
return this;
});
/**
* Add a router handler method to the instance. Interchangeable
* with the [handlers]() method.
*
* ```js
* app.handler('onFoo');
* // or
* app.handler(['onFoo', 'onBar']);
* ```
* @name .handler
* @param {String} `method` Name of the handler method to define.
* @return {Object} Returns the instance for chaining
* @api public
*/
this.define('handler', function(method) {
this.handlers(method);
return this;
});
/**
* Add one or more router handler methods to the instance.
*
* ```js
* app.handlers(['onFoo', 'onBar', 'onBaz']);
* // or
* app.handlers('onFoo');
* ```
* @name .handlers
* @param {Array|String} `methods` One or more method names to define.
* @return {Object} Returns the instance for chaining
* @api public
*/
this.define('handlers', function(methods) {
this.lazyRouter(methods);
mixinHandlers(methods);
return this;
});
function mixinHandlers(methods) {
utils.arrayify(methods).forEach(function(method) {
app.define(method, function(path) {
var route = this.route(path);
var args = [].slice.call(arguments, 1);
route[method].apply(route, args);
return this;
});
});
}
// Mix router handler methods onto the intance
mixinHandlers(utils.methods);
return baseRoutes;
};
};