-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
286 lines (249 loc) · 6.6 KB
/
index.js
File metadata and controls
286 lines (249 loc) · 6.6 KB
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
/**
* DEPENDENCIES
*/
var glob = require('glob'),
path = require('path'),
_ = require('underscore.string');
/**
* CONSTANTS
*/
var MAGIC_ACTIONS = {
'create': {method: 'post', args: null},
'read': {method: 'get', args: [':id']},
'update': {method: 'put', args: [':id']},
'del': {method: 'delete', args: [':id']},
'search': {method: 'get', args: null},
'index': {method: 'get', args: null},
'list': {method: 'get', args: null}
};
/**
* PUBLIC API
*/
/**
* Attach an express.js app
*
* @param {Function} app
*/
exports.bind = function (app) {
this.app = app;
return this;
};
/**
* Automatic router middleware
*
* @param {Function} app An express app
*/
exports.load = function (app, options) {
var self = this;
if (typeof app === 'object' && typeof options !== 'object') {
options = app;
app = null;
}
if (app) {
this.bind(app);
}
options = options || {};
if (!options.pattern) {
throw new Error('You must specify a controller matching pattern! E.g. \'controllers/*.js\'');
}
// Make this sync because we want this to be executed where we intend:
glob.sync(options.pattern).forEach(function (file) {
var controller = require(file),
controllerName = getControllerName(file, options.nameRegExp),
mountPoint = stripUnwantedSlashes(options.prefix) + '/' + (controllerName.toLowerCase() !== 'home' ? controllerName : '');
self.mount(mountPoint, controller);
});
return this;
};
/**
* Mount a given controller
*
* @param {String} name Used as mount point
* @param {Object} controller
*/
exports.mount = function (name, controller) {
if (!this.app) {
throw new Error('An Express.js app must be bound to exctrl before mounting a controller!');
}
if (typeof name === 'object' && typeof controller === 'undefined') {
controller = name;
name = null;
}
if (typeof controller.name === 'string') {
name = controller.name;
}
var app = this.app;
Object.keys(controller).forEach(function (action) {
if (action === 'name' && controller[action] === name) {
return;
}
var magic = MAGIC_ACTIONS[action] || {},
route = {
method: magic.method || getMethod(action) || 'get',
url: [],
args: magic.args || [],
handler: controller[action]
};
if (name) {
route.url.push(stripUnwantedSlashes(name));
}
if (action === 'init') {
controller[action](app, route.url.join('/'));
return;
}
if (!magic.method) {
// Custom actions (i.e. not magic) needs its name without HTTP method added to the url:
arrayAdd(route.url, getUrlPart(action));
}
// Is the action an array?
if (Array.isArray(route.handler)) {
if (route.handler.length > 1) {
// All preceeding elements in the array are url params/chunks/arguments e.g. ':id':
route.args = route.handler.splice(0, route.handler.length - 1);
}
// The last element in the array is the real handler:
route.handler = route.handler.pop();
}
if (typeof route.handler !== 'function') {
throw new Error('Bad handler type for action: ' + name + ':' + action + ', expected: \'function\', but was: \'' + typeof(route.handler) + '\'');
}
// Add the url params/chunks/arguments to the route.url:
arrayAdd(route.url, getUrlChunks(route.args));
if (app.get('env') === 'development') {
console.log('CONTROLLER-ACTION:ADD', route.method.toUpperCase() + ' ' + getUrl(route.url));
}
// Add the route to app:
app[route.method].apply(app,
[getUrl(route.url)]
.concat(getMiddlewares(route.args))
.concat(route.handler.bind(controller))
);
});
return this;
};
/**
* PRIVATE API
*/
/**
* Add one array's elements to another
*
* @param {Array} dest
* @param {Array} src
*/
function arrayAdd (dest, src) {
dest.push.apply(dest, src);
}
/**
* Get controller name
*
* Gets the controller name from a filename
* If `extractorRegExp` is provided and it matches
* the filename, the first match group will be returned.
* Otherwise the filename without extension will be returned.
*
* Example 1:
* file: /var/tmp/super.controller.js
* extractorRegExp: /([^\/\\]+).controller.js$/
* Returns: 'super'
*
* Example 2:
* file: /var/tmp/controllers/super.js
* extractorRegExp: NULL
* Returns: 'super'
*
* @param {String} file
* @param {RegExp} extractorRegExp
* @returns {String}
*/
function getControllerName (file, extractorRegExp) {
var name = extractorRegExp ? file.match(extractorRegExp)[1] : null;
return name || path.basename(file).slice(0, -path.extname(file).length);
}
/**
* Get an URL from an array of chunks
*
* Joins the chunks with '/' and prepends it as well
*
* @param {Array} urlChunks
* @returns {String}
*/
function getUrl (urlChunks) {
return '/' + urlChunks.join('/');
}
/**
* Get all middlewares
*
* Returns all middlewares, i.e. functions, in an array
*
* @param {Array} arr
* @returns {Array}
*/
function getMiddlewares (arr) {
return arr.filter(function (e) {
return typeof e === 'function';
});
}
/**
* Get all url chunks
*
* Returns all url chunks, i.e. not functions, in an array
*
* @param {Array} arr
* @returns {Array}
*/
function getUrlChunks (arr) {
return arr.filter(function (e) {
return typeof e !== 'function';
});
}
/**
* Removes leading and trailing slashes in url
*
* @param {String} url
* @returns {String}
*/
function stripUnwantedSlashes (url) {
return _.trim(url || '', '/');
}
/**
* Get HTTP method from action name
*
* Expects action name to be camelCased, e.g:
* - postThing -> post
* - getSuperThingy -> get
* - weirdAction -> NULL
*
* @param {String} action
* @returns {String}
*/
function getMethod (action) {
var method = action.split(/[^a-z]/)[0];
if (['post', 'head', 'get', 'delete', 'put', 'trace', 'options', 'patch'].indexOf(method) >= 0) {
return method;
}
return null;
}
/**
* Get url part from action name
*
* Expects action name to be camelCased, e.g:
* - postThing -> thing
* - getSuperThingy -> super-thingy
* - myAction -> my-action
*
* @param {String} action
* @returns {String}
*/
function getUrlPart (action) {
var method = getMethod(action);
if (method) {
action = action.slice(method.length);
}
return stripUnwantedSlashes(action).split('/').filter(function (chunk) { return !!chunk; })
.map(function (chunk) {
if (chunk.indexOf(':') < 0) {
return _.ltrim(_.dasherize(chunk), '-');
}
return chunk;
});
}