index.d.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Application, PlainObject } from 'egg';
  2. interface RenderOptions extends PlainObject {
  3. name?: string;
  4. root?: string;
  5. locals?: PlainObject;
  6. viewEngine?: string;
  7. }
  8. interface ViewBase {
  9. /**
  10. * Render a file by view engine, then set to body
  11. * @param {String} name - the file path based on root
  12. * @param {Object} [locals] - data used by template
  13. * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
  14. * @return {Promise<String>} result - return a promise with a render result
  15. */
  16. render(name: string, locals?: any, options?: RenderOptions): Promise<null>;
  17. /**
  18. * Render a file by view engine and return it
  19. * @param {String} name - the file path based on root
  20. * @param {Object} [locals] - data used by template
  21. * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
  22. * @return {Promise<String>} result - return a promise with a render result
  23. */
  24. renderView(
  25. name: string,
  26. locals?: any,
  27. options?: RenderOptions
  28. ): Promise<string>;
  29. /**
  30. * Render a template string by view engine
  31. * @param {String} tpl - template string
  32. * @param {Object} [locals] - data used by template
  33. * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
  34. * @return {Promise<String>} result - return a promise with a render result
  35. */
  36. renderString(
  37. name: string,
  38. locals?: any,
  39. options?: RenderOptions
  40. ): Promise<string>;
  41. }
  42. interface ViewManager extends Map<string, any> {
  43. use(name: string, viewEngine: ViewBase): void;
  44. resolve(name: string): Promise<string>;
  45. }
  46. interface ContextView extends ViewBase {
  47. app: Application;
  48. viewManager: ViewManager;
  49. }
  50. declare module 'egg' {
  51. interface Application {
  52. view: ViewManager;
  53. }
  54. interface Context extends ViewBase {
  55. /**
  56. * View instance that is created every request
  57. */
  58. view: ContextView;
  59. }
  60. interface EggAppConfig {
  61. view: {
  62. root: string;
  63. cache: boolean;
  64. defaultExtension: string;
  65. defaultViewEngine: string;
  66. mapping: PlainObject<string>;
  67. };
  68. }
  69. }