Skip to content

Commit dda6bb3

Browse files
authored
fix(types): app.router.url params should be optional (#5132)
add missing pathFor and methods defines
1 parent 2c79734 commit dda6bb3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

index.d.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,15 @@ declare module 'egg' {
546546
headers: { [key: string]: string };
547547
}
548548

549-
export interface Router extends KoaRouter<any, Context> {
549+
export interface Router extends Omit<KoaRouter<any, Context>, 'url'> {
550550
/**
551551
* restful router api
552552
*/
553553
resources(name: string, prefix: string, ...middleware: any[]): Router;
554554

555555
/**
556556
* @param {String} name - Router name
557-
* @param {Object} params - more parameters
557+
* @param {Object} [params] - more parameters
558558
* @example
559559
* ```js
560560
* router.url('edit_post', { id: 1, name: 'foo', page: 2 })
@@ -565,7 +565,12 @@ declare module 'egg' {
565565
* @return {String} url by path name and query params.
566566
* @since 1.0.0
567567
*/
568-
url(name: string, params: any): any;
568+
url(name: string, params?: any): string;
569+
/**
570+
* Alias for the url method
571+
*/
572+
pathFor(name: string, params?: any): string;
573+
methods: string[];
569574
}
570575

571576
export interface EggApplication extends EggCoreBase<EggAppConfig> { // tslint:disable-line

test/fixtures/apps/app-ts/app/controller/foo.ts

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export default class FooController extends Controller {
3030
this.fooLogger = ctx.getLogger('foo');
3131
assert(ctx.app.ctxStorage);
3232
assert(ctx.app.currentContext);
33+
34+
// router
35+
console.log(ctx.app.router.url('foo'));
36+
console.log(ctx.app.router.url('foo', {}));
37+
console.log(ctx.app.router.pathFor('foo'));
38+
console.log(ctx.app.router.pathFor('foo', {}));
39+
console.log(ctx.app.router.methods);
3340
}
3441

3542
async getData() {

0 commit comments

Comments
 (0)