Skip to content

Commit

Permalink
support more router define
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 11, 2017
1 parent 4e08de7 commit 9f200a5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 32 deletions.
23 changes: 18 additions & 5 deletions Readme.md
Expand Up @@ -62,11 +62,24 @@ parser.add('check-version', (version) => {
};
});

const router = parser.parse(['[GET,POST] [/user/favorites/:id,/user/:id] [check-version(1) & getUser & getFavorites]', {
methods: ['GET'],
urls: ['/user/me'],
handlers: ['getUser'],
});
const router = parser.parse([
'[GET,POST] [/user/favorites/:id,/user/:id] [check-version(1) & getUser & getFavorites]',
{
methods: ['GET'],
urls: ['/user/me'],
handlers: ['getUser'],
},
[
['GET', 'POST'],
['/user/favorites/:id', '/user/:id'],
[
(ctx, next) => next(),
'check-version(1)',
'getUser',
'getFavorites',
],
],
]);
const app = new Koa();
app.use(router.routes());
request(app.listen())
Expand Down
23 changes: 20 additions & 3 deletions lib/parser.js
Expand Up @@ -47,18 +47,35 @@ function covert(str) {
if (!str) {
throw new Error('desc can not be null');
}
if (isObject(str)) {
if (Array.isArray(str)) {
const descArr = str;
if (descArr.length !== 3) {
throw new Error(`${descArr.join(' ')} is invalid`);
}
const cut = (tmp) => {
if (isString(tmp)) {
return tmp.substring(1, tmp.length - 1);
}
return tmp;
};
return {
methods: convertMethods(cut(descArr[0])),
urls: convertUrls(cut(descArr[1])),
handlers: convertHandlers(cut(descArr[2])),
};
}
if (!Array.isArray(str) && isObject(str)) {
const item = str;
return {
methods: convertMethods(item.methods),
urls: convertUrls(item.urls),
handlers: convertHandlers(item.handlers),
};
}
const reg = /\] \[/g;
const reg = /\]\s+\[/g;
const arr = str.split(reg);
if (arr.length !== 3) {
throw new Error('desc is invalid');
throw new Error(`${str} is invalid`);
}
return {
methods: convertMethods(arr[0].substring(1)),
Expand Down
105 changes: 81 additions & 24 deletions test/parser.js
Expand Up @@ -13,8 +13,7 @@ describe('koa-router-parser', () => {
});

parser.add('getUser', (ctx, next) => {
const id = ctx.params.id;
const getUser = new Promise(resolve => {
const getUser = new Promise((resolve) => {
setTimeout(() => {
ctx.user = {
name: 'vicanso'
Expand All @@ -26,8 +25,7 @@ describe('koa-router-parser', () => {
});

parser.add('getFavorites', (ctx) => {
const user = ctx.user;
const getFavorites = new Promise(resolve => {
const getFavorites = new Promise((resolve) => {
setTimeout(() => {
ctx.body = [{
name: 'javascript'
Expand All @@ -42,7 +40,7 @@ describe('koa-router-parser', () => {

parser.add('check-version', (version) => {
return (ctx, next) => {
const v = parseInt(ctx.query.version);
const v = parseInt(ctx.query.version, 10);
if (v === version) {
return next();
}
Expand All @@ -62,7 +60,7 @@ describe('koa-router-parser', () => {
parser.add('check-options', (options, version) => {
return (ctx, next) => {
const v = parseInt(ctx.query.version);
if (v == version && ctx.query.tag === options.tag) {
if (v === version && ctx.query.tag === options.tag) {
return next();
}
throw new Error('check options fail');
Expand Down Expand Up @@ -90,7 +88,7 @@ describe('koa-router-parser', () => {
done();
});

it('should throw error when parse desc is null', done => {
it('should throw error when parse desc is null', (done) => {
try {
parser.parse();
} catch (err) {
Expand All @@ -99,16 +97,16 @@ describe('koa-router-parser', () => {
}
});

it('should throw error when parse desc is invalid', done => {
it('should throw error when parse desc is invalid', (done) => {
try {
parser.parse('[GET] [/]');
} catch (err) {
assert.equal(err.message, 'desc is invalid');
assert.equal(err.message, '[GET] [/] is invalid');
done();
}
});

it('should parse router successful', done => {
it('should parse router successful', (done) => {

const router = parser.parse('[GET] [/user] [user]');
const app = new Koa();
Expand All @@ -118,21 +116,23 @@ describe('koa-router-parser', () => {
.get('/user')
.end((err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');
done();
});
});

it('should parse get,post router successful', done => {
it('should parse get,post router successful', (done) => {
const router = parser.parse('[GET,POST] [/user] [user]');
const app = new Koa();
const server = app.listen();
let finishedCount = 0;
const finish = (err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');

Expand All @@ -156,14 +156,15 @@ describe('koa-router-parser', () => {
});


it('should parse /user /user/:id router successful', done => {
it('should parse /user /user/:id router successful', (done) => {
const router = parser.parse('[GET] [/user,/user/:id] [user]');
const app = new Koa();
const server = app.listen();
let finishedCount = 0;
const finish = (err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');

Expand All @@ -183,23 +184,24 @@ describe('koa-router-parser', () => {
.end(finish);
});

it('should parse multi middleware successful', done => {
it('should parse multi middleware successful', (done) => {
const router = parser.parse('[GET] [/user/favorites/:id] [getUser&getFavorites]');
const app = new Koa();
app.use(router.routes());
request(app.listen())
.get('/user/favorites/1234')
.end((err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.status, 200);
assert.equal(res.body.length, 2);
done();
});
});

it('should parse an object successful', done => {
it('should parse an object successful', (done) => {
const router = parser.parse({
methods: ['GET'],
urls: ['/user/favorites/:id'],
Expand All @@ -214,23 +216,25 @@ describe('koa-router-parser', () => {
.get('/user/favorites/1234')
.end((err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.status, 200);
assert.equal(res.body.length, 2);
done();
});
});

it('should parse function with defaultMiddlewares successful', done => {
it('should parse function with defaultMiddlewares successful', (done) => {

const router = parser.parse('[GET,POST] [/user] [user]');
const app = new Koa();
const server = app.listen();
let finishedCount = 0;
const finish = (err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');

Expand All @@ -253,12 +257,12 @@ describe('koa-router-parser', () => {
request(server)
.post('/user')
.send({
name: 'vicanso'
name: 'vicanso',
})
.end(finish);
});

it('should parse function with params successful', done => {
it('should parse function with params successful', (done) => {
const router = parser.parse('[GET] [/user] [types(["xml", "json"]) & check-version(1) & check-name("vicanso") & check-options({"tag":"a"},1) & user]');

const app = new Koa();
Expand All @@ -269,11 +273,64 @@ describe('koa-router-parser', () => {
.set('Accept', 'json')
.end((err, res) => {
if (err) {
return done(err);
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');
done();
});
});

it('should parse function using array successful', (done) => {
const router = parser.parse([
['[GET]', '[/user]', '[types(["xml", "json"]) & check-version(1) & check-name("vicanso") & check-options({"tag":"a"},1) & user]']
]);

const app = new Koa();
app.use(router.routes());

request(app.listen())
.get('/user?version=1&name=vicanso&tag=a')
.set('Accept', 'json')
.end((err, res) => {
if (err) {
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');
done();
});
});

it('should parse function using array params successful', (done) => {
const router = parser.parse([
[
['GET'],
['/user'],
[
(ctx, next) => next(),
'types(["xml", "json"])',
'check-version(1)',
'check-name("vicanso")',
'check-options({"tag":"a"},1)',
'user',
],
],
]);

const app = new Koa();
app.use(router.routes());

request(app.listen())
.get('/user?version=1&name=vicanso&tag=a')
.set('Accept', 'json')
.end((err, res) => {
if (err) {
done(err);
return;
}
assert.equal(res.body.name, 'vicanso');
done();
});
});
});

0 comments on commit 9f200a5

Please sign in to comment.