From 11f441840ca8ad0d6dc14fe7a69b60abf9f254d5 Mon Sep 17 00:00:00 2001 From: Romain Faust <11320210+romain-faust@users.noreply.github.com> Date: Fri, 27 Jul 2018 17:54:15 +0200 Subject: [PATCH] Add koa-router's Router to koa's Context (#27472) --- types/koa-router/index.d.ts | 5 ++++ types/koa-router/koa-router-tests.ts | 36 ++++++++++++++++------------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/types/koa-router/index.d.ts b/types/koa-router/index.d.ts index 7382dd91e6..f8a31e71cb 100644 --- a/types/koa-router/index.d.ts +++ b/types/koa-router/index.d.ts @@ -3,6 +3,7 @@ // Definitions by: Jerry Chin // Pavel Ivanov // JounQin +// Romain Faust // Definitions: https://github.com/hellopao/DefinitelyTyped // TypeScript Version: 2.3 @@ -51,6 +52,10 @@ declare namespace Router { * url params */ params: any; + /** + * the router instance + */ + router: Router; } export interface IMiddleware { diff --git a/types/koa-router/koa-router-tests.ts b/types/koa-router/koa-router-tests.ts index 1325da0de0..b2bec9ad0c 100644 --- a/types/koa-router/koa-router-tests.ts +++ b/types/koa-router/koa-router-tests.ts @@ -8,21 +8,27 @@ const router = new Router({ }); router - .param('id', function(id, ctx, next) { - next(); - }) - .get('/', function (ctx, next) { - ctx.body = 'Hello World!'; - }) - .post('/users', function (ctx, next) { - // ... - }) - .put('/users/:id', function (ctx, next) { - ctx.body = ctx.params.id; - }) - .del('/users/:id', function () { - // ... - }); + .param('id', function (id, ctx, next) { + next(); + }) + .get('/', function (ctx, next) { + ctx.body = 'Hello World!'; + }) + .get('/users/:id', function (ctx, next) { + ctx.body = ctx.router.url('user-accounts', { id: ctx.params.id }) + }) + .get('user-accounts', '/users/:id/accounts', function (ctx, next) { + // ... + }) + .post('/users', function (ctx, next) { + // ... + }) + .put('/users/:id', function (ctx, next) { + ctx.body = ctx.params.id; + }) + .del('/users/:id', function () { + // ... + }); router.get('user', '/users/:id', function (ctx) { ctx.body = "sdsd";