Skip to content

Commit

Permalink
add caseSenstive options to routes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jul 27, 2024
1 parent 8fe800a commit 50d4c04
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.17.0
### Features
* allow using glob as domain name
* add `caseSenstive` options to routes [#38](https://github.com/jcubic/wayne/issues/38)

## 0.16.5
### Bug fix
Expand Down
46 changes: 31 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export function RouteParser() {
const results = [];
for (let i=keys.length; i--;) {
const key = keys[i];
const route = input[key];
let pattern;
// check if origin match for full URL
const re = /:\/\/([^\/]+)(\/.*)/;
Expand All @@ -274,20 +275,24 @@ export function RouteParser() {
pattern = key;
}
const parts = parse(pattern);
m = url.match(new RegExp('^' + parts.re + '$'));
if (m) {
const matched = m.slice(1);
const data = {};
if (matched.length) {
parts.names.forEach((name, i) => {
data[name] = matched[i];
route.forEach(({ handler, options }) => {
const caseSensitive = options.caseSensitive ?? true;
m = url.match(new RegExp('^' + parts.re + '$', caseSensitive ? '' : 'i'));
if (m) {
const matched = m.slice(1);
const data = {};
if (matched.length) {
parts.names.forEach((name, i) => {
data[name] = matched[i];
});
}
results.push({
pattern: key,
handler,
data
});
}
results.push({
pattern: key,
data
});
}
});
}
return results;
};
Expand Down Expand Up @@ -470,6 +475,17 @@ export function FileSystem(options) {
}
};
}
// -----------------------------------------------------------------------------
function pluck(name) {
return function(object) {
return object[name];
};
}

// -----------------------------------------------------------------------------
function handlers(arr) {
return arr.map(pluck('handler'));
}

// -----------------------------------------------------------------------------
// :: Main Wayne Constructor
Expand Down Expand Up @@ -513,7 +529,7 @@ export class Wayne {
if (!(have_wildcard && selected_route)) {
selected_route = match[0];
}
const fns = [...this._middlewares, ...routes[selected_route.pattern]];
const fns = [...this._middlewares, ...handlers(match)];
req.params = selected_route.data;
setTimeout(function() {
reject('Timeout Error');
Expand Down Expand Up @@ -562,15 +578,15 @@ export class Wayne {
});
}
method(method) {
return function(url, fn) {
return function(url, handler, options = {}) {
if (!this._routes[method]) {
this._routes[method] = {};
}
const routes = this._routes[method];
if (!routes[url]) {
routes[url] = [];
}
routes[url].push(fn);
routes[url].push({ handler, options });
return this;
};
}
Expand Down
Loading

0 comments on commit 50d4c04

Please sign in to comment.