1
- import {
2
- Console ,
3
- IApplicationMessageToConsole ,
4
- IHandlebars ,
5
- Logger ,
6
- Middleware ,
7
- RenderTemplateOptions ,
8
- } from "@expressots/core" ;
1
+ import { Console , IApplicationMessageToConsole , Logger , Middleware } from "@expressots/core" ;
9
2
import express from "express" ;
10
3
import { Container } from "inversify" ;
11
4
import { provide } from "inversify-binding-decorators" ;
@@ -18,8 +11,12 @@ import {
18
11
MiddlewareConfig ,
19
12
ServerEnvironment ,
20
13
} from "./application-express.types" ;
21
- import { InversifyExpressServer } from "./express-utils/inversify-express-server" ;
22
14
import { HttpStatusCodeMiddleware } from "./express-utils/http-status-middleware" ;
15
+ import { InversifyExpressServer } from "./express-utils/inversify-express-server" ;
16
+ import { EjsOptions , setEngineEjs } from "./render/ejs/ejs.config" ;
17
+ import { Engine , EngineOptions , RenderOptions } from "./render/engine" ;
18
+ import { HandlebarsOptions , setEngineHandlebars } from "./render/handlebars/hbs.config" ;
19
+ import { packageResolver } from "./render/resolve-render" ;
23
20
24
21
/**
25
22
* The AppExpress class provides methods for configuring and running an Express application.
@@ -42,6 +39,7 @@ class AppExpress extends ApplicationBase implements IWebServer {
42
39
private globalPrefix : string = "/" ;
43
40
private middlewares : Array < ExpressHandler | MiddlewareConfig | ExpressoMiddleware > = [ ] ;
44
41
private console : Console ;
42
+ private renderOptions : RenderOptions = { } as RenderOptions ;
45
43
46
44
protected configureServices ( ) : void | Promise < void > { }
47
45
protected postServerInitialization ( ) : void | Promise < void > { }
@@ -146,6 +144,7 @@ class AppExpress extends ApplicationBase implements IWebServer {
146
144
consoleMessage ?: IApplicationMessageToConsole ,
147
145
) : Promise < void > {
148
146
await this . init ( ) ;
147
+ await this . configEngine ( ) ;
149
148
150
149
this . port = port || 3000 ;
151
150
this . environment = environment ;
@@ -177,23 +176,45 @@ class AppExpress extends ApplicationBase implements IWebServer {
177
176
this . globalPrefix = prefix ;
178
177
}
179
178
179
+ /**
180
+ * Configures the application's view engine based on the provided configuration options.
181
+ */
182
+ private async configEngine ( ) : Promise < void > {
183
+ if ( this . renderOptions . engine ) {
184
+ switch ( this . renderOptions . engine ) {
185
+ case Engine . HBS :
186
+ await setEngineHandlebars ( this . app , this . renderOptions . options as HandlebarsOptions ) ;
187
+ break ;
188
+ case Engine . EJS :
189
+ await setEngineEjs ( this . app , this . renderOptions . options as EjsOptions ) ;
190
+ break ;
191
+ default :
192
+ throw new Error ( "Unsupported engine type!" ) ;
193
+ }
194
+ }
195
+ }
196
+
180
197
/**
181
198
* Configures the application's view engine based on the provided configuration options.
182
199
*
183
200
* @public
184
201
* @method setEngine
185
202
* @template T - A generic type extending from RenderTemplateOptions.
186
203
*
187
- * @param {T } options - An object of type T (must be an object that extends RenderTemplateOptions)
188
- * that provides the configuration options for setting the view engine.
189
- * This includes the extension name, view path, and the engine function itself.
204
+ * @param {Engine } engine - The view engine to set
205
+ * @param {EngineOptions } [options] - The configuration options for the view engine
190
206
*/
191
- public setEngine < T extends RenderTemplateOptions > ( options : T ) : void {
192
- if ( "extName" in options ) {
193
- const { extName, viewPath, engine } = options as IHandlebars ;
194
- this . app . engine ( extName , engine ) ;
195
- this . app . set ( "view engine" , extName ) ;
196
- this . app . set ( "views" , viewPath ) ;
207
+ public async setEngine < T extends EngineOptions > ( engine : Engine , options ?: T ) : Promise < void > {
208
+ packageResolver ( engine , options ) ;
209
+
210
+ try {
211
+ if ( options ) {
212
+ this . renderOptions = { engine, options } ;
213
+ } else {
214
+ this . renderOptions = { engine } ;
215
+ }
216
+ } catch ( error : unknown ) {
217
+ this . logger . error ( ( error as Error ) . message , "adapter-express" ) ;
197
218
}
198
219
}
199
220
0 commit comments