Skip to content

Commit

Permalink
To v9.x.x 🚀 (#87)
Browse files Browse the repository at this point in the history
* 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 🎗 ..
  • Loading branch information
3imed-jaberi authored May 30, 2020
1 parent 53a6e0d commit e3d5870
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 112 deletions.
52 changes: 15 additions & 37 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript
# OS #
###################
.DS_Store
.idea
Thumbs.db
tmp/
temp/

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

# Dependency directories
# Node.js #
###################
node_modules
jspm_packages

package-lock.json
yarn.lock
*.log

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
.env*
!.env.test

.DS_Store
# NYC #
###################
coverage
*.lcov
.nyc_output
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock=false
package-lock=false
14 changes: 6 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: node_js
node_js:
- "8"
- "9"
- "10"
- "11"
- "12"
notifications:
email:
on_success: never
- 8
- 10
- 12
- 'stable'
script:
- npm run coverage
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @koa/router
# [@koa/router](https://github.com/koajs/router)

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

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

## Installation

Install using [npm](https://www.npmjs.org/):

```sh
npm install @koa/router
```bash
# npm ..
npm i @koa/router
# yarn ..
yarn add @koa/router
```

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

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

## Tests

Run tests using `npm test`.

## Support

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

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

### License

[MIT](LICENSE)
21 changes: 6 additions & 15 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,23 @@ function Layer(path, methods, middleware, opts) {

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

// ensure middleware is a function
for (let i = 0; i < this.stack.length; i++) {
const fn = this.stack[i];
const type = (typeof fn);
if (type !== 'function') {
if (type !== 'function')
throw new Error(
methods.toString() + " `" + (this.opts.name || path) +"`: `middleware` "
+ "must be a function, not `" + type + "`"
`${methods.toString()} \`${this.opts.name || path}\`: \`middleware\` must be a function, not \`${type}\``
);
}
}

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

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

/**
Expand Down Expand Up @@ -94,8 +90,7 @@ Layer.prototype.params = function (path, captures, existingParams) {
*/

Layer.prototype.captures = function (path) {
if (this.opts.ignoreCaptures) return [];
return path.match(this.regexp).slice(1);
return this.opts.ignoreCaptures ? [] : path.match(this.regexp).slice(1);
};

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

Layer.prototype.setPrefix = function (prefix) {
if (this.path) {
if (this.path !== '/' || this.opts.strict === true) {
this.path = prefix + this.path;
} else {
this.path = prefix;
}
this.path = (this.path !== '/' || this.opts.strict === true) ? `${prefix}${this.path}` : prefix
this.paramNames = [];
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
}
Expand Down
51 changes: 18 additions & 33 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ module.exports = Router;
*/

function Router(opts) {
if (!(this instanceof Router)) {
return new Router(opts);
}
if (!(this instanceof Router)) return new Router(opts);

this.opts = opts || {};
this.methods = this.opts.methods || [
Expand Down Expand Up @@ -253,13 +251,12 @@ Router.prototype.use = function () {
const p = arrPaths[i];
router.use.apply(router, [p].concat(middleware.slice(1)));
}

return this;
}

const hasPath = typeof middleware[0] === 'string';
if (hasPath) {
path = middleware.shift();
}
if (hasPath) path = middleware.shift();

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

if (!~implemented.indexOf(ctx.method)) {
if (options.throw) {
let notImplementedThrowable;
if (typeof options.notImplemented === 'function') {
notImplementedThrowable = options.notImplemented(); // set whatever the user returns from their function
} else {
notImplementedThrowable = new HttpError.NotImplemented();
}
let notImplementedThrowable = (typeof options.notImplemented === 'function')
? options.notImplemented() // set whatever the user returns from their function
: new HttpError.NotImplemented();

throw notImplementedThrowable;
} else {
ctx.status = 501;
Expand All @@ -458,12 +453,10 @@ Router.prototype.allowedMethods = function (options) {
ctx.set('Allow', allowedArr.join(', '));
} else if (!allowed[ctx.method]) {
if (options.throw) {
let notAllowedThrowable;
if (typeof options.methodNotAllowed === 'function') {
notAllowedThrowable = options.methodNotAllowed(); // set whatever the user returns from their function
} else {
notAllowedThrowable = new HttpError.MethodNotAllowed();
}
let notAllowedThrowable = (typeof options.methodNotAllowed === 'function')
? options.methodNotAllowed() // set whatever the user returns from their function
: new HttpError.MethodNotAllowed();

throw notAllowedThrowable;
} else {
ctx.status = 405;
Expand Down Expand Up @@ -496,9 +489,7 @@ Router.prototype.all = function (name, path, middleware) {
name = null;
}

this.register(path, methods, middleware, {
name: name
});
this.register(path, methods, middleware, { name });

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

Router.prototype.redirect = function (source, destination, code) {
// lookup source route by name
if (source[0] !== '/') {
source = this.url(source);
}
if (source[0] !== '/') source = this.url(source);

// lookup destination route by name
if (destination[0] !== '/') {
destination = this.url(destination);
}
if (destination[0] !== '/') destination = this.url(destination);

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

for (let len = routes.length, i=0; i<len; i++) {
if (routes[i].name && routes[i].name === name) {
return routes[i];
}
if (routes[i].name && routes[i].name === name) return routes[i];
}

return false;
Expand Down Expand Up @@ -657,7 +642,7 @@ Router.prototype.url = function (name, params) {
return route.url.apply(route, args);
}

return new Error("No route found for name: " + name);
return new Error(`No route found for name: ${name}`);
};

/**
Expand Down Expand Up @@ -752,6 +737,6 @@ Router.prototype.param = function(param, middleware) {
* @returns {String}
*/
Router.url = function (path) {
const args = Array.prototype.slice.call(arguments, 1);
return Layer.prototype.url.apply({ path: path }, args);
const args = Array.prototype.slice.call(arguments, 1);
return Layer.prototype.url.apply({ path }, args);
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koa/router",
"description": "Router middleware for koa. Provides RESTful resource routing.",
"version": "9.0.0",
"version": "8.0.8",
"author": "Alex Mingoia <talk@alexmingoia.com>",
"bugs": {
"url": "https://github.com/koajs/router/issues",
Expand Down Expand Up @@ -46,8 +46,8 @@
},
"scripts": {
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
"test": "NODE_ENV=test mocha test/**/*.js",
"test:cov": "NODE_ENV=test nyc mocha test/**/*.js",
"test": "mocha test/**/*.js",
"coverage": "nyc npm run test",
"bench": "make -C bench"
}
}
21 changes: 19 additions & 2 deletions test/lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,28 @@ describe('Layer', function() {
);
url.should.equal('/programming/how%20to%20node');
});

it('setPrefix method checks Layer for path', function () {
const route = new Layer('/category', ['get'], [function () {}], {name: 'books'});
route.path = '/hunter2'
const prefix = route.setPrefix('TEST')
prefix.path.should.equal('TEST/hunter2')
})
});
});

describe('Layer#prefix', () => {
it('setPrefix method passes check Layer for path', function () {
const route = new Layer('/category', ['get'], [function () {}], {name: 'books'});
route.path = '/hunter2'
const prefix = route.setPrefix('/TEST')
prefix.path.should.equal('/TEST/hunter2')
});

it('setPrefix method fails check Layer for path', function () {
const route = new Layer(false, ['get'], [function () {}], {name: 'books'});
route.path = false
const prefix = route.setPrefix('/TEST')
prefix.path.should.equal(false)
});
});
});
});
13 changes: 13 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,19 @@ describe('Router', function () {

});

it("allowedMethods check if flow (allowedArr.length)", function (done) {
const app = new Koa();
const router = new Router();
app.use(router.routes());
app.use(router.allowedMethods());

router.get('');

request(http.createServer(app.callback()))
.get('/users')
.end(() => done());
});

it('supports custom routing detect path: ctx.routerPath', function (done) {
const app = new Koa();
const router = new Router();
Expand Down

0 comments on commit e3d5870

Please sign in to comment.