Skip to content

Commit

Permalink
feat: add helper interface in d.ts (eggjs#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored and atian25 committed Jan 19, 2018
1 parent 19fe608 commit dede240
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const url = require('url');
module.exports = {

/**
* Generate URL path(withoud host) for route. Takes the route name and a map of named params.
* Generate URL path(without host) for route. Takes the route name and a map of named params.
* @method Helper#pathFor
* @param {String} name - Router Name
* @param {Object} params - Other params
*
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* helper.pathFor('home', { by: 'recent', limit: 20 })
* ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
* => /index.htm?by=recent&limit=20
* ```
* @return {String} url path(without host)
Expand All @@ -31,7 +31,7 @@ module.exports = {
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* helper.urlFor('home', { by: 'recent', limit: 20 })
* ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
* => http://127.0.0.1:7001/index.htm?by=recent&limit=20
* ```
* @return {String} full url(with host)
Expand Down
38 changes: 38 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@ export interface Context extends KoaApplication.Context {

response: Response;

/**
* helper
*/
helper: IHelper;

/**
* Resource Parameters
* @example
Expand Down Expand Up @@ -801,4 +806,37 @@ export interface IService { }// tslint:disable-line

export interface IController { } // tslint:disable-line

export interface IHelper {
/**
* Generate URL path(without host) for route. Takes the route name and a map of named params.
* @method Helper#pathFor
* @param {String} name - Router Name
* @param {Object} params - Other params
*
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
* => /index.htm?by=recent&limit=20
* ```
* @return {String} url path(without host)
*/
pathFor(name: string, params?: { [key: string]: any }): string;

/**
* Generate full URL(with host) for route. Takes the route name and a map of named params.
* @method Helper#urlFor
* @param {String} name - Router name
* @param {Object} params - Other params
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
* => http://127.0.0.1:7001/index.htm?by=recent&limit=20
* ```
* @return {String} full url(with host)
*/
urlFor(name: string, params?: { [key: string]: any }): string;
}

export as namespace Egg;

0 comments on commit dede240

Please sign in to comment.