Просмотр исходного кода

refactor: replaces native getter to functional getters

e22m4u 3 недель назад
Родитель
Сommit
57f533d666
5 измененных файлов с 89 добавлено и 73 удалено
  1. 18 19
      dist/cjs/index.cjs
  2. 1 1
      src/hooks/hook-invoker.js
  3. 7 7
      src/route/route.d.ts
  4. 19 20
      src/route/route.js
  5. 44 26
      src/route/route.spec.js

+ 18 - 19
dist/cjs/index.cjs

@@ -923,7 +923,7 @@ var _HookInvoker = class _HookInvoker extends DebuggableService {
     }
     }
     const hooks = [
     const hooks = [
       ...this.getService(HookRegistry).getHooks(hookType),
       ...this.getService(HookRegistry).getHooks(hookType),
-      ...route.hookRegistry.getHooks(hookType)
+      ...route.getHookRegistry().getHooks(hookType)
     ];
     ];
     let result = void 0;
     let result = void 0;
     for (let i = 0; i < hooks.length; i++) {
     for (let i = 0; i < hooks.length; i++) {
@@ -1045,19 +1045,33 @@ var HttpMethod = {
 };
 };
 var _Route = class _Route extends import_js_debug.Debuggable {
 var _Route = class _Route extends import_js_debug.Debuggable {
   /**
   /**
-   * Route definition.
+   * Definition.
    *
    *
    * @type {RouteDefinition}
    * @type {RouteDefinition}
    */
    */
   _definition;
   _definition;
   /**
   /**
-   * Getter of the route definition.
+   * Get definition.
    *
    *
    * @returns {RouteDefinition}
    * @returns {RouteDefinition}
    */
    */
-  get definition() {
+  getDefinition() {
     return this._definition;
     return this._definition;
   }
   }
+  /**
+   * Hook registry.
+   *
+   * @type {HookRegistry}
+   */
+  _hookRegistry = new HookRegistry();
+  /**
+   * Get hook registry.
+   *
+   * @returns {HookRegistry}
+   */
+  getHookRegistry() {
+    return this._hookRegistry;
+  }
   /**
   /**
    * Getter of the method.
    * Getter of the method.
    *
    *
@@ -1090,21 +1104,6 @@ var _Route = class _Route extends import_js_debug.Debuggable {
   get handler() {
   get handler() {
     return this._definition.handler;
     return this._definition.handler;
   }
   }
-  /**
-   * Hook registry.
-   *
-   * @type {HookRegistry}
-   * @private
-   */
-  _hookRegistry = new HookRegistry();
-  /**
-   * Getter of the hook registry.
-   *
-   * @returns {HookRegistry}
-   */
-  get hookRegistry() {
-    return this._hookRegistry;
-  }
   /**
   /**
    * Constructor.
    * Constructor.
    *
    *

+ 1 - 1
src/hooks/hook-invoker.js

@@ -63,7 +63,7 @@ export class HookInvoker extends DebuggableService {
     // их в данной последовательности
     // их в данной последовательности
     const hooks = [
     const hooks = [
       ...this.getService(HookRegistry).getHooks(hookType),
       ...this.getService(HookRegistry).getHooks(hookType),
-      ...route.hookRegistry.getHooks(hookType),
+      ...route.getHookRegistry().getHooks(hookType),
     ];
     ];
     let result = undefined;
     let result = undefined;
     // итерация по хукам выполняется по индексу,
     // итерация по хукам выполняется по индексу,

+ 7 - 7
src/route/route.d.ts

@@ -60,9 +60,14 @@ export interface RouteDefinition {
  */
  */
 export declare class Route {
 export declare class Route {
   /**
   /**
-   * Route definition.
+   * Get definition.
    */
    */
-  get definition(): RouteDefinition;
+  getDefinition(): RouteDefinition;
+
+  /**
+   * Get hook registry.
+   */
+  getHookRegistry(): HookRegistry;
 
 
   /**
   /**
    * Method.
    * Method.
@@ -84,11 +89,6 @@ export declare class Route {
    */
    */
   get handler(): RouteHandler;
   get handler(): RouteHandler;
 
 
-  /**
-   * Hook registry.
-   */
-  get hookRegistry(): HookRegistry;
-
   /**
   /**
    * Constructor.
    * Constructor.
    *
    *

+ 19 - 20
src/route/route.js

@@ -43,21 +43,37 @@ export const HttpMethod = {
  */
  */
 export class Route extends Debuggable {
 export class Route extends Debuggable {
   /**
   /**
-   * Route definition.
+   * Definition.
    *
    *
    * @type {RouteDefinition}
    * @type {RouteDefinition}
    */
    */
   _definition;
   _definition;
 
 
   /**
   /**
-   * Getter of the route definition.
+   * Get definition.
    *
    *
    * @returns {RouteDefinition}
    * @returns {RouteDefinition}
    */
    */
-  get definition() {
+  getDefinition() {
     return this._definition;
     return this._definition;
   }
   }
 
 
+  /**
+   * Hook registry.
+   *
+   * @type {HookRegistry}
+   */
+  _hookRegistry = new HookRegistry();
+
+  /**
+   * Get hook registry.
+   *
+   * @returns {HookRegistry}
+   */
+  getHookRegistry() {
+    return this._hookRegistry;
+  }
+
   /**
   /**
    * Getter of the method.
    * Getter of the method.
    *
    *
@@ -94,23 +110,6 @@ export class Route extends Debuggable {
     return this._definition.handler;
     return this._definition.handler;
   }
   }
 
 
-  /**
-   * Hook registry.
-   *
-   * @type {HookRegistry}
-   * @private
-   */
-  _hookRegistry = new HookRegistry();
-
-  /**
-   * Getter of the hook registry.
-   *
-   * @returns {HookRegistry}
-   */
-  get hookRegistry() {
-    return this._hookRegistry;
-  }
-
   /**
   /**
    * Constructor.
    * Constructor.
    *
    *

+ 44 - 26
src/route/route.spec.js

@@ -2,9 +2,9 @@ import {expect} from 'chai';
 import {format} from '@e22m4u/js-format';
 import {format} from '@e22m4u/js-format';
 import {ROOT_PATH} from '../constants.js';
 import {ROOT_PATH} from '../constants.js';
 import {Route, HttpMethod} from './route.js';
 import {Route, HttpMethod} from './route.js';
-import {RouterHookType} from '../hooks/index.js';
 import {ServiceContainer} from '@e22m4u/js-service';
 import {ServiceContainer} from '@e22m4u/js-service';
 import {RequestContext} from '../request-context.js';
 import {RequestContext} from '../request-context.js';
+import {HookRegistry, RouterHookType} from '../hooks/index.js';
 import {createRequestMock, createResponseMock} from '../utils/index.js';
 import {createRequestMock, createResponseMock} from '../utils/index.js';
 
 
 describe('Route', function () {
 describe('Route', function () {
@@ -30,17 +30,6 @@ describe('Route', function () {
       })();
       })();
     });
     });
 
 
-    it('should set a given definition to the "definition" property', function () {
-      const definition = {
-        method: HttpMethod.GET,
-        path: ROOT_PATH,
-        handler: () => undefined,
-        meta: {foo: 'bar'},
-      };
-      const route = new Route(definition);
-      expect(route.definition).to.be.eql(definition);
-    });
-
     describe('the "method" option', function () {
     describe('the "method" option', function () {
       it('should require the "method" option to be a non-empty String', function () {
       it('should require the "method" option to be a non-empty String', function () {
         const throwable = v => () =>
         const throwable = v => () =>
@@ -207,8 +196,9 @@ describe('Route', function () {
           preHandler: value,
           preHandler: value,
           handler: () => undefined,
           handler: () => undefined,
         });
         });
-        expect(route.hookRegistry.hasHook(RouterHookType.PRE_HANDLER, value)).to
-          .be.true;
+        const hookReg = route.getHookRegistry();
+        const res = hookReg.hasHook(RouterHookType.PRE_HANDLER, value);
+        expect(res).to.be.true;
       });
       });
 
 
       it('should add a Function Array to "preHandler" hooks', function () {
       it('should add a Function Array to "preHandler" hooks', function () {
@@ -219,10 +209,11 @@ describe('Route', function () {
           preHandler: value,
           preHandler: value,
           handler: () => undefined,
           handler: () => undefined,
         });
         });
-        expect(route.hookRegistry.hasHook(RouterHookType.PRE_HANDLER, value[0]))
-          .to.be.true;
-        expect(route.hookRegistry.hasHook(RouterHookType.PRE_HANDLER, value[1]))
-          .to.be.true;
+        const hookReg = route.getHookRegistry();
+        const res1 = hookReg.hasHook(RouterHookType.PRE_HANDLER, value[0]);
+        const res2 = hookReg.hasHook(RouterHookType.PRE_HANDLER, value[1]);
+        expect(res1).to.be.true;
+        expect(res2).to.be.true;
       });
       });
     });
     });
 
 
@@ -285,8 +276,9 @@ describe('Route', function () {
           handler: () => undefined,
           handler: () => undefined,
           postHandler: value,
           postHandler: value,
         });
         });
-        expect(route.hookRegistry.hasHook(RouterHookType.POST_HANDLER, value))
-          .to.be.true;
+        const hookReg = route.getHookRegistry();
+        const res = hookReg.hasHook(RouterHookType.POST_HANDLER, value);
+        expect(res).to.be.true;
       });
       });
 
 
       it('should add a Function Array to "postHandler" hooks', function () {
       it('should add a Function Array to "postHandler" hooks', function () {
@@ -297,12 +289,11 @@ describe('Route', function () {
           handler: () => undefined,
           handler: () => undefined,
           postHandler: value,
           postHandler: value,
         });
         });
-        expect(
-          route.hookRegistry.hasHook(RouterHookType.POST_HANDLER, value[0]),
-        ).to.be.true;
-        expect(
-          route.hookRegistry.hasHook(RouterHookType.POST_HANDLER, value[1]),
-        ).to.be.true;
+        const hookReg = route.getHookRegistry();
+        const res1 = hookReg.hasHook(RouterHookType.POST_HANDLER, value[0]);
+        const res2 = hookReg.hasHook(RouterHookType.POST_HANDLER, value[1]);
+        expect(res1).to.be.true;
+        expect(res2).to.be.true;
       });
       });
     });
     });
 
 
@@ -368,6 +359,33 @@ describe('Route', function () {
     });
     });
   });
   });
 
 
+  describe('getDefinition', function () {
+    it('should return a route definition that was provided to the constructor', function () {
+      const definition = {
+        method: HttpMethod.GET,
+        path: '/myPath',
+        handler: () => undefined,
+        meta: {foo: 'bar'},
+      };
+      const route = new Route(definition);
+      expect(route.getDefinition()).to.be.eql(definition);
+    });
+  });
+
+  describe('getHookRegistry', function () {
+    it('should return a hook registry of the current instance', function () {
+      const route = new Route({
+        method: HttpMethod.GET,
+        path: ROOT_PATH,
+        handler: () => undefined,
+      });
+      const res1 = route.getHookRegistry();
+      const res2 = route.getHookRegistry();
+      expect(res1).to.be.instanceOf(HookRegistry);
+      expect(res2).to.be.eq(res1);
+    });
+  });
+
   describe('handle', function () {
   describe('handle', function () {
     it('should invoke the handler with the given RequestContext and return its result', function () {
     it('should invoke the handler with the given RequestContext and return its result', function () {
       const route = new Route({
       const route = new Route({