Skip to content

Commit

Permalink
add support for koa v2
Browse files Browse the repository at this point in the history
Use const

Update for Koa 2

Use strict

Update example
  • Loading branch information
macalinao authored and tejasmanohar committed Nov 19, 2015
1 parent c749de7 commit e18e1df
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_js:
- "0.11"
- "4"
- "5"
language: node_js
16 changes: 9 additions & 7 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

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

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

// GET /i124
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
Expand All @@ -14,9 +15,10 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
// GET /js/jquery.js
app.use(rewrite('/js/*', '/public/assets/js/$1'));

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

app.listen(3000);
console.log('listening on port 3000');
app.listen(3000, () => {
console.log('listening on port 3000');
});
37 changes: 18 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

'use strict';
/**
* Module dependencies.
*/

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

/**
* Expose `expose`.
Expand All @@ -22,31 +22,30 @@ module.exports = rewrite;
*/

function rewrite(src, dst) {
var keys = [];
var re = toRegexp(src, keys);
var map = toMap(keys);
const keys = [];
const re = toRegexp(src, keys);
const map = toMap(keys);

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

return function*(next){
var orig = this.path;
var m = re.exec(orig);
return function(ctx, next) {
const orig = ctx.path;
const m = re.exec(orig);

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

debug('rewrite %s -> %s', orig, this.path);

yield* next;
debug('rewrite %s -> %s', orig, ctx.path);

this.path = orig;
return;
return next().then(function() {
ctx.path = orig;
});
}

yield* next;
return next();
}
}

Expand All @@ -59,12 +58,12 @@ function rewrite(src, dst) {
*/

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

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

return map;
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"repository": "koajs/rewrite",
"version": "1.1.0",
"scripts": {
"test": "./node_modules/mocha/bin/mocha --harmony test/test"
"test": "mocha test/test"
},
"keywords": [
"koa",
Expand All @@ -17,13 +17,16 @@
"index.js"
],
"devDependencies": {
"koa": "~0.2.1",
"koa": "^2.0.0-alpha.3",
"mocha": "^2.2.4",
"supertest": "^0.15.0"
},
"license": "MIT",
"dependencies": {
"debug": "*",
"path-to-regexp": "0.0.2"
},
"engines": {
"node": ">= 4"
}
}
67 changes: 30 additions & 37 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
var request = require('supertest');
var koa = require('koa');
var rewrite = require('..');
'use strict';

describe('koa-rewrite', function () {
const request = require('supertest');
const Koa = require('koa');
const rewrite = require('..');

function differentPathHelper(ctx, next) {
const orig = ctx.path;
return next().then(() => {
if (orig !== ctx.path) ctx.throw(ctx.path + ' not equal to original path ' + orig);
});
}

describe('new Koa-rewrite', function () {
it('rewrite /^\/i(\w+)/ -> /items/$1', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
app.use(function* () {
this.body = this.path;
app.use(function(ctx) {
ctx.body = ctx.path;
});

request(app.callback())
Expand All @@ -21,15 +26,11 @@ describe('koa-rewrite', function () {
});

it('rewrite /:src..:dst -> /commits/$1/to/$2', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(function* () {
this.body = this.path;
app.use(function(ctx) {
ctx.body = ctx.path;
});

request(app.callback())
Expand All @@ -38,15 +39,11 @@ describe('koa-rewrite', function () {
});

it('rewrite /:src..:dst -> /commits/:src/to/:dst', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
app.use(function* () {
this.body = this.path;
app.use(function(ctx) {
ctx.body = ctx.path;
});

request(app.callback())
Expand All @@ -55,19 +52,15 @@ describe('koa-rewrite', function () {
});

it('rewrite /js/* -> /public/assets/js/$1', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
const app = new Koa();
app.use(differentPathHelper);
app.use(rewrite('/js/*', '/public/assets/js/$1'));
app.use(function* () {
this.body = this.path;
app.use(function(ctx) {
ctx.body = ctx.path;
});

request(app.callback())
.get('/js/jquery.js')
.expect('/public/assets/js/jquery.js', done);
});
});
});

0 comments on commit e18e1df

Please sign in to comment.