1+ import type { Filter , Request } from './types' ;
12import * as isGlob from 'is-glob' ;
23import * as micromatch from 'micromatch' ;
34import * as url from 'url' ;
45import { ERRORS } from './errors' ;
56
6- export function match ( context , uri , req ) {
7+ export function match ( context : Filter , uri : string , req : Request ) : boolean {
78 // single path
8- if ( isStringPath ( context ) ) {
9- return matchSingleStringPath ( context , uri ) ;
9+ if ( isStringPath ( context as string ) ) {
10+ return matchSingleStringPath ( context as string , uri ) ;
1011 }
1112
1213 // 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 ) ;
1516 }
1617
1718 // multi path
@@ -20,7 +21,7 @@ export function match(context, uri, req) {
2021 return matchMultiPath ( context , uri ) ;
2122 }
2223 if ( context . every ( isGlobPath ) ) {
23- return matchMultiGlobPath ( context , uri ) ;
24+ return matchMultiGlobPath ( context as string [ ] , uri ) ;
2425 }
2526
2627 throw new Error ( ERRORS . ERR_CONTEXT_MATCHER_INVALID_ARRAY ) ;
@@ -40,18 +41,18 @@ export function match(context, uri, req) {
4041 * @param {String } uri 'http://example.org/api/b/c/d.html'
4142 * @return {Boolean }
4243 */
43- function matchSingleStringPath ( context , uri ) {
44+ function matchSingleStringPath ( context : string , uri : string ) {
4445 const pathname = getUrlPathName ( uri ) ;
4546 return pathname . indexOf ( context ) === 0 ;
4647}
4748
48- function matchSingleGlobPath ( pattern , uri ) {
49+ function matchSingleGlobPath ( pattern : string | string [ ] , uri : string ) {
4950 const pathname = getUrlPathName ( uri ) ;
5051 const matches = micromatch ( [ pathname ] , pattern ) ;
5152 return matches && matches . length > 0 ;
5253}
5354
54- function matchMultiGlobPath ( patternList , uri ) {
55+ function matchMultiGlobPath ( patternList : string | string [ ] , uri : string ) {
5556 return matchSingleGlobPath ( patternList , uri ) ;
5657}
5758
@@ -60,7 +61,7 @@ function matchMultiGlobPath(patternList, uri) {
6061 * @param {String } uri 'http://example.org/api/b/c/d.html'
6162 * @return {Boolean }
6263 */
63- function matchMultiPath ( contextList , uri ) {
64+ function matchMultiPath ( contextList : string [ ] , uri : string ) {
6465 let isMultiPath = false ;
6566
6667 for ( const context of contextList ) {
@@ -79,14 +80,14 @@ function matchMultiPath(contextList, uri) {
7980 * @param {String } uri from req.url
8081 * @return {String } RFC 3986 path
8182 */
82- function getUrlPathName ( uri ) {
83+ function getUrlPathName ( uri : string ) {
8384 return uri && url . parse ( uri ) . pathname ;
8485}
8586
86- function isStringPath ( context ) {
87+ function isStringPath ( context : string ) {
8788 return typeof context === 'string' && ! isGlob ( context ) ;
8889}
8990
90- function isGlobPath ( context ) {
91+ function isGlobPath ( context : string ) {
9192 return isGlob ( context ) ;
9293}
0 commit comments