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

refactor: replaces functional getter to native getters

e22m4u 3 недель назад
Родитель
Сommit
394f4aadfb

+ 8 - 7
dist/cjs/index.cjs

@@ -1968,7 +1968,7 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    *
    *
    * @type {TrieRouter}
    * @type {TrieRouter}
    */
    */
-  getRouter() {
+  get router() {
     return this._router;
     return this._router;
   }
   }
   /**
   /**
@@ -1982,8 +1982,8 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    *
    *
    * @type {RouterBranchDefinition}
    * @type {RouterBranchDefinition}
    */
    */
-  getDefinition() {
-    return { ...this._definition };
+  get definition() {
+    return this._definition;
   }
   }
   /**
   /**
    * Parent branch.
    * Parent branch.
@@ -1996,7 +1996,7 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    *
    *
    * @returns {RouterBranch|undefined}
    * @returns {RouterBranch|undefined}
    */
    */
-  getParentBranch() {
+  get parentBranch() {
     return this._parentBranch;
     return this._parentBranch;
   }
   }
   /**
   /**
@@ -2023,13 +2023,14 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
     }
     }
     this._parentBranch = parentBranch;
     this._parentBranch = parentBranch;
     if (parentBranch) {
     if (parentBranch) {
-      this._definition = mergeRouterBranchDefinitions(
-        parentBranch.getDefinition(),
+      const mergedDef = mergeRouterBranchDefinitions(
+        parentBranch.definition,
         branchDef
         branchDef
       );
       );
+      this._definition = cloneDeep(mergedDef);
     } else {
     } else {
       validateRouterBranchDefinition(branchDef);
       validateRouterBranchDefinition(branchDef);
-      this._definition = branchDef;
+      this._definition = cloneDeep(branchDef);
     }
     }
     this.ctorDebug("Branch %v created.", normalizePath(branchDef.path, true));
     this.ctorDebug("Branch %v created.", normalizePath(branchDef.path, true));
     this.ctorDebug("Branch path was %v.", this._definition.path);
     this.ctorDebug("Branch path was %v.", this._definition.path);

+ 7 - 9
src/branch/router-branch.d.ts

@@ -13,12 +13,12 @@ import {
 /**
 /**
  * Router branch definition.
  * Router branch definition.
  */
  */
-export type RouterBranchDefinition = {
+export interface RouterBranchDefinition {
   path: string;
   path: string;
   preHandler?: RoutePreHandler | RoutePreHandler[];
   preHandler?: RoutePreHandler | RoutePreHandler[];
   postHandler?: RoutePostHandler | RoutePostHandler[];
   postHandler?: RoutePostHandler | RoutePostHandler[];
   meta?: RouteMeta;
   meta?: RouteMeta;
-};
+}
 
 
 /**
 /**
  * Router branch.
  * Router branch.
@@ -27,28 +27,26 @@ export declare class RouterBranch extends DebuggableService {
   /**
   /**
    * Get router.
    * Get router.
    */
    */
-  getRouter(): TrieRouter;
+  get router(): TrieRouter;
 
 
   /**
   /**
-   * Get parent branch.
+   * Get definition.
    */
    */
-  getParentBranch(): RouterBranch | undefined;
+  get definition(): RouterBranchDefinition;
 
 
   /**
   /**
-   * Get definition.
+   * Get parent branch.
    */
    */
-  getDefinition(): RouterBranchDefinition;
+  get parentBranch(): RouterBranch | undefined;
 
 
   /**
   /**
    * Constructor.
    * Constructor.
    *
    *
-   * @param container
    * @param router
    * @param router
    * @param branchDef
    * @param branchDef
    * @param parentBranch
    * @param parentBranch
    */
    */
   constructor(
   constructor(
-    container: ServiceContainer,
     router: TrieRouter,
     router: TrieRouter,
     branchDef: RouterBranchDefinition,
     branchDef: RouterBranchDefinition,
     parentBranch?: RouterBranch,
     parentBranch?: RouterBranch,

+ 9 - 7
src/branch/router-branch.js

@@ -1,4 +1,5 @@
 import {Route} from '../route/index.js';
 import {Route} from '../route/index.js';
+import {cloneDeep} from '../utils/index.js';
 import {TrieRouter} from '../trie-router.js';
 import {TrieRouter} from '../trie-router.js';
 import {InvalidArgumentError} from '@e22m4u/js-format';
 import {InvalidArgumentError} from '@e22m4u/js-format';
 import {normalizePath} from '../utils/normalize-path.js';
 import {normalizePath} from '../utils/normalize-path.js';
@@ -35,7 +36,7 @@ export class RouterBranch extends DebuggableService {
    *
    *
    * @type {TrieRouter}
    * @type {TrieRouter}
    */
    */
-  getRouter() {
+  get router() {
     return this._router;
     return this._router;
   }
   }
 
 
@@ -51,8 +52,8 @@ export class RouterBranch extends DebuggableService {
    *
    *
    * @type {RouterBranchDefinition}
    * @type {RouterBranchDefinition}
    */
    */
-  getDefinition() {
-    return {...this._definition};
+  get definition() {
+    return this._definition;
   }
   }
 
 
   /**
   /**
@@ -67,7 +68,7 @@ export class RouterBranch extends DebuggableService {
    *
    *
    * @returns {RouterBranch|undefined}
    * @returns {RouterBranch|undefined}
    */
    */
-  getParentBranch() {
+  get parentBranch() {
     return this._parentBranch;
     return this._parentBranch;
   }
   }
 
 
@@ -96,13 +97,14 @@ export class RouterBranch extends DebuggableService {
     }
     }
     this._parentBranch = parentBranch;
     this._parentBranch = parentBranch;
     if (parentBranch) {
     if (parentBranch) {
-      this._definition = mergeRouterBranchDefinitions(
-        parentBranch.getDefinition(),
+      const mergedDef = mergeRouterBranchDefinitions(
+        parentBranch.definition,
         branchDef,
         branchDef,
       );
       );
+      this._definition = cloneDeep(mergedDef);
     } else {
     } else {
       validateRouterBranchDefinition(branchDef);
       validateRouterBranchDefinition(branchDef);
-      this._definition = branchDef;
+      this._definition = cloneDeep(branchDef);
     }
     }
     this.ctorDebug('Branch %v created.', normalizePath(branchDef.path, true));
     this.ctorDebug('Branch %v created.', normalizePath(branchDef.path, true));
     this.ctorDebug('Branch path was %v.', this._definition.path);
     this.ctorDebug('Branch path was %v.', this._definition.path);

+ 9 - 14
src/branch/router-branch.spec.js

@@ -16,33 +16,28 @@ describe('RouterBranch', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const parent = router.createBranch({path: 'foo'});
       const parent = router.createBranch({path: 'foo'});
       const S = new RouterBranch(router, {path: 'bar'}, parent);
       const S = new RouterBranch(router, {path: 'bar'}, parent);
-      expect(S.getDefinition().path).to.be.eq('/foo/bar');
+      expect(S.definition.path).to.be.eq('/foo/bar');
     });
     });
-  });
 
 
-  describe('getRouter', function () {
-    it('should return the router instance that was provided to the constructor', function () {
+    it('should set a given router to the "router" property', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const S = new RouterBranch(router, {path: ROOT_PATH});
       const S = new RouterBranch(router, {path: ROOT_PATH});
-      expect(S.getRouter()).to.be.eq(router);
+      expect(S.router).to.be.eq(router);
     });
     });
-  });
 
 
-  describe('getDefinition', function () {
-    it('should return the branch definition that was provided to the constructor', function () {
+    it('should set a given definition to the "definition" property as a copy', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const branchDef = {path: ROOT_PATH};
       const branchDef = {path: ROOT_PATH};
       const S = new RouterBranch(router, branchDef);
       const S = new RouterBranch(router, branchDef);
-      expect(S.getDefinition()).to.be.eql(branchDef);
+      expect(S.definition).to.be.eql(branchDef);
+      expect(S.definition).to.be.not.eq(branchDef);
     });
     });
-  });
 
 
-  describe('getParentBranch', function () {
-    it('should return the parent branch that was provided to the constructor', function () {
+    it('should set a parent branch to the "parentBranch" property', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const parent = router.createBranch({path: ROOT_PATH});
       const parent = router.createBranch({path: ROOT_PATH});
       const S = new RouterBranch(router, {path: ROOT_PATH}, parent);
       const S = new RouterBranch(router, {path: ROOT_PATH}, parent);
-      expect(S.getParentBranch()).to.be.eq(parent);
+      expect(S.parentBranch).to.be.eq(parent);
     });
     });
   });
   });
 
 
@@ -82,7 +77,7 @@ describe('RouterBranch', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const S = new RouterBranch(router, {path: 'foo'});
       const S = new RouterBranch(router, {path: 'foo'});
       const res = S.createBranch({path: 'bar'});
       const res = S.createBranch({path: 'bar'});
-      expect(res.getDefinition().path).to.be.eq('/foo/bar');
+      expect(res.definition.path).to.be.eq('/foo/bar');
     });
     });
   });
   });
 });
 });

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

@@ -46,14 +46,14 @@ export type RouteMeta = {
 /**
 /**
  * Route definition.
  * Route definition.
  */
  */
-export type RouteDefinition = {
+export interface RouteDefinition {
   method: string;
   method: string;
   path: string;
   path: string;
   handler: RouteHandler;
   handler: RouteHandler;
   preHandler?: RoutePreHandler | RoutePreHandler[];
   preHandler?: RoutePreHandler | RoutePreHandler[];
   postHandler?: RoutePostHandler | RoutePostHandler[];
   postHandler?: RoutePostHandler | RoutePostHandler[];
   meta?: RouteMeta;
   meta?: RouteMeta;
-};
+}
 
 
 /**
 /**
  * Route.
  * Route.

+ 1 - 1
src/trie-router.spec.js

@@ -34,7 +34,7 @@ describe('TrieRouter', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const branchDef = {path: 'foo'};
       const branchDef = {path: 'foo'};
       const res = router.createBranch(branchDef);
       const res = router.createBranch(branchDef);
-      expect(res.getDefinition().path).to.be.eq(branchDef.path);
+      expect(res.definition.path).to.be.eq(branchDef.path);
     });
     });
   });
   });