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

feat: add routerPath to respond to routerName #491

Closed
wants to merge 15 commits into from
Closed
15 changes: 9 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
sudo: false
language: node_js
node_js:
- "6"
- "7"
- "8"
notifications:
email:
on_success: never
- '8'
- '10'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Alexander C. Mingoia
Copyright (c) 2019 eggjs

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 Down
148 changes: 58 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,34 @@
# koa-router
# @eggjs/router

[![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) [![Node.js Version](https://img.shields.io/node/v/koa-router.svg?style=flat)](http://nodejs.org/download/) [![Build Status](https://img.shields.io/travis/alexmingoia/koa-router.svg?style=flat)](http://travis-ci.org/alexmingoia/koa-router) [![Tips](https://img.shields.io/gratipay/alexmingoia.svg?style=flat)](https://www.gratipay.com/alexmingoia/) [![Gitter Chat](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg?style=flat)](https://gitter.im/alexmingoia/koa-router/)
Router core component for [Egg.js](https://github.com/eggjs).

> Router middleware for [koa](https://github.com/koajs/koa)
> **This repository is a fork of [koa-router](https://github.com/alexmingoia/koa-router).** with some additional features.

* Express-style routing using `app.get`, `app.put`, `app.post`, etc.
* Named URL parameters.
* Named routes with URL generation.
* Responds to `OPTIONS` requests with allowed methods.
* Support for `405 Method Not Allowed` and `501 Not Implemented`.
* Multiple route middleware.
* Multiple routers.
* Nestable routers.
* ES7 async/await support.

## 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
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
```
> And thanks for the greate work of @alexmingoia and the original team.

## API Reference
* [koa-router](#module_koa-router)
* [Router](#exp_module_koa-router--Router) ⏏
* [new Router([opts])](#new_module_koa-router--Router_new)

* [egg-router](#module_egg-router)
* [Router](#exp_module_egg-router--Router) ⏏
* [new Router([opts])](#new_module_egg-router--Router_new)
* _instance_
* [.get|put|post|patch|delete|del](#module_koa-router--Router+get|put|post|patch|delete|del) ⇒ <code>Router</code>
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code>
* [.use([path], middleware)](#module_koa-router--Router+use) ⇒ <code>Router</code>
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code>
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code>
* [.redirect(source, destination, [code])](#module_koa-router--Router+redirect) ⇒ <code>Router</code>
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Layer</code> &#124; <code>false</code>
* [.url(name, params, [options])](#module_koa-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code>
* [.get|put|post|patch|delete|del](#module_egg-router--Router+get|put|post|patch|delete|del) ⇒ <code>Router</code>
* [.routes](#module_egg-router--Router+routes) ⇒ <code>function</code>
* [.use([path], middleware)](#module_egg-router--Router+use) ⇒ <code>Router</code>
* [.prefix(prefix)](#module_egg-router--Router+prefix) ⇒ <code>Router</code>
* [.allowedMethods([options])](#module_egg-router--Router+allowedMethods) ⇒ <code>function</code>
* [.redirect(source, destination, [code])](#module_egg-router--Router+redirect) ⇒ <code>Router</code>
* [.route(name)](#module_egg-router--Router+route) ⇒ <code>Layer</code> &#124; <code>false</code>
* [.url(name, params, [options])](#module_egg-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>
* [.param(param, middleware)](#module_egg-router--Router+param) ⇒ <code>Router</code>
* _static_
* [.url(path, params)](#module_koa-router--Router.url) ⇒ <code>String</code>
* [.url(path, params)](#module_egg-router--Router.url) ⇒ <code>String</code>

<a name="exp_module_koa-router--Router"></a>
<a name="exp_module_egg-router--Router"></a>

### Router ⏏
**Kind**: Exported class
<a name="new_module_koa-router--Router_new"></a>
**Kind**: Exported class
<a name="new_module_egg-router--Router_new"></a>

#### new Router([opts])
Create a new router.
Expand All @@ -64,12 +39,12 @@ Create a new router.
| [opts] | <code>Object</code> | |
| [opts.prefix] | <code>String</code> | prefix router paths |

**Example**
**Example**
Basic usage:

```javascript
var Koa = require('koa');
var Router = require('koa-router');
var Router = require('@eggjs/router');

var app = new Koa();
var router = new Router();
Expand All @@ -82,7 +57,7 @@ app
.use(router.routes())
.use(router.allowedMethods());
```
<a name="module_koa-router--Router+get|put|post|patch|delete|del"></a>
<a name="module_egg-router--Router+get|put|post|patch|delete|del"></a>

#### router.get|put|post|patch|delete|del ⇒ <code>Router</code>
Create `router.verb()` methods, where *verb* is one of the HTTP verbs such
Expand Down Expand Up @@ -197,21 +172,21 @@ router.get('/:category/:title', (ctx, next) => {
The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
used to convert paths to regular expressions.

**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance property of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type | Description |
| --- | --- | --- |
| path | <code>String</code> | |
| [middleware] | <code>function</code> | route middleware(s) |
| callback | <code>function</code> | route callback |

<a name="module_koa-router--Router+routes"></a>
<a name="module_egg-router--Router+routes"></a>

#### router.routes ⇒ <code>function</code>
Returns router middleware which dispatches a route matching the request.

**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>
<a name="module_koa-router--Router+use"></a>
**Kind**: instance property of <code>[Router](#exp_module_egg-router--Router)</code>
<a name="module_egg-router--Router+use"></a>

#### router.use([path], middleware) ⇒ <code>Router</code>
Use given middleware.
Expand All @@ -220,15 +195,15 @@ Middleware run in the order they are defined by `.use()`. They are invoked
sequentially, requests start at the first middleware and work their way
"down" the middleware stack.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type |
| --- | --- |
| [path] | <code>String</code> |
| middleware | <code>function</code> |
| [...] | <code>function</code> |
| [path] | <code>String</code> |
| middleware | <code>function</code> |
| [...] | <code>function</code> |

**Example**
**Example**
```javascript
// session middleware will run before authorize
router
Expand All @@ -243,29 +218,29 @@ router.use(['/users', '/admin'], userAuth());

app.use(router.routes());
```
<a name="module_koa-router--Router+prefix"></a>
<a name="module_egg-router--Router+prefix"></a>

#### router.prefix(prefix) ⇒ <code>Router</code>
Set the path prefix for a Router instance that was already initialized.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type |
| --- | --- |
| prefix | <code>String</code> |
| prefix | <code>String</code> |

**Example**
**Example**
```javascript
router.prefix('/things/:thing_id')
```
<a name="module_koa-router--Router+allowedMethods"></a>
<a name="module_egg-router--Router+allowedMethods"></a>

#### router.allowedMethods([options]) ⇒ <code>function</code>
Returns separate middleware for responding to `OPTIONS` requests with
an `Allow` header containing the allowed methods, as well as responding
with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -274,10 +249,10 @@ with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
| [options.notImplemented] | <code>function</code> | throw the returned value in place of the default NotImplemented error |
| [options.methodNotAllowed] | <code>function</code> | throw the returned value in place of the default MethodNotAllowed error |

**Example**
**Example**
```javascript
var Koa = require('koa');
var Router = require('koa-router');
var Router = require('egg-router');

var app = new Koa();
var router = new Router();
Expand All @@ -290,7 +265,7 @@ app.use(router.allowedMethods());

```javascript
var Koa = require('koa');
var Router = require('koa-router');
var Router = require('egg-router');
var Boom = require('boom');

var app = new Koa();
Expand All @@ -303,7 +278,7 @@ app.use(router.allowedMethods({
methodNotAllowed: () => new Boom.methodNotAllowed()
}));
```
<a name="module_koa-router--Router+redirect"></a>
<a name="module_egg-router--Router+redirect"></a>

#### router.redirect(source, destination, [code]) ⇒ <code>Router</code>
Redirect `source` to `destination` URL with optional 30x status `code`.
Expand All @@ -323,31 +298,31 @@ router.all('/login', ctx => {
});
```

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type | Description |
| --- | --- | --- |
| source | <code>String</code> | URL or route name. |
| destination | <code>String</code> | URL or route name. |
| [code] | <code>Number</code> | HTTP status code (default: 301). |

<a name="module_koa-router--Router+route"></a>
<a name="module_egg-router--Router+route"></a>

#### router.route(name) ⇒ <code>Layer</code> &#124; <code>false</code>
Lookup route with given `name`.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type |
| --- | --- |
| name | <code>String</code> |
| name | <code>String</code> |

<a name="module_koa-router--Router+url"></a>
<a name="module_egg-router--Router+url"></a>

#### router.url(name, params, [options]) ⇒ <code>String</code> &#124; <code>Error</code>
Generate URL for route. Takes a route name and map of named `params`.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -356,7 +331,7 @@ Generate URL for route. Takes a route name and map of named `params`.
| [options] | <code>Object</code> | options parameter |
| [options.query] | <code>Object</code> &#124; <code>String</code> | query options |

**Example**
**Example**
```javascript
router.get('user', '/users/:id', (ctx, next) => {
// ...
Expand All @@ -379,20 +354,20 @@ router.url('user', { id: 3 }, { query: { limit: 1 } });
router.url('user', { id: 3 }, { query: "limit=1" });
// => "/users/3?limit=1"
```
<a name="module_koa-router--Router+param"></a>
<a name="module_egg-router--Router+param"></a>

#### router.param(param, middleware) ⇒ <code>Router</code>
Run middleware for named route parameters. Useful for auto-loading or
validation.

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type |
| --- | --- |
| param | <code>String</code> |
| middleware | <code>function</code> |
| param | <code>String</code> |
| middleware | <code>function</code> |

**Example**
**Example**
```javascript
router
.param('user', (id, ctx, next) => {
Expand All @@ -411,12 +386,12 @@ router
// /users/3 => {"id": 3, "name": "Alex"}
// /users/3/friends => [{"id": 4, "name": "TJ"}]
```
<a name="module_koa-router--Router.url"></a>
<a name="module_egg-router--Router.url"></a>

#### Router.url(path, params [, options]) ⇒ <code>String</code>
Generate URL from url pattern and given `params`.

**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: static method of <code>[Router](#exp_module_egg-router--Router)</code>

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -425,22 +400,15 @@ Generate URL from url pattern and given `params`.
| [options] | <code>Object</code> | options parameter |
| [options.query] | <code>Object</code> &#124; <code>String</code> | query options |

**Example**
**Example**
```javascript
var url = Router.url('/users/:id', {id: 1});
// => "/users/1"

const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
// => "/users/1?active=true"
```
## Contributing

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

## Tests

Run tests using `npm test`.

## Support

If you have any problem or suggestion please open an issue [here](https://github.com/alexmingoia/koa-router/issues).
15 changes: 15 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run test

build: off
Loading