-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
205 lines (157 loc) · 7.13 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
const verbs = [
'all','get','post','put','head','delete','options','trace',
'copy','lock','mkcol','move','purge','propfind','proppatch',
'unlock','report','mkactivity','checkout','merge','m-search',
'notify','subscribe','unsubscribe','patch','search','connect'
];
//the http methods supported by express + all for all.
//flatten array functiont aken from gist.github
const flatten = arr => {
for (let i=0; i < arr.length;Array.isArray(arr[i]) && arr.splice(i, 1, ...arr[i]) || i++){}
return arr
}
//use var so it can be cleaned up when the app starts.
var controllers = {};//cache controllers.
var cPath = false;//cache controller path
const InvalidArgumentException = (...args) => { throw new Error(args) };
const Map = function Map(...args){
//first assure our controller path has been set
let path = cPath || this.get('controllers');//if cPath = false, retrieve controllers from app, only once.
// if path === undefined, then controllers hasn't been set on app instance.
if(path === undefined){
throw new Error("You must use app.set('controllers','/path/to/controllers') to set the controller path before using `express-map2`");
}
// if cPath hasn't been set, set it.
if(!cPath){
cPath = path;
}
//next parse our arguments
if(args.length >= 3){
var prefix = args.shift();
var routes = args.pop();
var middleware = args;
}
else if(args.length == 2){
var routes = args[1];
if(args[0] instanceof Array){
var middleware = args[0];
var prefix = '';
}
else if(typeof args[0] === 'function'){
var middleware = [args[0]];
var prefix = '';
}
else if(typeof args[0] === 'string'){
var middleware = [];
var prefix = args[0];
}
else{
InvalidArgumentException(args);
}
}else{
var prefix = '';
var middleware = [];
var routes = args[0];
}
//normalize middleware and parse
flatten(middleware);
middleware.map(handlers=>{
if(typeof handlers == 'string') {
handlers = handlers.split(',');
handlers = handlers.map((v) => { return v.trim().trimLeft() });
}
return handlers;
});
middleware = flatten(middleware);
if(typeof prefix !== 'string'){
throw new Error('`express-map` invalid argument exception. option `prefix` must be a string.');
}
if(!(middleware instanceof Array)){
throw new Error('`express-map` invalid argument exception. option `middleware` must be a function or array of functions');
}
if(typeof routes !== 'object' || routes instanceof Array){
throw new Error('`express-map` invalid argument exception option `routes` must be an object.')
}
//now process it
for(let prop in routes){
//normalize spaces in resource
let resource = prop.replace(/\s+/g,' ').replace(/^\s+|\s+$/,'');
if(resource.indexOf(' ') != -1 ){
resource = resource.split(' ').map((v)=>{ return v.trim().trimLeft(); });
}else{
// throw error since we can't parse it.
throw new Error('`express-map` unable to parse option `routes` at line `"'+prop+'":"'+routes[prop]+'",`');
}
let verb = resource[0].toLowerCase();
//create routePath from prefix and actual path. replace duplicate slashes
let routePath = (prefix + resource[1].trim()).replace(/\/+/g, '/');
if(routePath != '/'){
routePath = routePath.replace(/\/+$/, '');//only trim trailing slash if not `/`
}
if(verbs.indexOf(verb) == -1){
throw new Error('`express-map` encountered an invalid http verb `'+verb.toUpperCase()+'`.');
}
let handlers = routes[prop];
if(typeof handlers != 'string'){
let msg = '`express-map` encountered an invalid handler format `'+handlers+'`.\n'+
'please define your controllers in the form of <module>.<function> or just <module> if module is a function, and seperate them by a comma';
throw new Error(msg);
}else{
if(handlers.indexOf(',') >=0){
handlers = handlers.split(',');
handlers = handlers.map((v) => { return v.trim().trimLeft() });
}else{
handlers = [handlers];
}
var strHandlers = [];//for the logger output
if(middleware.length){
handlers = middleware.concat(...handlers);
//handlers.splice(1,0,...middleware);//insert the middleware into the list of handlers.
}
handlers = handlers.map((v)=>{
//handle case when a handler is already a function,
//such as when passed global middleware
if(typeof v == 'function'){
strHandlers.push(v.name||'[function]');
return v;
}
if(v.indexOf('.') > 0){
v = v.split('.');
}else{
v = [v];
}
if(!controllers.hasOwnProperty(v[0])){
controllers[v[0]] = require(path +v[0]);
}
//technically we could use `eval('h = '+strH+';');` but that seems like a dirtier way to handle both concerns.
h = controllers;
let strH = '';
for(let i=0;i<v.length;i++){
h = h[v[i]]; //recursively point to the subsequent child until we reach the function.
if(i>0) strH += '.';
strH +=v[i];
}
if(typeof h !='function'){
throw new Error('`express-map` `'+strH+'` does not resolve to a function and is not a valid handler for route `'+routePath+'`');
}
strHandlers.push(strH);
return h;//h should now point directly to each handler function, regardless of depth.
});
// console.log(verb);
// console.log(routePath);
// console.log(strHandlers);
// console.log(handlers);
this[verb](routePath,...handlers);//call the associated express method with handlers as arguments
}
}
};
module.exports = (app) => {
app.map = Map.bind(app);//bind express app as `this` context.
var oldListener = app.listen;
//cleanup controllers once the app starts.
app.listen = (...args)=>{
controllers = null;
app.listen = oldListener;
app.listen.apply(app,args);
};
};