koa-router: router.url with 3rd argument (query)

This commit is contained in:
Guillaume Mayer
2018-10-24 02:45:19 -03:00
parent 3b01bb29b1
commit bbdf8ce9db
2 changed files with 29 additions and 1 deletions
+22
View File
@@ -4,6 +4,7 @@
// Pavel Ivanov <https://github.com/schfkt>
// JounQin <https://github.com/JounQin>
// Romain Faust <https://github.com/romain-faust>
// Guillaume Mayer <https://github.com/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.
*/
+7 -1
View File
@@ -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) {
// ...