-
Notifications
You must be signed in to change notification settings - Fork 140
/
index.js
277 lines (241 loc) · 5.99 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
/*!
* Express - Resource
* Copyright(c) 2010-2012 TJ Holowaychuk <[email protected]>
* Copyright(c) 2011 Daniel Gasienica <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var express = require('express')
, methods = require('methods')
, debug = require('debug')('express-resource')
, lingo = require('lingo')
, app = express.application
, en = lingo.en;
/**
* Pre-defined action ordering.
*/
var orderedActions = [
'index' // GET /
, 'new' // GET /new
, 'create' // POST /
, 'show' // GET /:id
, 'edit' // GET /edit/:id
, 'update' // PUT /:id
, 'patch' // PATCH /:id
, 'destroy' // DEL /:id
];
/**
* Expose `Resource`.
*/
module.exports = Resource;
/**
* Initialize a new `Resource` with the given `name` and `actions`.
*
* @param {String} name
* @param {Object} actions
* @param {Server} app
* @api private
*/
function Resource(name, actions, app) {
this.name = name;
this.app = app;
this.routes = {};
actions = actions || {};
this.base = actions.base || '/';
if ('/' != this.base[this.base.length - 1]) this.base += '/';
this.format = actions.format;
this.id = actions.id || this.defaultId;
this.param = ':' + this.id;
// default actions
for (var i = 0, key; i < orderedActions.length; ++i) {
key = orderedActions[i];
if (actions[key]) this.mapDefaultAction(key, actions[key]);
}
// auto-loader
if (actions.load) this.load(actions.load);
};
/**
* Set the auto-load `fn`.
*
* @param {Function} fn
* @return {Resource} for chaining
* @api public
*/
Resource.prototype.load = function(fn){
var self = this
, id = this.id;
this.loadFunction = fn;
this.app.param(this.id, function(req, res, next){
function callback(err, obj){
if (err) return next(err);
// TODO: ideally we should next() passed the
// route handler
if (null == obj) return res.send(404);
req[id] = obj;
next();
};
// Maintain backward compatibility
if (2 == fn.length) {
fn(req.params[id], callback);
} else {
fn(req, req.params[id], callback);
}
});
return this;
};
/**
* Retun this resource's default id string.
*
* @return {String}
* @api private
*/
Resource.prototype.__defineGetter__('defaultId', function(){
return this.name
? en.singularize(this.name.split('/').pop())
: 'id';
});
/**
* Map http `method` and optional `path` to `fn`.
*
* @param {String} method
* @param {String|Function|Object} path
* @param {Function} fn
* @return {Resource} for chaining
* @api public
*/
Resource.prototype.map = function(method, path, fn){
var self = this
, orig = path;
if (method instanceof Resource) return this.add(method);
if ('function' == typeof path) fn = path, path = '';
if ('object' == typeof path) fn = path, path = '';
if ('/' == path[0]) path = path.substr(1);
else path = path ? this.param + '/' + path : this.param;
method = method.toLowerCase();
// setup route pathname
var route = this.base + (this.name || '');
if (this.name && path) route += '/';
route += path;
route += '.:format?';
// register the route so we may later remove it
(this.routes[method] = this.routes[method] || {})[route] = {
method: method
, path: route
, orig: orig
, fn: fn
};
// apply the route
this.app[method](route, function(req, res, next){
req.format = req.params.format || req.format || self.format;
if (req.format) res.type(req.format);
if ('object' == typeof fn) {
if (fn[req.format]) {
fn[req.format](req, res, next);
} else {
res.format(fn);
}
} else {
fn(req, res, next);
}
});
return this;
};
/**
* Nest the given `resource`.
*
* @param {Resource} resource
* @return {Resource} for chaining
* @see Resource#map()
* @api public
*/
Resource.prototype.add = function(resource){
var app = this.app
, routes
, route;
// relative base
resource.base = this.base
+ (this.name ? this.name + '/': '')
+ this.param + '/';
// re-define previous actions
for (var method in resource.routes) {
routes = resource.routes[method];
for (var key in routes) {
route = routes[key];
delete routes[key];
if (method == 'del') method = 'delete';
app.routes[method].forEach(function(route, i){
if (route.path == key) {
app.routes[method].splice(i, 1);
}
})
resource.map(route.method, route.orig, route.fn);
}
}
return this;
};
/**
* Map the given action `name` with a callback `fn()`.
*
* @param {String} key
* @param {Function} fn
* @api private
*/
Resource.prototype.mapDefaultAction = function(key, fn){
switch (key) {
case 'index':
this.get('/', fn);
break;
case 'new':
this.get('/new', fn);
break;
case 'create':
this.post('/', fn);
break;
case 'show':
this.get(fn);
break;
case 'edit':
this.get('edit', fn);
break;
case 'update':
this.put(fn);
break;
case 'patch':
this.patch(fn);
break;
case 'destroy':
this.del(fn);
break;
}
};
/**
* Setup http verb methods.
*/
methods.concat(['del', 'all']).forEach(function(method){
Resource.prototype[method] = function(path, fn){
if ('function' == typeof path
|| 'object' == typeof path) fn = path, path = '';
this.map(method, path, fn);
return this;
}
});
/**
* Define a resource with the given `name` and `actions`.
*
* @param {String|Object} name or actions
* @param {Object} actions
* @return {Resource}
* @api public
*/
app.resource = function(name, actions, opts){
var options = actions || {};
if ('object' == typeof name) actions = name, name = null;
if (options.id) actions.id = options.id;
this.resources = this.resources || {};
if (!actions) return this.resources[name] || new Resource(name, null, this);
for (var key in opts) options[key] = opts[key];
var res = this.resources[name] = new Resource(name, actions, this);
return res;
};