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: support cjs and esm both #10

Merged
merged 13 commits into from
Jun 11, 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
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '8, 10, 12, 14, 16, 18, 20, 22'
version: '18.7.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ yarn.lock
!.env.test

.DS_Store

.tshy*
dist
File renamed without changes.
100 changes: 64 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,48 @@ Router core component for [Egg.js](https://github.com/eggjs).
- [router.param(param, middleware) ⇒ Router](#routerparamparam-middleware--router)
- [Router.url(path, params \[, options\]) ⇒ String](#routerurlpath-params--options--string)
- [Tests](#tests)
- [Breaking changes on v3](#breaking-changes-on-v3)
- [License](#license)

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

### Router ⏏

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

#### new Router([opts])
Create a new router.

Create a new router.

| Param | Type | Description |
| --- | --- | --- |
| --- | --- | --- |
| [opts] | <code>Object</code> | |
| [opts.prefix] | <code>String</code> | prefix router paths |

**Example**
Basic usage:

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

var app = new Koa();
var router = new Router();
const app = new Koa();
const router = new Router();

router.get('/', (ctx, next) => {
router.get('/', async (ctx, next) => {
// ctx.router available
});

app
.use(router.routes())
.use(router.allowedMethods());
```

<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
as `router.get()` or `router.post()`.

Expand All @@ -73,7 +77,7 @@ where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()

Additionaly, `router.all()` can be used to match against all methods.

```javascript
```ts
router
.get('/', (ctx, next) => {
ctx.body = 'Hello World!';
Expand Down Expand Up @@ -105,7 +109,7 @@ Query strings will not be considered when matching requests.
Routes can optionally have names. This allows generation of URLs and easy
renaming of URLs during development.

```javascript
```ts
router.get('user', '/users/:id', (ctx, next) => {
// ...
});
Expand All @@ -118,7 +122,7 @@ router.url('user', 3);

Multiple middleware may be given:

```javascript
```ts
router.get(
'/users/:id',
(ctx, next) => {
Expand All @@ -138,9 +142,9 @@ router.get(

Nesting routers is supported:

```javascript
var forums = new Router();
var posts = new Router();
```ts
const forums = new Router();
const posts = new Router();

posts.get('/', (ctx, next) => {...});
posts.get('/:pid', (ctx, next) => {...});
Expand All @@ -154,8 +158,8 @@ app.use(forums.routes());

Route paths can be prefixed at the router level:

```javascript
var router = new Router({
```ts
const router = new Router({
prefix: '/users'
});

Expand All @@ -167,7 +171,7 @@ router.get('/:id', ...); // responds to "/users/:id"

Named route parameters are captured and added to `ctx.params`.

```javascript
```ts
router.get('/:category/:title', (ctx, next) => {
console.log(ctx.params);
// => { category: 'programming', title: 'how-to-node' }
Expand All @@ -180,7 +184,7 @@ used to convert paths to regular expressions.
**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 |
Expand All @@ -194,6 +198,7 @@ Returns router middleware which dispatches a route matching the request.
<a name="module_egg-router--Router+use"></a>

#### router.use([path], middleware) ⇒ <code>Router</code>

Use given middleware.

Middleware run in the order they are defined by `.use()`. They are invoked
Expand All @@ -209,7 +214,8 @@ sequentially, requests start at the first middleware and work their way
| [...] | <code>function</code> |

**Example**
```javascript

```ts
// session middleware will run before authorize
router
.use(session())
Expand All @@ -223,9 +229,11 @@ router.use(['/users', '/admin'], userAuth());

app.use(router.routes());
```

<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_egg-router--Router)</code>
Expand All @@ -235,12 +243,15 @@ Set the path prefix for a Router instance that was already initialized.
| prefix | <code>String</code> |

**Example**
```javascript

```ts
router.prefix('/things/:thing_id')
```

<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.
Expand All @@ -255,26 +266,27 @@ with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
| [options.methodNotAllowed] | <code>function</code> | throw the returned value in place of the default MethodNotAllowed error |

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

var app = new Koa();
var router = new Router();
```ts
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';

const app = new Koa();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods());
```

**Example with [Boom](https://github.com/hapijs/boom)**

```javascript
var Koa = require('koa');
var Router = require('egg-router');
var Boom = require('boom');
```ts
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';
import Boom from 'boom';

var app = new Koa();
var router = new Router();
const app = new Koa();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods({
Expand All @@ -283,9 +295,11 @@ app.use(router.allowedMethods({
methodNotAllowed: () => new Boom.methodNotAllowed()
}));
```

<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`.

Both `source` and `destination` can be route names.
Expand All @@ -296,7 +310,7 @@ router.redirect('/login', 'sign-in');

This is equivalent to:

```javascript
```ts
router.all('/login', ctx => {
ctx.redirect('/sign-in');
ctx.status = 301;
Expand All @@ -314,6 +328,7 @@ router.all('/login', ctx => {
<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_egg-router--Router)</code>
Expand All @@ -325,6 +340,7 @@ Lookup route with given `name`.
<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_egg-router--Router)</code>
Expand All @@ -337,7 +353,8 @@ Generate URL for route. Takes a route name and map of named `params`.
| [options.query] | <code>Object</code> &#124; <code>String</code> | query options |

**Example**
```javascript

```ts
router.get('user', '/users/:id', (ctx, next) => {
// ...
});
Expand All @@ -359,9 +376,11 @@ router.url('user', { id: 3 }, { query: { limit: 1 } });
router.url('user', { id: 3 }, { query: "limit=1" });
// => "/users/3?limit=1"
```

<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.

Expand All @@ -373,7 +392,8 @@ validation.
| middleware | <code>function</code> |

**Example**
```javascript

```ts
router
.param('user', (id, ctx, next) => {
ctx.user = users[id];
Expand All @@ -391,9 +411,11 @@ router
// /users/3 => {"id": 3, "name": "Alex"}
// /users/3/friends => [{"id": 4, "name": "TJ"}]
```

<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_egg-router--Router)</code>
Expand All @@ -406,8 +428,9 @@ Generate URL from url pattern and given `params`.
| [options.query] | <code>Object</code> &#124; <code>String</code> | query options |

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

```ts
const url = Router.url('/users/:id', {id: 1});
// => "/users/1"

const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
Expand All @@ -418,6 +441,11 @@ const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});

Run tests using `npm test`.

## Breaking changes on v3

- Drop generator function support
- Drop Node.js < 18.7.0 support

## License

[MIT](LICENSE)
2 changes: 1 addition & 1 deletion bench/run
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export PORT=3333

host="http://localhost:$PORT"

node "$(dirname $0)/server.js" &
node "$(dirname $0)/server.cjs" &

pid=$!

Expand Down
13 changes: 6 additions & 7 deletions bench/server.js → bench/server.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
/* eslint-disable @typescript-eslint/no-var-requires */
const { Application } = require('@eggjs/koa');
const { Router } = require('../dist/commonjs');

const Koa = require('koa');
const Router = require('../');

const app = new Koa();
const app = new Application();
const router = new Router();

const ok = ctx => {
Expand Down Expand Up @@ -36,8 +35,8 @@ for (let i = n; i > 0; i--) {
const child = new Router();
if (useMiddleware) child.use((ctx, next) => next());
child.get(`/:${''.padStart(i, 'a')}`, ok);
child.nest('/grandchild', grandchild);
router.nest(`/${i}/child`, child);
// child.use('/grandchild', grandchild);
// router.use(`/${i}/child`, child);
}

if (process.env.DEBUG) {
Expand Down
10 changes: 0 additions & 10 deletions index.js

This file was deleted.

Loading
Loading