ts-router
Version:
koa router middleware, typescript fully supported, jersey like coding
60 lines (56 loc) • 1.72 kB
text/typescript
import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';
.Path('/test')
class TestController extends tsRouter.Controller {
.Path('/:v1/:v2')
.GET
.Produce(tsRouter.MediaType.JSON)
async index(
.Params params:Object,
.PathParam('v1') v1:string,
.PathParam('v2') v2:string
):Promise<tsRouter.Response> {
return tsRouter.Response.status(200).body({v1, v2, params}).build();
}
.After
async after(
.Params params:Object,
.PathParam('v1') v1:string,
.PathParam('v2') v2:string,
.AppContext context:tsRouter.Context,
.RouteResponse res:tsRouter.Response
) {
res.body = JSON.stringify({v1, v2, params, v3: v1 + v2});
res.send(context);
}
}
const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server;
describe('GET with path paramters with afterwares', () => {
before(() => {
server = app.listen(3000)
})
after(() => {
server.close();
})
it('should respond paramters in back in json', function (done) {
request(server)
.get('/test/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hello',
v2: 'world',
v3: 'helloworld'
})
.expect(200, done);
});
})