From 2ff560243088539036c5b10f05f73782a52b8887 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Thu, 4 Dec 2025 22:15:26 +0000 Subject: [PATCH] fix(typedrouter): Use globalThis-backed globalHooks for TypedRouter to enable cross-bundle sharing; fix merging and clearing of global hooks. --- changelog.md | 7 +++++++ ts/00_commitinfo_data.ts | 2 +- ts/classes.typedrouter.ts | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index d53bfa2..93a4246 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-12-04 - 3.2.1 - fix(typedrouter) +Use globalThis-backed globalHooks for TypedRouter to enable cross-bundle sharing; fix merging and clearing of global hooks. + +- Replace static globalHooks field with getter/setter that stores hooks on globalThis so hooks are shared across bundles. +- Fix setGlobalHooks to merge new hooks with existing ones (avoiding accidental overwrite). +- Update clearGlobalHooks to clear the globalThis storage used for hooks. + ## 2025-12-04 - 3.2.0 - feat(typedrouter) Add request/response hooks and monitoring to TypedRouter; emit hooks from TypedRequest; improve VirtualStream encoding/decoding; re-export hook types diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 40d6423..3d8d396 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@api.global/typedrequest', - version: '3.2.0', + version: '3.2.1', description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.' } diff --git a/ts/classes.typedrouter.ts b/ts/classes.typedrouter.ts index 827d6d0..d5a2c7a 100644 --- a/ts/classes.typedrouter.ts +++ b/ts/classes.typedrouter.ts @@ -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)