e22m4u 1 месяц назад
Родитель
Сommit
99d4576b99
2 измененных файлов с 51 добавлено и 3 удалено
  1. 28 1
      src/hooks/hook-registry.d.ts
  2. 23 2
      src/trie-router.d.ts

+ 28 - 1
src/hooks/hook-registry.d.ts

@@ -1,4 +1,5 @@
 import {Callable} from '../types.js';
+import {RequestContext} from '../request-context.js';
 import {DebuggableService} from '../debuggable-service.js';
 
 /**
@@ -17,12 +18,38 @@ export type HookType = (typeof HookType)[keyof typeof HookType];
 /**
  * Router hook.
  */
-export type RouterHook<T = unknown> = Callable<T>;
+export type RouterHook = Callable;
+
+/**
+ * Pre handler hook.
+ */
+export type PreHandlerHook = (ctx: RequestContext) => unknown;
+
+/**
+ * Post handler hook.
+ */
+export type PostHandlerHook = (ctx: RequestContext, data: unknown) => unknown;
 
 /**
  * Hook registry.
  */
 export declare class HookRegistry extends DebuggableService {
+  /**
+   * Add hook.
+   *
+   * @param type
+   * @param hook
+   */
+  addHook(type: typeof HookType.PRE_HANDLER, hook: PreHandlerHook): this;
+
+  /**
+   * Add hook.
+   *
+   * @param type
+   * @param hook
+   */
+  addHook(type: typeof HookType.POST_HANDLER, hook: PostHandlerHook): this;
+
   /**
    * Add hook.
    *

+ 23 - 2
src/trie-router.d.ts

@@ -1,10 +1,15 @@
 import {Route} from './route.js';
 import {RequestListener} from 'http';
-import {HookType} from './hooks/index.js';
 import {RouteDefinition} from './route.js';
-import {RouterHook} from './hooks/index.js';
 import {DebuggableService} from './debuggable-service.js';
 
+import {
+  HookType,
+  RouterHook,
+  PostHandlerHook,
+  PreHandlerHook,
+} from './hooks/index.js';
+
 /**
  * Trie router.
  */
@@ -56,6 +61,22 @@ export declare class TrieRouter extends DebuggableService {
    */
   get requestListener(): RequestListener;
 
+  /**
+   * Add hook.
+   *
+   * @param type
+   * @param hook
+   */
+  addHook(type: typeof HookType.PRE_HANDLER, hook: PreHandlerHook): this;
+
+  /**
+   * Add hook.
+   *
+   * @param type
+   * @param hook
+   */
+  addHook(type: typeof HookType.POST_HANDLER, hook: PostHandlerHook): this;
+
   /**
    * Add hook.
    *