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

feat: adds hasHook method to the trie router

e22m4u 2 недель назад
Родитель
Сommit
68a2f6c841
4 измененных файлов с 42 добавлено и 2 удалено
  1. 11 1
      dist/cjs/index.cjs
  2. 8 0
      src/trie-router.d.ts
  3. 12 1
      src/trie-router.js
  4. 11 0
      src/trie-router.spec.js

+ 11 - 1
dist/cjs/index.cjs

@@ -2273,7 +2273,7 @@ var _TrieRouter = class _TrieRouter extends DebuggableService {
    * );
    * ```
    *
-   * @param {string} type
+   * @param {RouterHookType} type
    * @param {Function} hook
    * @returns {this}
    */
@@ -2281,6 +2281,16 @@ var _TrieRouter = class _TrieRouter extends DebuggableService {
     this.getService(RouterHookRegistry).addHook(type, hook);
     return this;
   }
+  /**
+   * Has hook.
+   *
+   * @param {RouterHookType} type
+   * @param {Function} hook
+   * @returns {boolean}
+   */
+  hasHook(type, hook) {
+    return this.getService(RouterHookRegistry).hasHook(type, hook);
+  }
   /**
    * Add pre-handler hook.
    *

+ 8 - 0
src/trie-router.d.ts

@@ -109,6 +109,14 @@ export declare class TrieRouter extends DebuggableService {
    */
   addHook(type: RouterHookType, hook: RouterHook): this;
 
+  /**
+   * Has hook.
+   *
+   * @param type
+   * @param hook
+   */
+  hasHook(type: RouterHookType, hook: RouterHook): boolean;
+
   /**
    * Add pre-handler hook.
    *

+ 12 - 1
src/trie-router.js

@@ -228,7 +228,7 @@ export class TrieRouter extends DebuggableService {
    * );
    * ```
    *
-   * @param {string} type
+   * @param {RouterHookType} type
    * @param {Function} hook
    * @returns {this}
    */
@@ -237,6 +237,17 @@ export class TrieRouter extends DebuggableService {
     return this;
   }
 
+  /**
+   * Has hook.
+   *
+   * @param {RouterHookType} type
+   * @param {Function} hook
+   * @returns {boolean}
+   */
+  hasHook(type, hook) {
+    return this.getService(RouterHookRegistry).hasHook(type, hook);
+  }
+
   /**
    * Add pre-handler hook.
    *

+ 11 - 0
src/trie-router.spec.js

@@ -618,6 +618,17 @@ describe('TrieRouter', function () {
     });
   });
 
+  describe('hasHook', function () {
+    it('should return true if a given function is registered with the hook type', function () {
+      const router = new TrieRouter();
+      const type = RouterHookType.PRE_HANDLER;
+      const hook = () => undefined;
+      expect(router.hasHook(type, hook)).to.be.false;
+      router.addHook(type, hook);
+      expect(router.hasHook(type, hook)).to.be.true;
+    });
+  });
+
   describe('addPreHandler', function () {
     it('should add the given pre-handler hook to the RouterHookRegistry and returns itself', function () {
       const router = new TrieRouter();