diff --git a/types/koa-joi-router/index.d.ts b/types/koa-joi-router/index.d.ts index b18c47894a..e9a70d9311 100644 --- a/types/koa-joi-router/index.d.ts +++ b/types/koa-joi-router/index.d.ts @@ -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; diff --git a/types/koa-joi-router/koa-joi-router-tests.ts b/types/koa-joi-router/koa-joi-router-tests.ts index a7a745aa94..586f0c406e 100644 --- a/types/koa-joi-router/koa-joi-router-tests.ts +++ b/types/koa-joi-router/koa-joi-router-tests.ts @@ -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);