From bbdf8ce9dbb462f488b49234e8dacec1ac167e8f Mon Sep 17 00:00:00 2001 From: Guillaume Mayer Date: Wed, 24 Oct 2018 02:45:19 -0300 Subject: [PATCH 1/3] koa-router: router.url with 3rd argument (query) --- types/koa-router/index.d.ts | 22 ++++++++++++++++++++++ types/koa-router/koa-router-tests.ts | 8 +++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/types/koa-router/index.d.ts b/types/koa-router/index.d.ts index b5dc934965..29e85a75de 100644 --- a/types/koa-router/index.d.ts +++ b/types/koa-router/index.d.ts @@ -4,6 +4,7 @@ // Pavel Ivanov // JounQin // Romain Faust +// Guillaume Mayer // Definitions: https://github.com/hellopao/DefinitelyTyped // TypeScript Version: 2.3 @@ -86,6 +87,10 @@ declare namespace Router { sensitive?: boolean; strict?: boolean; } + + export interface IUrlOptionsQuery { + query?: object | string; + } export class ParamName { asterisk: boolean; @@ -262,6 +267,23 @@ declare class Router { url(name: string, params: Object): string; url(name: string, params: Object): Error; + /** + * Generate URL for route with query part. + * + * Takes either map of named `params` or series of + * arguments (for regular expression routes) + * + * Query is generated from `options` argument: + * + * router.url('user', { id: 3 }, { query: { limit: 1 } }); + * // => "/users/3?limit=1" + * + * router.url('user', { id: 3 }, { query: "limit=1" }); + * // => "/users/3?limit=1" + */ + url(name: string, params: Object, options?: Router.IUrlOptionsQuery): string; + url(name: string, params: Object, options?: Router.IUrlOptionsQuery): Error; + /** * Match given `path` and return corresponding routes. */ diff --git a/types/koa-router/koa-router-tests.ts b/types/koa-router/koa-router-tests.ts index b2bec9ad0c..d7b3807b2d 100644 --- a/types/koa-router/koa-router-tests.ts +++ b/types/koa-router/koa-router-tests.ts @@ -18,7 +18,13 @@ router ctx.body = ctx.router.url('user-accounts', { id: ctx.params.id }) }) .get('user-accounts', '/users/:id/accounts', function (ctx, next) { - // ... + ctx.body = { + test1: ctx.router.url('users', { id: 3 }, { query: { limit: 1 } }), + test2: ctx.router.url('users', { id: 3 }, { query: "limit=1" }), + test3: ctx.router.url('users', 3, { query: { limit: 1 } }), + test4: ctx.router.url('users', [3], { query: "limit=1" }), + test5: ctx.router.url('users', ["3"], { query: { limit: "1" } }), + } }) .post('/users', function (ctx, next) { // ... From 098ee2ffeb615c3387eda384750665d18a022099 Mon Sep 17 00:00:00 2001 From: Guillaume Mayer Date: Wed, 24 Oct 2018 03:33:15 -0300 Subject: [PATCH 2/3] remove 2 arg version, Object to any --- types/koa-router/index.d.ts | 22 ++++++++++------------ types/koa-router/koa-router-tests.ts | 18 +++++++++++------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/types/koa-router/index.d.ts b/types/koa-router/index.d.ts index 29e85a75de..93473626fa 100644 --- a/types/koa-router/index.d.ts +++ b/types/koa-router/index.d.ts @@ -263,26 +263,24 @@ declare class Router { /** * Generate URL for route. Takes either map of named `params` or series of * arguments (for regular expression routes) - */ - url(name: string, params: Object): string; - url(name: string, params: Object): Error; - - /** - * Generate URL for route with query part. * - * Takes either map of named `params` or series of - * arguments (for regular expression routes) + * router = new Router(); + * router.get('user', "/users/:id", ... * - * Query is generated from `options` argument: + * router.url('user', { id: 3 }); + * // => "/users/3" + * + * Query can be generated from third argument: * * router.url('user', { id: 3 }, { query: { limit: 1 } }); * // => "/users/3?limit=1" - * + * * router.url('user', { id: 3 }, { query: "limit=1" }); * // => "/users/3?limit=1" + * */ - url(name: string, params: Object, options?: Router.IUrlOptionsQuery): string; - url(name: string, params: Object, options?: Router.IUrlOptionsQuery): Error; + url(name: string, params: any, options?: Router.IUrlOptionsQuery): string; + url(name: string, params: any, options?: Router.IUrlOptionsQuery): Error; /** * Match given `path` and return corresponding routes. diff --git a/types/koa-router/koa-router-tests.ts b/types/koa-router/koa-router-tests.ts index d7b3807b2d..b5a4083146 100644 --- a/types/koa-router/koa-router-tests.ts +++ b/types/koa-router/koa-router-tests.ts @@ -14,16 +14,20 @@ router .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', '/users/:id', function (ctx, next) { + ctx.body = { + test1: ctx.router.url('user-accounts', { id: ctx.params.id }), + test2: ctx.router.url('user-accounts', ctx.params.id), + test3: ctx.router.url('user-accounts', [ctx.params.id]), + } }) .get('user-accounts', '/users/:id/accounts', function (ctx, next) { ctx.body = { - test1: ctx.router.url('users', { id: 3 }, { query: { limit: 1 } }), - test2: ctx.router.url('users', { id: 3 }, { query: "limit=1" }), - test3: ctx.router.url('users', 3, { query: { limit: 1 } }), - test4: ctx.router.url('users', [3], { query: "limit=1" }), - test5: ctx.router.url('users', ["3"], { query: { limit: "1" } }), + test1: ctx.router.url('user', { id: 3 }, { query: { limit: 1 } }), + test2: ctx.router.url('user', { id: 3 }, { query: "limit=1" }), + test3: ctx.router.url('user', 3, { query: { limit: 1 } }), + test4: ctx.router.url('user', [3], { query: "limit=1" }), + test5: ctx.router.url('user', ["3"], { query: { limit: "1" } }), } }) .post('/users', function (ctx, next) { From 5716e008b683da10856172d71f5fb77f689b6e62 Mon Sep 17 00:00:00 2001 From: Guillaume Mayer Date: Wed, 24 Oct 2018 03:49:19 -0300 Subject: [PATCH 3/3] query required --- types/koa-router/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/koa-router/index.d.ts b/types/koa-router/index.d.ts index 93473626fa..6763f8d7b8 100644 --- a/types/koa-router/index.d.ts +++ b/types/koa-router/index.d.ts @@ -89,7 +89,7 @@ declare namespace Router { } export interface IUrlOptionsQuery { - query?: object | string; + query: object | string; } export class ParamName {