Skip to content

Commit

Permalink
feat(types): remove declaration of view (#3466)
Browse files Browse the repository at this point in the history
Declaration of `egg-view` has migrated to https://github.com/eggjs/egg-view/blob/master/index.d.ts . And egg has import 'egg-view' in dts, so it's no need to declare again in egg.
  • Loading branch information
whxaxes authored and Maledong committed Feb 15, 2019
1 parent 4a3ab5a commit 1e859f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 51 deletions.
51 changes: 0 additions & 51 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,6 @@ declare module 'egg' {
realStatus: number;
}

export interface ContextView { // tslint:disable-line
/**
* Render a file by view engine
* @param {String} name - the file path based on root
* @param {Object} [locals] - data used by template
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
* @return {Promise<String>} result - return a promise with a render result
*/
render(name: string, locals?: any, options?: any): Promise<string>;

/**
* Render a template string by view engine
* @param {String} tpl - template string
* @param {Object} [locals] - data used by template
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
* @return {Promise<String>} result - return a promise with a render result
*/
renderString(name: string, locals?: any, options?: any): Promise<string>;
}

export type LoggerLevel = EggLoggerLevel;

/**
Expand Down Expand Up @@ -436,14 +416,6 @@ declare module 'egg' {
maxFiles: number;
} & PlainObject;

view: {
root: string;
cache: boolean;
defaultExtension: string;
defaultViewEngine: string;
mapping: PlainObject<string>;
};

watcher: PlainObject;

onClientError(err: Error, socket: Socket, app: EggApplication): ClientErrorResponse | Promise<ClientErrorResponse>;
Expand Down Expand Up @@ -902,36 +874,13 @@ declare module 'egg' {
*/
starttime: number;

/**
* View instance that is created every request
*/
view: ContextView;

/**
* http request helper base on httpclient, it will auto save httpclient log.
* Keep the same api with httpclient.request(url, args).
* See https://github.com/node-modules/urllib#api-doc for more details.
*/
curl<T = any>(url: string, opt?: RequestOptions): Promise<T>;

/**
* Render a file by view engine
* @param {String} name - the file path based on root
* @param {Object} [locals] - data used by template
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
* @return {Promise<String>} result - return a promise with a render result
*/
render(name: string, locals?: any, options?: any): Promise<string>;

/**
* Render a template string by view engine
* @param {String} tpl - template string
* @param {Object} [locals] - data used by template
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
* @return {Promise<String>} result - return a promise with a render result
*/
renderString(name: string, locals?: any, options?: any): Promise<string>;

__(key: string, ...values: string[]): string;
gettext(key: string, ...values: string[]): string;

Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/apps/app-ts/app/controller/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ export default class FooController extends Controller {
this.app.logger.info(e.name, body.foo);
}
}

async getBar() {
try {
this.ctx.body = await this.service.foo.bar();
} catch (e) {
this.ctx.logger.error(e);
}
}

async httpclient() {
await this.app.httpclient.request('url', {
method: 'POST',
Expand All @@ -37,4 +39,18 @@ export default class FooController extends Controller {
method: 'POST',
});
}

async testViewRender() {
const { ctx } = this;
this.app.logger.info(this.app.view.get('nunjucks'));
this.app.logger.info(this.app.config.view.root);
this.app.logger.info(this.app.config.view.defaultExtension);
ctx.body = await this.ctx.view.render('test.tpl', {
test: '123'
});
}

async testViewRenderString() {
this.ctx.body = await this.ctx.view.renderString('test');
}
}

0 comments on commit 1e859f2

Please sign in to comment.