Skip to content

Commit

Permalink
fix(types): app.router.url params should be optional (#5132)
Browse files Browse the repository at this point in the history
add missing pathFor and methods defines
  • Loading branch information
fengmk2 committed Jan 20, 2023
1 parent 2c79734 commit dda6bb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,15 @@ declare module 'egg' {
headers: { [key: string]: string };
}

export interface Router extends KoaRouter<any, Context> {
export interface Router extends Omit<KoaRouter<any, Context>, 'url'> {
/**
* restful router api
*/
resources(name: string, prefix: string, ...middleware: any[]): Router;

/**
* @param {String} name - Router name
* @param {Object} params - more parameters
* @param {Object} [params] - more parameters
* @example
* ```js
* router.url('edit_post', { id: 1, name: 'foo', page: 2 })
Expand All @@ -565,7 +565,12 @@ declare module 'egg' {
* @return {String} url by path name and query params.
* @since 1.0.0
*/
url(name: string, params: any): any;
url(name: string, params?: any): string;
/**
* Alias for the url method
*/
pathFor(name: string, params?: any): string;
methods: string[];
}

export interface EggApplication extends EggCoreBase<EggAppConfig> { // tslint:disable-line
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/apps/app-ts/app/controller/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export default class FooController extends Controller {
this.fooLogger = ctx.getLogger('foo');
assert(ctx.app.ctxStorage);
assert(ctx.app.currentContext);

// router
console.log(ctx.app.router.url('foo'));
console.log(ctx.app.router.url('foo', {}));
console.log(ctx.app.router.pathFor('foo'));
console.log(ctx.app.router.pathFor('foo', {}));
console.log(ctx.app.router.methods);
}

async getData() {
Expand Down

0 comments on commit dda6bb3

Please sign in to comment.