Skip to content

Commit

Permalink
Fix and Update πŸš€ ! (#26)
Browse files Browse the repository at this point in the history
*  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 πŸŽ— ..
  • Loading branch information
3imed-jaberi authored and haoxins committed Jan 28, 2020
1 parent fad904d commit 2c6ce82
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 147 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
extends: standard
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# OS #
###################
.DS_Store
.idea
Thumbs.db
tmp/
temp/


# Node.js #
###################
node_modules
package-lock.json
npm-debug.log
yarn-debug.log
yarn-error.log


# NYC #
###################
coverage
*.lcov
.nyc_output
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
node_js:
- 8
language: node_js
node_js:
- 8
- 10
- 12
- 'lts/*'
- 'node'
script:
- npm run test-cov
after_script:
- npm i coveralls
- cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Koa 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
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.
22 changes: 11 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
# [**koa-rewrite**](https://github.com/koajs/rewrite)

# koa-rewrite

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


## Installation

```js
$ npm install koa-rewrite
```


## Examples

Rewrite using a regular expression, rewriting
`/i123` to `/items/123`.
Rewrite using a regular expression, rewriting `/i123` to `/items/123`.

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

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

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

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

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


## Debugging

Use the __DEBUG__ environment variable with "koa-rewrite":
Use the __DEBUG__ environment variable with "koa-rewrite":

```
koa-rewrite rewrite /i123 -> /items/123 +762ms
koa-rewrite rewrite /i321 -> /items/321 +9s
koa-rewrite rewrite /i123 -> /items/123 +5s
```


## License

MIT
[MIT](/LICENSE)
26 changes: 13 additions & 13 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'use strict';
'use strict'

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

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

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

// GET /foo..bar
//app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
// app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'))

// GET /js/jquery.js
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'))

app.use(function(ctx) {
ctx.body = ctx.url + '\n';
});
app.use(function (ctx) {
ctx.body = `${ctx.url}\n`
})

app.listen(3000, () => {
console.log('listening on port 3000');
});
console.log('listening on port 3000')
})
54 changes: 24 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
'use strict';
'use strict'
/**
* Module dependencies.
*/

const debug = require('debug')('koa-rewrite');
const toRegexp = require('path-to-regexp');

/**
* Expose `expose`.
*/

module.exports = rewrite;
const debug = require('debug')('koa-rewrite')
const { pathToRegexp } = require('path-to-regexp')

/**
* Rwrite `src` to `dst`.
Expand All @@ -21,31 +15,31 @@ module.exports = rewrite;
* @api public
*/

function rewrite(src, dst) {
const keys = [];
const re = toRegexp(src, keys);
const map = toMap(keys);
module.exports = function rewrite (src, dst) {
const keys = []
const re = pathToRegexp(src, keys)
const map = toMap(keys)

debug('rewrite %s -> %s %s', src, dst, re);
debug('rewrite %s -> %s %s', src, dst, re)

return function(ctx, next) {
const orig = ctx.url;
const m = re.exec(orig);
return function (ctx, next) {
const orig = ctx.url
const m = re.exec(orig)

if (m) {
ctx.url = dst.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => {
if (name) return m[map[name].index + 1] || '';
return m[n] || '';
});
if (name) return m[map[name].index + 1] || ''
return m[n] || ''
})

debug('rewrite %s -> %s', orig, ctx.url);
debug('rewrite %s -> %s', orig, ctx.url)

return next().then(() => {
ctx.url = orig;
});
ctx.url = orig
})
}

return next();
return next()
}
}

Expand All @@ -57,13 +51,13 @@ function rewrite(src, dst) {
* @api private
*/

function toMap(params) {
const map = {};
function toMap (params) {
const map = {}

params.forEach((param, i) => {
param.index = i;
map[param.name] = param;
});
param.index = i
map[param.name] = param
})

return map;
return map
}
54 changes: 38 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "koa-rewrite",
"description": "URL rewrite middleware for koa",
"repository": "koajs/rewrite",
"name": "koa-rewrite",
"version": "3.0.1",
"description": "URL rewrite middleware for koa",
"main": "index.js",
"scripts": {
"test": "mocha test/test"
"lint": "eslint --fix .",
"test-only": "mocha --reporter spec test/test.js --exit",
"test": "npm run lint & npm run test-only",
"test-cov": "nyc npm run test"
},
"keywords": [
"koa",
Expand All @@ -13,20 +16,39 @@
"redirect",
"url"
],
"files": [
"index.js"
],
"devDependencies": {
"koa": "^2.3.0",
"mocha": "^4.0.1",
"supertest": "^3.0.0"
"repository": {
"type": "git",
"url": "git://github.com/koajs/rewrite.git"
},
"license": "MIT",
"nyc": {
"reporter": [
"lcov",
"text-summary"
],
"report-dir": "./coverage"
},
"dependencies": {
"debug": "^3.1.0",
"path-to-regexp": "^2.1.0"
"debug": "^4.1.1",
"path-to-regexp": "^6.1.0"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"koa": "^2.11.0",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"supertest": "^4.0.2"
},
"engines": {
"node": ">= 4"
}
}
"node": ">= 8"
},
"bugs": {
"url": "https://github.com/koajs/rewrite/issues"
},
"homepage": "https://github.com/koajs/rewrite"
}
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
env:
mocha: true
Loading

0 comments on commit 2c6ce82

Please sign in to comment.