@expressots/adapter-express 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
 
2
2
 
3
+ ## [1.7.0](https://github.com/expressots/adapter-express/compare/1.6.0...1.7.0) (2024-07-23)
4
+
5
+
6
+ ### Features
7
+
8
+ * add @Render decorator ([522d6e2](https://github.com/expressots/adapter-express/commit/522d6e27163b1ec638a9d5328fb3bf5f81054c5c))
9
+ * add pug engine support ([a879f96](https://github.com/expressots/adapter-express/commit/a879f9642dccd74a49b9188a25fcd1bbf30eb8ba))
10
+ * bump husky from 9.0.11 to 9.1.1 ([7c1678a](https://github.com/expressots/adapter-express/commit/7c1678abf4a78e52483fff190524c5658dba1100))
11
+ * bump vite from 5.3.3 to 5.3.4 ([86f51a3](https://github.com/expressots/adapter-express/commit/86f51a3f5f920fcc7a760730e877ae830fa574e5))
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * add options to ejs and hbs support ([b52ce5c](https://github.com/expressots/adapter-express/commit/b52ce5c3860ea7d859534d96cee030dbc4ffc19c))
17
+
3
18
  ## [1.6.0](https://github.com/expressots/adapter-express/compare/1.5.0...1.6.0) (2024-07-17)
4
19
 
5
20
 
@@ -20,7 +20,7 @@ const inversify_express_server_1 = require("./express-utils/inversify-express-se
20
20
  const ejs_config_1 = require("./render/ejs/ejs.config");
21
21
  const engine_1 = require("./render/engine");
22
22
  const hbs_config_1 = require("./render/handlebars/hbs.config");
23
- const resolve_render_1 = require("./render/resolve-render");
23
+ const pug_config_1 = require("./render/pug/pug.config");
24
24
  /**
25
25
  * The AppExpress class provides methods for configuring and running an Express application.
26
26
  * @class AppExpress
@@ -163,6 +163,9 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
163
163
  case engine_1.Engine.EJS:
164
164
  await (0, ejs_config_1.setEngineEjs)(this.app, this.renderOptions.options);
165
165
  break;
166
+ case engine_1.Engine.PUG:
167
+ await (0, pug_config_1.setEnginePug)(this.app, this.renderOptions.options);
168
+ break;
166
169
  default:
167
170
  throw new Error("Unsupported engine type!");
168
171
  }
@@ -179,7 +182,6 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
179
182
  * @param {EngineOptions} [options] - The configuration options for the view engine
180
183
  */
181
184
  async setEngine(engine, options) {
182
- (0, resolve_render_1.packageResolver)(engine, options);
183
185
  try {
184
186
  if (options) {
185
187
  this.renderOptions = { engine, options };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_ROUTING_ROOT_PATH = exports.NO_CONTROLLERS_FOUND = exports.DUPLICATED_CONTROLLER_NAME = exports.HTTP_VERBS_ENUM = exports.PARAMETER_TYPE = exports.HTTP_CODE_METADATA = exports.METADATA_KEY = exports.TYPE = void 0;
3
+ exports.DEFAULT_ROUTING_ROOT_PATH = exports.NO_CONTROLLERS_FOUND = exports.DUPLICATED_CONTROLLER_NAME = exports.HTTP_VERBS_ENUM = exports.PARAMETER_TYPE = exports.RENDER_METADATA_KEY = exports.HTTP_CODE_METADATA = exports.METADATA_KEY = exports.TYPE = void 0;
4
4
  exports.TYPE = {
5
5
  AuthProvider: Symbol.for("AuthProvider"),
6
6
  Controller: Symbol.for("Controller"),
@@ -17,6 +17,7 @@ exports.HTTP_CODE_METADATA = {
17
17
  statusCode: "inversify-express-utils:statuscode",
18
18
  path: "inversify-express-utils:path",
19
19
  };
20
+ exports.RENDER_METADATA_KEY = Symbol("Render");
20
21
  var PARAMETER_TYPE;
21
22
  (function (PARAMETER_TYPE) {
22
23
  PARAMETER_TYPE[PARAMETER_TYPE["REQUEST"] = 0] = "REQUEST";
@@ -12,6 +12,8 @@ exports.Head = Head;
12
12
  exports.Delete = Delete;
13
13
  exports.httpMethod = httpMethod;
14
14
  exports.params = params;
15
+ exports.Render = Render;
16
+ exports.getRenderMetadata = getRenderMetadata;
15
17
  require("reflect-metadata");
16
18
  const inversify_1 = require("inversify");
17
19
  const constants_1 = require("./constants");
@@ -301,6 +303,21 @@ function params(type, parameterName) {
301
303
  Reflect.defineMetadata(constants_1.METADATA_KEY.controllerParameter, metadataList, target.constructor);
302
304
  };
303
305
  }
306
+ /**
307
+ * Render decorator to define the template and default data for a route
308
+ * @param template The template to render
309
+ * @param defaultData The default data to pass to the template
310
+ * @returns
311
+ */
312
+ function Render(template, defaultData) {
313
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
314
+ return (target, propertyKey, descriptor) => {
315
+ Reflect.defineMetadata(constants_1.RENDER_METADATA_KEY, { template, defaultData }, target, propertyKey);
316
+ };
317
+ }
318
+ function getRenderMetadata(target, propertyKey) {
319
+ return Reflect.getMetadata(constants_1.RENDER_METADATA_KEY, target, propertyKey) || {};
320
+ }
304
321
  /**
305
322
  * Converts a string value to the specified type.
306
323
  * @param value The value to convert.
@@ -30,6 +30,7 @@ const base_middleware_1 = require("./base-middleware");
30
30
  const utils_1 = require("./utils");
31
31
  const constants_1 = require("./constants");
32
32
  const httpResponseMessage_1 = require("./httpResponseMessage");
33
+ const decorators_1 = require("./decorators");
33
34
  class InversifyExpressServer {
34
35
  /**
35
36
  * Wrapper for the express server.
@@ -182,8 +183,14 @@ class InversifyExpressServer {
182
183
  const httpContext = this._getHttpContext(req);
183
184
  httpContext.container.bind(constants_1.TYPE.HttpContext).toConstantValue(httpContext);
184
185
  // invoke controller's action
185
- const value = await httpContext.container.getNamed(constants_1.TYPE.Controller, controllerName)[key](...args);
186
- if (value instanceof httpResponseMessage_1.HttpResponseMessage) {
186
+ const controller = httpContext.container.getNamed(constants_1.TYPE.Controller, controllerName);
187
+ const value = await controller[key](...args);
188
+ const { template, defaultData } = (0, decorators_1.getRenderMetadata)(controller, key);
189
+ if (template) {
190
+ const data = value || defaultData || {};
191
+ res.render(template, data);
192
+ }
193
+ else if (value instanceof httpResponseMessage_1.HttpResponseMessage) {
187
194
  await this.handleHttpResponseMessage(value, res);
188
195
  }
189
196
  else if ((0, utils_1.instanceOfIHttpActionResult)(value)) {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setEngineEjs = setEngineEjs;
4
4
  const path_1 = require("path");
5
+ const resolve_render_1 = require("../resolve-render");
5
6
  /**
6
7
  * Ejs defaults
7
8
  * @type {EjsOptions}
@@ -19,6 +20,7 @@ const EJS_DEFAULTS = {
19
20
  * @param {EjsOptions} [options=EJS_DEFAULTS] - The ejs options
20
21
  */
21
22
  async function setEngineEjs(app, options = EJS_DEFAULTS) {
23
+ (0, resolve_render_1.packageResolver)("ejs");
22
24
  app.set("view engine", options.viewEngine || EJS_DEFAULTS.viewEngine);
23
25
  app.set("views", options.viewsDir || EJS_DEFAULTS.viewsDir);
24
26
  if (Array.isArray(options.viewsDir)) {
@@ -11,4 +11,5 @@ var Engine;
11
11
  (function (Engine) {
12
12
  Engine["HBS"] = "hbs";
13
13
  Engine["EJS"] = "ejs";
14
+ Engine["PUG"] = "pug";
14
15
  })(Engine || (exports.Engine = Engine = {}));
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setEngineHandlebars = setEngineHandlebars;
4
4
  const path_1 = require("path");
5
5
  const core_1 = require("@expressots/core");
6
+ const resolve_render_1 = require("../resolve-render");
6
7
  /**
7
8
  * Handlebars defaults
8
9
  * @type {HandlebarsOptions}
@@ -10,10 +11,14 @@ const core_1 = require("@expressots/core");
10
11
  * @default
11
12
  */
12
13
  const HANDLEBARS_DEFAULTS = {
13
- viewsDir: (0, path_1.join)(process.cwd(), "views"),
14
14
  viewEngine: "hbs",
15
- serverOptions: {},
15
+ viewsDir: (0, path_1.join)(process.cwd(), "views"),
16
+ partialsDir: (0, path_1.join)(process.cwd(), "views/partials"),
16
17
  };
18
+ /**
19
+ * Default partials directory
20
+ */
21
+ const DEFAULT_PARTIALS_DIR = (0, path_1.join)(process.cwd(), "views/partials");
17
22
  /**
18
23
  * Set Handlebars as the view engine
19
24
  * @param {express.Application} app - The express application
@@ -22,19 +27,10 @@ const HANDLEBARS_DEFAULTS = {
22
27
  async function setEngineHandlebars(app, options = HANDLEBARS_DEFAULTS) {
23
28
  const logger = new core_1.Logger();
24
29
  try {
30
+ const hbs = (0, resolve_render_1.packageResolver)("hbs");
31
+ hbs.registerPartials(options.partialsDir || DEFAULT_PARTIALS_DIR);
25
32
  app.set("view engine", options.viewEngine || HANDLEBARS_DEFAULTS.viewEngine);
26
33
  app.set("views", options.viewsDir || HANDLEBARS_DEFAULTS.viewsDir);
27
- if (Array.isArray(options.viewsDir)) {
28
- options.viewsDir.forEach((dir) => {
29
- app.set("views", dir);
30
- });
31
- }
32
- if (options.serverOptions) {
33
- app.locals = {
34
- ...app.locals,
35
- ...options.serverOptions,
36
- };
37
- }
38
34
  }
39
35
  catch (error) {
40
36
  logger.error(error.message, "handlebars-config");
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setEnginePug = setEnginePug;
4
+ const path_1 = require("path");
5
+ const resolve_render_1 = require("../resolve-render");
6
+ /**
7
+ * Pug defaults
8
+ * @type {PugOptions}
9
+ * @constant
10
+ * @default
11
+ */
12
+ const PUG_DEFAULTS = {
13
+ viewEngine: "pug",
14
+ viewsDir: (0, path_1.join)(process.cwd(), "views"),
15
+ };
16
+ /**
17
+ * Set Pug as the view engine
18
+ * @param {express.Application} app - The express application
19
+ * @param {PugOptions} [options=PUG_DEFAULTS] - The pug options
20
+ */
21
+ async function setEnginePug(app, options = PUG_DEFAULTS) {
22
+ (0, resolve_render_1.packageResolver)("pug");
23
+ app.set("view engine", options.viewEngine || PUG_DEFAULTS.viewEngine);
24
+ app.set("views", options.viewsDir || PUG_DEFAULTS.viewsDir);
25
+ }
@@ -14,6 +14,7 @@ export declare const HTTP_CODE_METADATA: {
14
14
  statusCode: string;
15
15
  path: string;
16
16
  };
17
+ export declare const RENDER_METADATA_KEY: unique symbol;
17
18
  export declare enum PARAMETER_TYPE {
18
19
  REQUEST = 0,
19
20
  RESPONSE = 1,
@@ -121,3 +121,14 @@ export declare const principal: () => ParameterDecorator;
121
121
  * @returns ParameterDecorator
122
122
  */
123
123
  export declare function params(type: PARAMETER_TYPE, parameterName?: string): ParameterDecorator;
124
+ /**
125
+ * Render decorator to define the template and default data for a route
126
+ * @param template The template to render
127
+ * @param defaultData The default data to pass to the template
128
+ * @returns
129
+ */
130
+ export declare function Render(template: string, defaultData?: Record<string, unknown>): MethodDecorator;
131
+ export declare function getRenderMetadata(target: object, propertyKey: string | symbol): {
132
+ template?: string;
133
+ defaultData?: Record<string, unknown>;
134
+ };
@@ -17,7 +17,8 @@ export type RenderOptions = {
17
17
  */
18
18
  export declare enum Engine {
19
19
  HBS = "hbs",
20
- EJS = "ejs"
20
+ EJS = "ejs",
21
+ PUG = "pug"
21
22
  }
22
23
  /**
23
24
  * The configuration options for the view engine.
@@ -8,9 +8,9 @@ import express from "express";
8
8
  *
9
9
  */
10
10
  export type HandlebarsOptions = {
11
- viewsDir?: string | Array<string>;
12
11
  viewEngine?: string;
13
- serverOptions?: unknown;
12
+ viewsDir?: string;
13
+ partialsDir?: string;
14
14
  };
15
15
  /**
16
16
  * Set Handlebars as the view engine
@@ -1,3 +1,4 @@
1
1
  export { EjsOptions as EJS } from "./ejs/ejs.config";
2
2
  export { HandlebarsOptions as HBS } from "./handlebars/hbs.config";
3
+ export { PugOptions as PUG } from "./pug/pug.config";
3
4
  export { Engine } from "./engine";
@@ -0,0 +1,17 @@
1
+ import { Application } from "express";
2
+ /**
3
+ * Pug options
4
+ * @typedef {Object} PugOptions
5
+ * @property {string} viewEngine - The view engine to be used
6
+ * @property {string} viewsDir - The path to the views folder
7
+ */
8
+ export type PugOptions = {
9
+ viewEngine?: string;
10
+ viewsDir?: string;
11
+ };
12
+ /**
13
+ * Set Pug as the view engine
14
+ * @param {express.Application} app - The express application
15
+ * @param {PugOptions} [options=PUG_DEFAULTS] - The pug options
16
+ */
17
+ export declare function setEnginePug(app: Application, options?: PugOptions): Promise<void>;
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/adapter-express",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@adapter-express)",
5
5
  "author": "",
6
6
  "main": "./lib/cjs/index.js",
@@ -84,11 +84,11 @@
84
84
  "@vitest/coverage-v8": "2.0.3",
85
85
  "eslint": "8.57.0",
86
86
  "eslint-config-prettier": "9.1.0",
87
- "husky": "9.0.11",
87
+ "husky": "9.1.1",
88
88
  "prettier": "3.3.3",
89
89
  "release-it": "17.6.0",
90
90
  "typescript": "5.5.3",
91
- "vite": "5.3.3",
91
+ "vite": "5.3.4",
92
92
  "vite-tsconfig-paths": "4.3.2",
93
93
  "vitest": "2.0.3"
94
94
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/adapter-express",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@adapter-express)",
5
5
  "author": "",
6
6
  "main": "./lib/cjs/index.js",
@@ -84,11 +84,11 @@
84
84
  "@vitest/coverage-v8": "2.0.3",
85
85
  "eslint": "8.57.0",
86
86
  "eslint-config-prettier": "9.1.0",
87
- "husky": "9.0.11",
87
+ "husky": "9.1.1",
88
88
  "prettier": "3.3.3",
89
89
  "release-it": "17.6.0",
90
90
  "typescript": "5.5.3",
91
- "vite": "5.3.3",
91
+ "vite": "5.3.4",
92
92
  "vite-tsconfig-paths": "4.3.2",
93
93
  "vitest": "2.0.3"
94
94
  },