From 18f0d7c3fc3ab4a62c5f53604f624c9217c10f83 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 17 Aug 2024 23:15:43 +0300 Subject: [PATCH] . --- index.js | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 21e399b..3eabaed 100644 --- a/index.js +++ b/index.js @@ -179,20 +179,17 @@ class API { methods.forEach((_method) => { // Method must be a string if (typeof _method === 'string') { - // Check for wild card at this level - if (routes['ROUTES']['*']) { + const wildcardForPathLevel = routes[i] == '' ? routes['ROUTES']['*'] : routes['ROUTES'][route[i]]['*']; + if (wildcardForPathLevel != null) { if ( - routes['ROUTES']['*']['MIDDLEWARE'] && + wildcardForPathLevel['MIDDLEWARE'] && (route[i] !== '*' || _method !== '__MW__') ) { - _stack['*'][method] = routes['ROUTES']['*']['MIDDLEWARE'].stack; + _stack['*'][method] = wildcardForPathLevel['MIDDLEWARE'].stack; } - if ( - routes['ROUTES']['*']['METHODS'] && - routes['ROUTES']['*']['METHODS'][method] - ) { + if (wildcardForPathLevel?.['METHODS']?.[method] != null) { _stack['m'][method] = - routes['ROUTES']['*']['METHODS'][method].stack; + wildcardForPathLevel['METHODS'][method].stack; } } // end if wild card @@ -216,13 +213,12 @@ class API { : _stack['*'][method] ? _stack['*'][method].concat(stack) : stack, - // inherited: _stack[method] ? _stack[method] : [], route: '/' + parsedPath.join('/'), path: '/' + this._prefix.concat(parsedPath).join('/'), }; - // If mounting middleware - if (method === '__MW__') { + const isMountingMiddleware = _method === '__MW__'; + if (isMountingMiddleware) { // Merge stacks if middleware exists if (routes['ROUTES'][route[i]]['MIDDLEWARE']) { meta.stack = @@ -234,17 +230,6 @@ class API { } // Add/update middleware routes['ROUTES'][route[i]]['MIDDLEWARE'] = meta; - - // Apply middleware to all child middlware routes - // if (route[i] === "*") { - // // console.log("APPLY NESTED MIDDLEWARE"); - // // console.log(JSON.stringify(routes["ROUTES"], null, 2)); - // Object.keys(routes["ROUTES"]).forEach((nestedRoute) => { - // if (nestedRoute != "*") { - // console.log(nestedRoute); - // } - // }); - // } } else { // Create the methods section if it doesn't exist if (!routes['ROUTES'][route[i]]['METHODS']) @@ -266,8 +251,6 @@ class API { routes['ROUTES'][route[i]]['METHODS'][_method] = meta; } // end else - // console.log('STACK:',meta); - // If there's a wild card that's not at the end } else if (route[i] === '*') { throw new ConfigurationError(