koa-joi-router: updated to match usage instructions on koajs/joi-router (#27704)

* koa-joi-browser fixes

* fixed linting errors
This commit is contained in:
Dave Welsh 2018-07-31 15:22:16 -04:00 committed by Sheetal Nandi
parent 6e755bfc1b
commit b3be281d3c
2 changed files with 57 additions and 18 deletions

View File

@ -1,30 +1,36 @@
// Type definitions for koa-joi-router 5.0
// Project: https://github.com/koajs/joi-router
// Definitions by: Matthew Bull <https://github.com/wingsbob>
// Dave Welsh <https://github.com/move-zig>
// 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};

View File

@ -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);