Merge pull request #29992 from Guillaume-Mayer/koa-router-url-query

koa-router: router.url with 3rd argument (query)
This commit is contained in:
Armando Aguirre
2018-11-05 15:46:58 -08:00
committed by GitHub
2 changed files with 35 additions and 5 deletions
+22 -2
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;
@@ -258,9 +263,24 @@ declare class Router {
/**
* Generate URL for route. Takes either map of named `params` or series of
* arguments (for regular expression routes)
*
* router = new Router();
* router.get('user', "/users/:id", ...
*
* 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): string;
url(name: string, params: Object): 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.
+13 -3
View File
@@ -14,11 +14,21 @@ 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('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) {
// ...