Skip to content

Commit e3d5870

Browse files
authored
To v9.x.x 🚀 (#87)
* avoid generating package-lock.json ☔️ .. * update .gitignore file 🐞 .. * update travis ci pipeline 🎲 .. * update LICENSE 🗝 .. * better code for layer 🌸 .. * reduce line of code + better throw code 🍤 .. * clean test + fix #64 🧪.. * update README.md 📋 .. * revert pkg version + update scripts 🎗 ..
1 parent 53a6e0d commit e3d5870

10 files changed

+94
-112
lines changed

.gitignore

+15-37
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# nyc test coverage
18-
.nyc_output
19-
20-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21-
.grunt
22-
23-
# node-waf configuration
24-
.lock-wscript
1+
# OS #
2+
###################
3+
.DS_Store
4+
.idea
5+
Thumbs.db
6+
tmp/
7+
temp/
258

26-
# Compiled binary addons (http://nodejs.org/api/addons.html)
27-
build/Release
289

29-
# Dependency directories
10+
# Node.js #
11+
###################
3012
node_modules
31-
jspm_packages
32-
3313
package-lock.json
3414
yarn.lock
15+
*.log
3516

36-
# Optional npm cache directory
37-
.npm
38-
39-
# Optional REPL history
40-
.node_repl_history
41-
.env*
42-
!.env.test
4317

44-
.DS_Store
18+
# NYC #
19+
###################
20+
coverage
21+
*.lcov
22+
.nyc_output

.npmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package-lock=false
1+
package-lock=false

.travis.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: node_js
22
node_js:
3-
- "8"
4-
- "9"
5-
- "10"
6-
- "11"
7-
- "12"
8-
notifications:
9-
email:
10-
on_success: never
3+
- 8
4+
- 10
5+
- 12
6+
- 'stable'
7+
script:
8+
- npm run coverage

LICENSE

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2015 Alexander C. Mingoia
4+
Copyright (c) 2019-present Nick Baugh and Koajs contributors
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1819
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1920
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2021
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
22+
THE SOFTWARE.

README.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# @koa/router
1+
# [@koa/router](https://github.com/koajs/router)
22

3-
Router middleware for [koa](https://github.com/koajs/koa)
3+
> Router middleware for [Koa](https://github.com/koajs/koa).
44
55
[![NPM version](https://img.shields.io/npm/v/@koa/router.svg?style=flat)](https://npmjs.org/package/@koa/router)
66
[![NPM Downloads](https://img.shields.io/npm/dm/@koa/router.svg?style=flat)](https://npmjs.org/package/@koa/router)
@@ -20,18 +20,18 @@ Router middleware for [koa](https://github.com/koajs/koa)
2020
## Migrating to 7 / Koa 2
2121

2222
- The API has changed to match the new promise-based middleware
23-
signature of koa 2. See the
24-
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
23+
signature of koa 2. See the [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
2524
information.
2625
- Middleware is now always run in the order declared by `.use()` (or `.get()`,
2726
etc.), which matches Express 4 API.
2827

2928
## Installation
3029

31-
Install using [npm](https://www.npmjs.org/):
32-
33-
```sh
34-
npm install @koa/router
30+
```bash
31+
# npm ..
32+
npm i @koa/router
33+
# yarn ..
34+
yarn add @koa/router
3535
```
3636

3737
## [API Reference](./API.md)
@@ -40,10 +40,6 @@ npm install @koa/router
4040

4141
Please submit all issues and pull requests to the [koajs/router](http://github.com/koajs/router) repository!
4242

43-
## Tests
44-
45-
Run tests using `npm test`.
46-
4743
## Support
4844

4945
If you have any problem or suggestion please open an issue [here](https://github.com/koajs/router/issues).
@@ -54,3 +50,6 @@ This module is forked from the original [koa-router](https://github.com/ZijianHe
5450

5551
Thanks to the original authors @alexmingoia and the original team for their great work.
5652

53+
### License
54+
55+
[MIT](LICENSE)

lib/layer.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,23 @@ function Layer(path, methods, middleware, opts) {
2727

2828
for(let i = 0; i < methods.length; i++) {
2929
const l = this.methods.push(methods[i].toUpperCase());
30-
if (this.methods[l-1] === 'GET') {
31-
this.methods.unshift('HEAD');
32-
}
30+
if (this.methods[l-1] === 'GET') this.methods.unshift('HEAD');
3331
}
3432

3533
// ensure middleware is a function
3634
for (let i = 0; i < this.stack.length; i++) {
3735
const fn = this.stack[i];
3836
const type = (typeof fn);
39-
if (type !== 'function') {
37+
if (type !== 'function')
4038
throw new Error(
41-
methods.toString() + " `" + (this.opts.name || path) +"`: `middleware` "
42-
+ "must be a function, not `" + type + "`"
39+
`${methods.toString()} \`${this.opts.name || path}\`: \`middleware\` must be a function, not \`${type}\``
4340
);
44-
}
4541
}
4642

4743
this.path = path;
4844
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
4945

50-
debug('defined route %s %s', this.methods, this.opts.prefix + this.path);
46+
debug('defined route %s %s', this.methods, `${this.opts.prefix}${this.path}`);
5147
};
5248

5349
/**
@@ -94,8 +90,7 @@ Layer.prototype.params = function (path, captures, existingParams) {
9490
*/
9591

9692
Layer.prototype.captures = function (path) {
97-
if (this.opts.ignoreCaptures) return [];
98-
return path.match(this.regexp).slice(1);
93+
return this.opts.ignoreCaptures ? [] : path.match(this.regexp).slice(1);
9994
};
10095

10196
/**
@@ -220,11 +215,7 @@ Layer.prototype.param = function (param, fn) {
220215

221216
Layer.prototype.setPrefix = function (prefix) {
222217
if (this.path) {
223-
if (this.path !== '/' || this.opts.strict === true) {
224-
this.path = prefix + this.path;
225-
} else {
226-
this.path = prefix;
227-
}
218+
this.path = (this.path !== '/' || this.opts.strict === true) ? `${prefix}${this.path}` : prefix
228219
this.paramNames = [];
229220
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
230221
}

lib/router.js

+18-33
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ module.exports = Router;
4747
*/
4848

4949
function Router(opts) {
50-
if (!(this instanceof Router)) {
51-
return new Router(opts);
52-
}
50+
if (!(this instanceof Router)) return new Router(opts);
5351

5452
this.opts = opts || {};
5553
this.methods = this.opts.methods || [
@@ -253,13 +251,12 @@ Router.prototype.use = function () {
253251
const p = arrPaths[i];
254252
router.use.apply(router, [p].concat(middleware.slice(1)));
255253
}
254+
256255
return this;
257256
}
258257

259258
const hasPath = typeof middleware[0] === 'string';
260-
if (hasPath) {
261-
path = middleware.shift();
262-
}
259+
if (hasPath) path = middleware.shift();
263260

264261
for (let i = 0; i < middleware.length; i++) {
265262
const m = middleware[i];
@@ -440,12 +437,10 @@ Router.prototype.allowedMethods = function (options) {
440437

441438
if (!~implemented.indexOf(ctx.method)) {
442439
if (options.throw) {
443-
let notImplementedThrowable;
444-
if (typeof options.notImplemented === 'function') {
445-
notImplementedThrowable = options.notImplemented(); // set whatever the user returns from their function
446-
} else {
447-
notImplementedThrowable = new HttpError.NotImplemented();
448-
}
440+
let notImplementedThrowable = (typeof options.notImplemented === 'function')
441+
? options.notImplemented() // set whatever the user returns from their function
442+
: new HttpError.NotImplemented();
443+
449444
throw notImplementedThrowable;
450445
} else {
451446
ctx.status = 501;
@@ -458,12 +453,10 @@ Router.prototype.allowedMethods = function (options) {
458453
ctx.set('Allow', allowedArr.join(', '));
459454
} else if (!allowed[ctx.method]) {
460455
if (options.throw) {
461-
let notAllowedThrowable;
462-
if (typeof options.methodNotAllowed === 'function') {
463-
notAllowedThrowable = options.methodNotAllowed(); // set whatever the user returns from their function
464-
} else {
465-
notAllowedThrowable = new HttpError.MethodNotAllowed();
466-
}
456+
let notAllowedThrowable = (typeof options.methodNotAllowed === 'function')
457+
? options.methodNotAllowed() // set whatever the user returns from their function
458+
: new HttpError.MethodNotAllowed();
459+
467460
throw notAllowedThrowable;
468461
} else {
469462
ctx.status = 405;
@@ -496,9 +489,7 @@ Router.prototype.all = function (name, path, middleware) {
496489
name = null;
497490
}
498491

499-
this.register(path, methods, middleware, {
500-
name: name
501-
});
492+
this.register(path, methods, middleware, { name });
502493

503494
return this;
504495
};
@@ -529,14 +520,10 @@ Router.prototype.all = function (name, path, middleware) {
529520

530521
Router.prototype.redirect = function (source, destination, code) {
531522
// lookup source route by name
532-
if (source[0] !== '/') {
533-
source = this.url(source);
534-
}
523+
if (source[0] !== '/') source = this.url(source);
535524

536525
// lookup destination route by name
537-
if (destination[0] !== '/') {
538-
destination = this.url(destination);
539-
}
526+
if (destination[0] !== '/') destination = this.url(destination);
540527

541528
return this.all(source, ctx => {
542529
ctx.redirect(destination);
@@ -606,9 +593,7 @@ Router.prototype.route = function (name) {
606593
const routes = this.stack;
607594

608595
for (let len = routes.length, i=0; i<len; i++) {
609-
if (routes[i].name && routes[i].name === name) {
610-
return routes[i];
611-
}
596+
if (routes[i].name && routes[i].name === name) return routes[i];
612597
}
613598

614599
return false;
@@ -657,7 +642,7 @@ Router.prototype.url = function (name, params) {
657642
return route.url.apply(route, args);
658643
}
659644

660-
return new Error("No route found for name: " + name);
645+
return new Error(`No route found for name: ${name}`);
661646
};
662647

663648
/**
@@ -752,6 +737,6 @@ Router.prototype.param = function(param, middleware) {
752737
* @returns {String}
753738
*/
754739
Router.url = function (path) {
755-
const args = Array.prototype.slice.call(arguments, 1);
756-
return Layer.prototype.url.apply({ path: path }, args);
740+
const args = Array.prototype.slice.call(arguments, 1);
741+
return Layer.prototype.url.apply({ path }, args);
757742
};

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@koa/router",
33
"description": "Router middleware for koa. Provides RESTful resource routing.",
4-
"version": "9.0.0",
4+
"version": "8.0.8",
55
"author": "Alex Mingoia <talk@alexmingoia.com>",
66
"bugs": {
77
"url": "https://github.com/koajs/router/issues",
@@ -46,8 +46,8 @@
4646
},
4747
"scripts": {
4848
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
49-
"test": "NODE_ENV=test mocha test/**/*.js",
50-
"test:cov": "NODE_ENV=test nyc mocha test/**/*.js",
49+
"test": "mocha test/**/*.js",
50+
"coverage": "nyc npm run test",
5151
"bench": "make -C bench"
5252
}
5353
}

test/lib/layer.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,28 @@ describe('Layer', function() {
245245
);
246246
url.should.equal('/programming/how%20to%20node');
247247
});
248+
248249
it('setPrefix method checks Layer for path', function () {
249250
const route = new Layer('/category', ['get'], [function () {}], {name: 'books'});
250251
route.path = '/hunter2'
251252
const prefix = route.setPrefix('TEST')
252253
prefix.path.should.equal('TEST/hunter2')
253-
})
254+
});
255+
});
256+
257+
describe('Layer#prefix', () => {
258+
it('setPrefix method passes check Layer for path', function () {
259+
const route = new Layer('/category', ['get'], [function () {}], {name: 'books'});
260+
route.path = '/hunter2'
261+
const prefix = route.setPrefix('/TEST')
262+
prefix.path.should.equal('/TEST/hunter2')
263+
});
264+
265+
it('setPrefix method fails check Layer for path', function () {
266+
const route = new Layer(false, ['get'], [function () {}], {name: 'books'});
267+
route.path = false
268+
const prefix = route.setPrefix('/TEST')
269+
prefix.path.should.equal(false)
270+
});
254271
});
255-
});
272+
});

test/lib/router.js

+13
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,19 @@ describe('Router', function () {
779779

780780
});
781781

782+
it("allowedMethods check if flow (allowedArr.length)", function (done) {
783+
const app = new Koa();
784+
const router = new Router();
785+
app.use(router.routes());
786+
app.use(router.allowedMethods());
787+
788+
router.get('');
789+
790+
request(http.createServer(app.callback()))
791+
.get('/users')
792+
.end(() => done());
793+
});
794+
782795
it('supports custom routing detect path: ctx.routerPath', function (done) {
783796
const app = new Koa();
784797
const router = new Router();

0 commit comments

Comments
 (0)