-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (122 loc) · 4.25 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
const is = require( '@lvchengbin/is' );
const re = require( 'path-to-regexp' );
const methods = [ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ];
function match( path, ctx, options ) {
let matches;
if( is.regexp( path ) ) {
path.lastIndex = 0;
matches = path.exec( ctx.path );
} else if( is.object( path ) ) {
const keys = Object.keys( path );
const query = ctx.query;
matches = [ true ];
for( let key of keys ) {
const item = path[ key ];
if( item === false && is.undefined( query[ key ] ) ) continue;
if( is.undefined( query[ key ] ) ) {
matches = false;
break;
}
if( is.regexp( item ) ) {
path[ key ].lastIndex = 0;
if( !path[ key ].test( query[ key ] ) ) {
matches = false;
break;
}
} else if( is.array( item ) ) {
if( item.indexOf( query[ key ] ) < 0 ) {
matches = false;
break;
}
} else if( item === true ) {
continue;
} else if( query[ key ] != path[ key ] ) {
matches = false;
break;
}
}
} else {
const keys = [];
matches = re( path, keys, options ).exec( ctx.path );
if( matches ) {
ctx.params = {};
for( let i = 0, l = keys.length; i < l; i += 1 ) {
const name = keys[ i ].name;
ctx.params[ name ] = matches[ i + 1 ];
}
}
}
return matches;
}
function createMethod( method ) {
return ( path, fn, options = {} ) => {
const func = async function( ctx, next ) {
if( method ) {
if( ctx.method !== method ) return next();
} else {
/**
* if the method wasn't specified during create the "method" function,
* to use the methods in options instead.
*/
if( !options.methods || !options.methods.length ) {
options.methods = [ 'GET' ];
}
if( !Array.isArray( options.methods ) ) {
options.methods = [ options.methods ];
}
options.methods = options.methods.map( m => m.toUpperCase() );
if( options.methods.indexOf( ctx.method ) < 0 ) return next();
}
let matches;
if( is.generator( path ) ) {
for( const i of path() ) {
matches = match( i, ctx, options );
if( matches ) break;
}
} else if( is.array( path ) ) {
for( const i of path ) {
matches = match( i, ctx, options );
if( matches ) break;
}
} else {
matches = match( path, ctx, options );
}
if( matches ) {
const args = matches.slice( 1 ).map( v => decodeURIComponent( v ) );
ctx.routerMatches = args;
return Promise.resolve( fn.call( this.app, ...arguments, ...args ) );
}
return next();
};
return this.app ? this.app.use( func ) : func;
};
}
class Router {
constructor( app ) {
this.app = app;
this.methods = methods;
for( let method of methods ) {
this[ method.toLowerCase() ] = function( path, fn, options ) {
if( is.array( fn ) ) {
for( const f of fn ) {
this[ method.toLowerCase() ]( path, f, options );
}
return;
}
createMethod.call( this, method )( ...arguments );
}
}
}
any( m, path, fn, options = {} ) {
const handler = createMethod.call( this );
options.methods = m === '*' ? methods : m;
if( is.array( fn ) ) {
for( const f of fn ) {
this.any( m, path, f, options );
}
return;
}
return handler( path, fn, options );
}
}
module.exports = Router;