Identified some missing definitions for ctx.request.body and ctx.request.params

This commit is contained in:
Matthew Bull
2017-11-06 16:43:19 +00:00
parent 31c8432e74
commit 87c3e156e7
2 changed files with 41 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ import * as Joi from 'joi';
interface Spec {
method: string;
path: string|RegExp;
handler: (ctx: Koa.Context) => void;
handler: (ctx: createRouter.Context) => void;
validate?: {
type: string;
body?: Joi.AnySchema;
@@ -27,6 +27,17 @@ interface createRouter {
Joi: typeof Joi;
}
declare var create: createRouter;
declare namespace createRouter {
interface Request extends Koa.Request {
body: any;
params: {[key: string]: string};
}
export = create;
interface Context extends Koa.Context {
request: Request;
}
}
declare var createRouter: createRouter;
export = createRouter;

View File

@@ -1,12 +1,11 @@
import * as router from 'koa-joi-router';
import { Context } from 'koa';
const { Joi } = router;
const spec1 = {
path: '/user',
method: 'POST',
handler: (ctx: Context) => ctx.body = '',
handler: (ctx: router.Context) => ctx.body = '',
};
router().route(spec1);
@@ -17,7 +16,7 @@ const spec2 = {
validate: {
type: 'json',
},
handler: (ctx: Context) => ctx.status = 201,
handler: (ctx: router.Context) => ctx.status = 201,
};
router().route(spec2);
@@ -29,7 +28,7 @@ const spec3 = {
type: 'json',
body: Joi.any(),
},
handler: (ctx: Context) => ctx.status = 201,
handler: (ctx: router.Context) => ctx.status = 201,
};
router().route(spec3);
@@ -41,10 +40,32 @@ const spec4 = {
type: 'json',
201: Joi.object(),
},
handler: (ctx: Context) => {
handler: (ctx: router.Context) => {
ctx.status = 201;
ctx.body = {};
},
};
router().route(spec3);
router().route(spec4);
const spec5 = {
method: 'PUT',
path: '/user',
handler: (ctx: router.Context) => {
ctx.status = 201;
ctx.body = ctx.request.body;
},
};
router().route(spec5);
const spec6 = {
method: 'GET',
path: '/user',
handler: (ctx: router.Context) => {
ctx.status = 201;
ctx.body = ctx.request.params;
},
};
router().route(spec6);