fix(typedrouter): Use globalThis-backed globalHooks for TypedRouter to enable cross-bundle sharing; fix merging and clearing of global hooks.
This commit is contained in:
@@ -34,21 +34,32 @@ export interface ITypedRouterHooks {
|
||||
* This is thought for reusing the same url endpoint for different methods
|
||||
*/
|
||||
export class TypedRouter {
|
||||
// Static hooks for global traffic monitoring
|
||||
public static globalHooks: ITypedRouterHooks = {};
|
||||
// Use globalThis for cross-bundle hook sharing
|
||||
public static get globalHooks(): ITypedRouterHooks {
|
||||
if (!(globalThis as any).__typedRouterGlobalHooks) {
|
||||
(globalThis as any).__typedRouterGlobalHooks = {};
|
||||
}
|
||||
return (globalThis as any).__typedRouterGlobalHooks;
|
||||
}
|
||||
|
||||
public static set globalHooks(value: ITypedRouterHooks) {
|
||||
(globalThis as any).__typedRouterGlobalHooks = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set global hooks for monitoring all TypedRequest traffic
|
||||
* Hooks are shared across all bundles via globalThis
|
||||
*/
|
||||
public static setGlobalHooks(hooks: ITypedRouterHooks): void {
|
||||
TypedRouter.globalHooks = { ...TypedRouter.globalHooks, ...hooks };
|
||||
const current = TypedRouter.globalHooks;
|
||||
TypedRouter.globalHooks = { ...current, ...hooks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all global hooks
|
||||
*/
|
||||
public static clearGlobalHooks(): void {
|
||||
TypedRouter.globalHooks = {};
|
||||
(globalThis as any).__typedRouterGlobalHooks = {};
|
||||
}
|
||||
|
||||
// Instance-level hooks (for per-router monitoring)
|
||||
|
||||
Reference in New Issue
Block a user