1
+ import type { Filter , Request } from './types' ;
1
2
import * as isGlob from 'is-glob' ;
2
3
import * as micromatch from 'micromatch' ;
3
4
import * as url from 'url' ;
4
5
import { ERRORS } from './errors' ;
5
6
6
- export function match ( context , uri , req ) {
7
+ export function match ( context : Filter , uri : string , req : Request ) : boolean {
7
8
// single path
8
- if ( isStringPath ( context ) ) {
9
- return matchSingleStringPath ( context , uri ) ;
9
+ if ( isStringPath ( context as string ) ) {
10
+ return matchSingleStringPath ( context as string , uri ) ;
10
11
}
11
12
12
13
// single glob path
13
- if ( isGlobPath ( context ) ) {
14
- return matchSingleGlobPath ( context , uri ) ;
14
+ if ( isGlobPath ( context as string ) ) {
15
+ return matchSingleGlobPath ( context as string [ ] , uri ) ;
15
16
}
16
17
17
18
// multi path
@@ -20,7 +21,7 @@ export function match(context, uri, req) {
20
21
return matchMultiPath ( context , uri ) ;
21
22
}
22
23
if ( context . every ( isGlobPath ) ) {
23
- return matchMultiGlobPath ( context , uri ) ;
24
+ return matchMultiGlobPath ( context as string [ ] , uri ) ;
24
25
}
25
26
26
27
throw new Error ( ERRORS . ERR_CONTEXT_MATCHER_INVALID_ARRAY ) ;
@@ -40,18 +41,18 @@ export function match(context, uri, req) {
40
41
* @param {String } uri 'http://example.org/api/b/c/d.html'
41
42
* @return {Boolean }
42
43
*/
43
- function matchSingleStringPath ( context , uri ) {
44
+ function matchSingleStringPath ( context : string , uri : string ) {
44
45
const pathname = getUrlPathName ( uri ) ;
45
46
return pathname . indexOf ( context ) === 0 ;
46
47
}
47
48
48
- function matchSingleGlobPath ( pattern , uri ) {
49
+ function matchSingleGlobPath ( pattern : string | string [ ] , uri : string ) {
49
50
const pathname = getUrlPathName ( uri ) ;
50
51
const matches = micromatch ( [ pathname ] , pattern ) ;
51
52
return matches && matches . length > 0 ;
52
53
}
53
54
54
- function matchMultiGlobPath ( patternList , uri ) {
55
+ function matchMultiGlobPath ( patternList : string | string [ ] , uri : string ) {
55
56
return matchSingleGlobPath ( patternList , uri ) ;
56
57
}
57
58
@@ -60,7 +61,7 @@ function matchMultiGlobPath(patternList, uri) {
60
61
* @param {String } uri 'http://example.org/api/b/c/d.html'
61
62
* @return {Boolean }
62
63
*/
63
- function matchMultiPath ( contextList , uri ) {
64
+ function matchMultiPath ( contextList : string [ ] , uri : string ) {
64
65
let isMultiPath = false ;
65
66
66
67
for ( const context of contextList ) {
@@ -79,14 +80,14 @@ function matchMultiPath(contextList, uri) {
79
80
* @param {String } uri from req.url
80
81
* @return {String } RFC 3986 path
81
82
*/
82
- function getUrlPathName ( uri ) {
83
+ function getUrlPathName ( uri : string ) {
83
84
return uri && url . parse ( uri ) . pathname ;
84
85
}
85
86
86
- function isStringPath ( context ) {
87
+ function isStringPath ( context : string ) {
87
88
return typeof context === 'string' && ! isGlob ( context ) ;
88
89
}
89
90
90
- function isGlobPath ( context ) {
91
+ function isGlobPath ( context : string ) {
91
92
return isGlob ( context ) ;
92
93
}
0 commit comments