diff --git a/types/koa-joi-router/index.d.ts b/types/koa-joi-router/index.d.ts index bf3f387d31..e9e8670726 100644 --- a/types/koa-joi-router/index.d.ts +++ b/types/koa-joi-router/index.d.ts @@ -1,30 +1,36 @@ // Type definitions for koa-joi-router 5.0 // Project: https://github.com/koajs/joi-router // Definitions by: Matthew Bull +// Dave Welsh // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 import * as Koa from 'koa'; import * as Joi from 'joi'; -interface Spec { - method: string; - path: string|RegExp; - handler: (ctx: createRouter.Context) => void; - validate?: { - type: string; - body?: Joi.AnySchema; - params?: Joi.AnySchema; - output?: {[status: number]: Joi.AnySchema}; - }; -} - interface createRouter { (): createRouter.Router; Joi: typeof Joi; } declare namespace createRouter { + interface Spec { + method: string; + path: string|RegExp; + handler: (ctx: Context) => void; + validate?: { + header?: Joi.AnySchema|{[key: string]: Joi.AnySchema}; + query?: Joi.AnySchema|{[key: string]: Joi.AnySchema}; + params?: Joi.AnySchema|{[key: string]: Joi.AnySchema}; + body?: Joi.AnySchema|{[key: string]: Joi.AnySchema}; + maxBody?: number; + failure?: number; + type?: 'form'|'json'|'multipart'; + output?: {[status: number]: Joi.AnySchema}; + continueOnError?: boolean; + }; + } + interface Request extends Koa.Request { body: any; params: {[key: string]: string}; diff --git a/types/koa-joi-router/koa-joi-router-tests.ts b/types/koa-joi-router/koa-joi-router-tests.ts index 76a392bdec..90dac8d752 100644 --- a/types/koa-joi-router/koa-joi-router-tests.ts +++ b/types/koa-joi-router/koa-joi-router-tests.ts @@ -2,7 +2,7 @@ import router = require('koa-joi-router'); const { Joi } = router; -const spec1 = { +const spec1: router.Spec = { path: '/user', method: 'POST', handler: (ctx: router.Context) => ctx.body = '', @@ -10,7 +10,7 @@ const spec1 = { router().route(spec1); -const spec2 = { +const spec2: router.Spec = { method: 'PATCH', path: '/user', validate: { @@ -21,7 +21,7 @@ const spec2 = { router().route(spec2); -const spec3 = { +const spec3: router.Spec = { method: 'PATCH', path: '/user', validate: { @@ -33,7 +33,7 @@ const spec3 = { router().route(spec3); -const spec4 = { +const spec4: router.Spec = { method: 'PATCH', path: '/user', validate: { @@ -50,7 +50,7 @@ const spec4 = { router().route(spec4); -const spec5 = { +const spec5: router.Spec = { method: 'PUT', path: '/user', handler: (ctx: router.Context) => { @@ -61,7 +61,7 @@ const spec5 = { router().route(spec5); -const spec6 = { +const spec6: router.Spec = { method: 'GET', path: '/user', handler: (ctx: router.Context) => { @@ -72,6 +72,39 @@ const spec6 = { router().route(spec6); +const spec7: router.Spec = { + method: 'GET', + path: '/user', + validate: { + query: { + id: Joi.number(), + name: Joi.string(), + } + }, + handler: (ctx: router.Context) => { + ctx.status = 201; + ctx.body = ctx.request.params; + }, +}; + +router().route(spec7); + +const spec8: router.Spec = { + method: 'POST', + path: '/user', + validate: { + header: { + authentication: Joi.string(), + } + }, + handler: (ctx: router.Context) => { + ctx.status = 201; + ctx.body = ctx.request.params; + }, +}; + +router().route(spec8); + router().route([spec1, spec2, spec3]); router().routes.map(({ path }) => path);