Skip to content
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

Chore: Add pathIsRegexp option to docs, revert new trailing option as unneeded #190

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions lib/layer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { parse: parseUrl, format: formatUrl } = require('node:url');

const { pathToRegexp, compile, parse, stringify } = require('path-to-regexp');

Check warning on line 3 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

'stringify' is assigned a value but never used.

Check warning on line 3 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

'stringify' is assigned a value but never used.

Check warning on line 3 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

'stringify' is assigned a value but never used.

module.exports = class Layer {
/**
Expand All @@ -13,6 +13,7 @@
* @param {String=} opts.name route name
* @param {String=} opts.sensitive case sensitive (default: false)
* @param {String=} opts.strict require the trailing slash (default: false)
* @param {Boolean=} opts.pathIsRegexp if true, treat `path` as a regular expression
* @returns {Layer}
* @private
*/
Expand Down Expand Up @@ -45,6 +46,11 @@
if (this.opts.pathIsRegexp === true) {
this.regexp = new RegExp(path);
} else if (this.path) {
if (this.opts.strict === true) {
// path-to-regexp renamed strict to trailing in v8.1.0
this.opts.trailing = false;
}

const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
this.regexp = regex;
this.paramNames = keys;
Expand Down Expand Up @@ -114,7 +120,7 @@
const url = this.path.replace(/\(\.\*\)/g, '');

if (typeof params !== 'object') {
args = Array.prototype.slice.call(arguments);

Check warning on line 123 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 123 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 123 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Use the rest parameters instead of 'arguments'.
if (typeof args[args.length - 1] === 'object') {
options = args[args.length - 1];
args = args.slice(0, -1);
Expand Down Expand Up @@ -197,7 +203,7 @@
const x = names.indexOf(param);
if (x > -1) {
// iterate through the stack, to figure out where to place the handler fn
stack.some((fn, i) => {

Check warning on line 206 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Array.prototype.some() expects a value to be returned at the end of arrow function.

Check warning on line 206 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Array.prototype.some() expects a value to be returned at the end of arrow function.

Check warning on line 206 in lib/layer.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Array.prototype.some() expects a value to be returned at the end of arrow function.
// param handlers are always first, so when we find an fn w/o a param property, stop here
// if the param handler at this part of the stack comes after the one we are adding, stop here
if (!fn.param || names.indexOf(fn.param) > x) {
Expand Down
3 changes: 1 addition & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
const arrPaths = middleware[0];
for (const p of arrPaths) {
router.use.apply(router, [p, ...middleware.slice(1)]);

Check warning on line 129 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Unnecessary '.apply()'.

Check warning on line 129 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Unnecessary '.apply()'.

Check warning on line 129 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Unnecessary '.apply()'.
}

return this;
Expand Down Expand Up @@ -228,7 +228,7 @@
ctx.routerPath;
const matched = router.match(path, ctx.method);
if (ctx.matched) {
ctx.matched.push.apply(ctx.matched, matched.path);

Check warning on line 231 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the spread operator instead of '.apply()'.

Check warning on line 231 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Use the spread operator instead of '.apply()'.

Check warning on line 231 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Use the spread operator instead of '.apply()'.
} else {
ctx.matched = matched.path;
}
Expand All @@ -246,7 +246,7 @@

const layerChain = (
router.exclusive ? [mostSpecificLayer] : matchedLayers
).reduce((memo, layer) => {

Check warning on line 249 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

`Array#reduce()` is not allowed

Check warning on line 249 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

`Array#reduce()` is not allowed

Check warning on line 249 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

`Array#reduce()` is not allowed
memo.push((ctx, next) => {
ctx.captures = layer.captures(path, ctx.captures);
ctx.request.params = layer.params(path, ctx.captures, ctx.params);
Expand Down Expand Up @@ -381,9 +381,9 @@
*/
all(name, path, middleware) {
if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2);

Check warning on line 384 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 384 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 384 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Use the rest parameters instead of 'arguments'.
} else {
middleware = Array.prototype.slice.call(arguments, 1);

Check warning on line 386 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 386 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Use the rest parameters instead of 'arguments'.

Check warning on line 386 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Use the rest parameters instead of 'arguments'.
path = name;
name = null;
}
Expand Down Expand Up @@ -467,7 +467,7 @@
// support array of paths
if (Array.isArray(path)) {
for (const curPath of path) {
router.register.call(router, curPath, methods, middleware, opts);

Check warning on line 470 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Unnecessary '.call()'.

Check warning on line 470 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Unnecessary '.call()'.

Check warning on line 470 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Unnecessary '.call()'.
}

return this;
Expand All @@ -481,8 +481,7 @@
strict: opts.strict || false,
prefix: opts.prefix || '',
ignoreCaptures: opts.ignoreCaptures,
pathIsRegexp: opts.pathIsRegexp,
trailing: opts.trailing
pathIsRegexp: opts.pathIsRegexp
});

// if parent prefix exists, add prefix to new route
Expand Down Expand Up @@ -555,7 +554,7 @@
*/
url(name, ...args) {
const route = this.route(name);
if (route) return route.url.apply(route, args);

Check warning on line 557 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the spread operator instead of '.apply()'.

Check warning on line 557 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Use the spread operator instead of '.apply()'.

Check warning on line 557 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node 22 on ubuntu-latest

Use the spread operator instead of '.apply()'.

return new Error(`No route found for name: ${String(name)}`);
}
Expand Down
8 changes: 4 additions & 4 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ describe('Router#opts', () => {
it('responds with 200', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand Down Expand Up @@ -1689,7 +1689,7 @@ describe('Router#opts', () => {
it('responds with 404 when has a trailing slash', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand All @@ -1704,7 +1704,7 @@ describe('use middleware with opts', () => {
it('responds with 200', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand All @@ -1720,7 +1720,7 @@ describe('use middleware with opts', () => {
it('responds with 404 when has a trailing slash', async () => {
const app = new Koa();
const router = new Router({
trailing: false
strict: true
});
router.get('/info', (ctx) => {
ctx.body = 'hello';
Expand Down
Loading