Skip to content

Commit 2c6ce82

Browse files
3imed-jaberihaoxins
authored andcommitted
Fix and Update πŸš€ ! (#26)
* add LICENSE πŸ” .. * update README.md πŸ“‹ .. * update .gitignore file 🐞 .. * add eslintrc(s) ☒️ .. * add eslint to pkg.json πŸ’„ .. * run lint --eslint πŸ’…πŸ» .. * ES6+: add string template over '' πŸ“Œ .. * add test coverage πŸ“¦ .. * update some depen. πŸͺ.. * update the CI pipeline 🎲 .. * update pkg --pathToRegExp πŸš€ .. * better stuctrue πŸŽ— ..
1 parent fad904d commit 2c6ce82

10 files changed

+219
-147
lines changed

β€Ž.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
extends: standard

β€Ž.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1+
# OS #
2+
###################
3+
.DS_Store
4+
.idea
5+
Thumbs.db
6+
tmp/
7+
temp/
8+
9+
10+
# Node.js #
11+
###################
112
node_modules
13+
package-lock.json
14+
npm-debug.log
15+
yarn-debug.log
16+
yarn-error.log
17+
18+
19+
# NYC #
20+
###################
21+
coverage
22+
*.lcov
23+
.nyc_output

β€Ž.travis.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
node_js:
2-
- 8
31
language: node_js
2+
node_js:
3+
- 8
4+
- 10
5+
- 12
6+
- 'lts/*'
7+
- 'node'
8+
script:
9+
- npm run test-cov
10+
after_script:
11+
- npm i coveralls
12+
- cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls

β€ŽLICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Koa contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

β€ŽReadme.md

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

2-
# koa-rewrite
3-
4-
URL rewrite middleware for koa.
3+
> URL rewrite middleware for koa.
54
65
___Notice: `koa-rewrite@2+` supports `koa@2`, if you want to use this module with `koa@1`, please use `koa-rewrite@1`.___
76

7+
88
## Installation
99

1010
```js
1111
$ npm install koa-rewrite
1212
```
1313

14+
1415
## Examples
1516

16-
Rewrite using a regular expression, rewriting
17-
`/i123` to `/items/123`.
17+
Rewrite using a regular expression, rewriting `/i123` to `/items/123`.
1818

1919
```js
2020
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
2121
```
2222

23-
Rewrite using route parameters, references may be named
24-
or numeric. For example rewrite `/foo..bar` to `/commits/foo/to/bar`:
23+
Rewrite using route parameters, references may be named or numeric. For example rewrite `/foo..bar` to `/commits/foo/to/bar`:
2524

2625
```js
2726
app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
2827
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
2928
```
3029

31-
You may also use the wildcard `*` to soak up several segments,
32-
for example `/js/vendor/jquery.js` would become `/public/assets/js/vendor/jquery.js`:
30+
You may also use the wildcard `*` to soak up several segments, for example `/js/vendor/jquery.js` would become `/public/assets/js/vendor/jquery.js`:
3331

3432
```js
3533
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));
3634
```
3735

36+
3837
## Debugging
3938

40-
Use the __DEBUG__ environment variable with "koa-rewrite":
39+
Use the __DEBUG__ environment variable with "koa-rewrite":
4140

4241
```
4342
koa-rewrite rewrite /i123 -> /items/123 +762ms
4443
koa-rewrite rewrite /i321 -> /items/321 +9s
4544
koa-rewrite rewrite /i123 -> /items/123 +5s
4645
```
4746

47+
4848
## License
4949

50-
MIT
50+
[MIT](/LICENSE)

β€Žexample.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
'use strict';
1+
'use strict'
22

3-
const rewrite = require('./');
4-
const Koa = require('koa');
3+
const rewrite = require('./')
4+
const Koa = require('koa')
55

6-
const app = new Koa();
6+
const app = new Koa()
77

88
// GET /i124
9-
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
9+
app.use(rewrite(/^\/i(\w+)/, '/items/$1'))
1010

1111
// GET /foo..bar
12-
//app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
13-
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
12+
// app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
13+
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'))
1414

1515
// GET /js/jquery.js
16-
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));
16+
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'))
1717

18-
app.use(function(ctx) {
19-
ctx.body = ctx.url + '\n';
20-
});
18+
app.use(function (ctx) {
19+
ctx.body = `${ctx.url}\n`
20+
})
2121

2222
app.listen(3000, () => {
23-
console.log('listening on port 3000');
24-
});
23+
console.log('listening on port 3000')
24+
})

β€Žindex.js

+24-30
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
'use strict';
1+
'use strict'
22
/**
33
* Module dependencies.
44
*/
55

6-
const debug = require('debug')('koa-rewrite');
7-
const toRegexp = require('path-to-regexp');
8-
9-
/**
10-
* Expose `expose`.
11-
*/
12-
13-
module.exports = rewrite;
6+
const debug = require('debug')('koa-rewrite')
7+
const { pathToRegexp } = require('path-to-regexp')
148

159
/**
1610
* Rwrite `src` to `dst`.
@@ -21,31 +15,31 @@ module.exports = rewrite;
2115
* @api public
2216
*/
2317

24-
function rewrite(src, dst) {
25-
const keys = [];
26-
const re = toRegexp(src, keys);
27-
const map = toMap(keys);
18+
module.exports = function rewrite (src, dst) {
19+
const keys = []
20+
const re = pathToRegexp(src, keys)
21+
const map = toMap(keys)
2822

29-
debug('rewrite %s -> %s %s', src, dst, re);
23+
debug('rewrite %s -> %s %s', src, dst, re)
3024

31-
return function(ctx, next) {
32-
const orig = ctx.url;
33-
const m = re.exec(orig);
25+
return function (ctx, next) {
26+
const orig = ctx.url
27+
const m = re.exec(orig)
3428

3529
if (m) {
3630
ctx.url = dst.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => {
37-
if (name) return m[map[name].index + 1] || '';
38-
return m[n] || '';
39-
});
31+
if (name) return m[map[name].index + 1] || ''
32+
return m[n] || ''
33+
})
4034

41-
debug('rewrite %s -> %s', orig, ctx.url);
35+
debug('rewrite %s -> %s', orig, ctx.url)
4236

4337
return next().then(() => {
44-
ctx.url = orig;
45-
});
38+
ctx.url = orig
39+
})
4640
}
4741

48-
return next();
42+
return next()
4943
}
5044
}
5145

@@ -57,13 +51,13 @@ function rewrite(src, dst) {
5751
* @api private
5852
*/
5953

60-
function toMap(params) {
61-
const map = {};
54+
function toMap (params) {
55+
const map = {}
6256

6357
params.forEach((param, i) => {
64-
param.index = i;
65-
map[param.name] = param;
66-
});
58+
param.index = i
59+
map[param.name] = param
60+
})
6761

68-
return map;
62+
return map
6963
}

β€Žpackage.json

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
2-
"name": "koa-rewrite",
3-
"description": "URL rewrite middleware for koa",
4-
"repository": "koajs/rewrite",
2+
"name": "koa-rewrite",
53
"version": "3.0.1",
4+
"description": "URL rewrite middleware for koa",
5+
"main": "index.js",
66
"scripts": {
7-
"test": "mocha test/test"
7+
"lint": "eslint --fix .",
8+
"test-only": "mocha --reporter spec test/test.js --exit",
9+
"test": "npm run lint & npm run test-only",
10+
"test-cov": "nyc npm run test"
811
},
912
"keywords": [
1013
"koa",
@@ -13,20 +16,39 @@
1316
"redirect",
1417
"url"
1518
],
16-
"files": [
17-
"index.js"
18-
],
19-
"devDependencies": {
20-
"koa": "^2.3.0",
21-
"mocha": "^4.0.1",
22-
"supertest": "^3.0.0"
19+
"repository": {
20+
"type": "git",
21+
"url": "git://github.com/koajs/rewrite.git"
2322
},
2423
"license": "MIT",
24+
"nyc": {
25+
"reporter": [
26+
"lcov",
27+
"text-summary"
28+
],
29+
"report-dir": "./coverage"
30+
},
2531
"dependencies": {
26-
"debug": "^3.1.0",
27-
"path-to-regexp": "^2.1.0"
32+
"debug": "^4.1.1",
33+
"path-to-regexp": "^6.1.0"
34+
},
35+
"devDependencies": {
36+
"eslint": "^6.8.0",
37+
"eslint-config-standard": "^14.1.0",
38+
"eslint-plugin-import": "^2.20.0",
39+
"eslint-plugin-node": "^11.0.0",
40+
"eslint-plugin-promise": "^4.2.1",
41+
"eslint-plugin-standard": "^4.0.1",
42+
"koa": "^2.11.0",
43+
"mocha": "^7.0.1",
44+
"nyc": "^15.0.0",
45+
"supertest": "^4.0.2"
2846
},
2947
"engines": {
30-
"node": ">= 4"
31-
}
32-
}
48+
"node": ">= 8"
49+
},
50+
"bugs": {
51+
"url": "https://github.com/koajs/rewrite/issues"
52+
},
53+
"homepage": "https://github.com/koajs/rewrite"
54+
}

β€Žtest/.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
env:
3+
mocha: true

0 commit comments

Comments
Β (0)