8000 fix(middleware): allow usage of `basePath` in index route by naorpeled · Pull Request #227 · jeremydaly/lambda-api · GitHub
[go: up one dir, main page]

Skip to content

fix(middleware): allow usage of basePath in index route #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
  • Loading branch information
naorpeled committed Aug 17, 2024
commit 18f0d7c3fc3ab4a62c5f53604f624c9217c10f83
33 changes: 8 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 =
Expand All @@ -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'])
Expand All @@ -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(
Expand Down
Loading
0