Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a89e88dd1 | |||
| 2ff5602430 | |||
| 5444e1be88 | |||
| 94ba38b4d4 |
18
changelog.md
18
changelog.md
@@ -1,5 +1,23 @@
|
||||
# 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
|
||||
|
||||
- Introduce ITypedRequestLogEntry and ITypedRouterHooks interfaces to represent structured traffic log entries and hook callbacks.
|
||||
- Add static globalHooks on TypedRouter with helper APIs TypedRouter.setGlobalHooks and TypedRouter.clearGlobalHooks for global traffic monitoring.
|
||||
- Add instance-level hooks on TypedRouter (setHooks) and a unified callHook() that invokes both global and instance hooks safely (errors are caught and logged).
|
||||
- TypedRequest now emits onOutgoingRequest before sending and onIncomingResponse after receiving, including timestamps, duration and payload/error details.
|
||||
- TypedRouter now emits lifecycle hooks while routing: onIncomingRequest when a request arrives, onOutgoingResponse for responses (both success and handler-missing error cases), and onIncomingResponse when responses arrive to be fulfilled.
|
||||
- VirtualStream.encodePayloadForNetwork and decodePayloadFromNetwork were enhanced to recurse into arrays and nested objects (preserving special built-ins) to correctly handle embedded virtual streams.
|
||||
- Re-export ITypedRequestLogEntry and ITypedRouterHooks from the package index for external consumption.
|
||||
|
||||
## 2025-12-03 - 3.1.11 - fix(virtualstream)
|
||||
Expose transport localData to handlers via TypedTools; improve VirtualStream payload encode/decode to preserve built-ins and handle nested arrays/objects
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@api.global/typedrequest",
|
||||
"version": "3.1.11",
|
||||
"version": "3.2.1",
|
||||
"private": false,
|
||||
"description": "A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.",
|
||||
"main": "dist_ts/index.js",
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedrequest',
|
||||
version: '3.1.11',
|
||||
version: '3.2.1',
|
||||
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
|
||||
}
|
||||
|
||||
@@ -1,11 +1,25 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { VirtualStream } from './classes.virtualstream.js';
|
||||
import { TypedResponseError } from './classes.typedresponseerror.js';
|
||||
import { TypedRouter } from './classes.typedrouter.js';
|
||||
import { TypedRouter, type ITypedRequestLogEntry } from './classes.typedrouter.js';
|
||||
import { TypedTarget } from './classes.typedtarget.js';
|
||||
|
||||
const webrequestInstance = new plugins.webrequest.WebRequest();
|
||||
|
||||
/**
|
||||
* Helper to call global hooks from TypedRequest
|
||||
*/
|
||||
function callGlobalHook(
|
||||
hookName: keyof typeof TypedRouter.globalHooks,
|
||||
entry: ITypedRequestLogEntry
|
||||
): void {
|
||||
try {
|
||||
TypedRouter.globalHooks[hookName]?.(entry);
|
||||
} catch (err) {
|
||||
console.error(`TypedRequest hook error (${hookName}):`, err);
|
||||
}
|
||||
}
|
||||
|
||||
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
/**
|
||||
* in case we post against a url endpoint
|
||||
@@ -36,6 +50,8 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
* fires the request
|
||||
*/
|
||||
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
|
||||
const requestStartTime = Date.now();
|
||||
|
||||
let payloadSending: plugins.typedRequestInterfaces.ITypedRequest = {
|
||||
method: this.method,
|
||||
request: fireArg,
|
||||
@@ -53,6 +69,16 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
}
|
||||
});
|
||||
|
||||
// Hook: outgoing request
|
||||
callGlobalHook('onOutgoingRequest', {
|
||||
correlationId: payloadSending.correlation.id,
|
||||
method: this.method,
|
||||
direction: 'outgoing',
|
||||
phase: 'request',
|
||||
timestamp: requestStartTime,
|
||||
payload: fireArg,
|
||||
});
|
||||
|
||||
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
payloadReceiving = await this.postTrObject(payloadSending, useCacheArg);
|
||||
|
||||
@@ -62,6 +88,19 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
|
||||
}
|
||||
});
|
||||
|
||||
// Hook: incoming response (for this outgoing request)
|
||||
callGlobalHook('onIncomingResponse', {
|
||||
correlationId: payloadSending.correlation.id,
|
||||
method: this.method,
|
||||
direction: 'incoming',
|
||||
phase: 'response',
|
||||
timestamp: Date.now(),
|
||||
durationMs: Date.now() - requestStartTime,
|
||||
payload: payloadReceiving?.response,
|
||||
error: payloadReceiving?.error?.text,
|
||||
});
|
||||
|
||||
return payloadReceiving.response;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,91 @@ import { VirtualStream } from './classes.virtualstream.js';
|
||||
import { TypedHandler } from './classes.typedhandler.js';
|
||||
import { TypedRequest } from './classes.typedrequest.js';
|
||||
|
||||
/**
|
||||
* Log entry for TypedRequest traffic monitoring
|
||||
*/
|
||||
export interface ITypedRequestLogEntry {
|
||||
correlationId: string;
|
||||
method: string;
|
||||
direction: 'outgoing' | 'incoming';
|
||||
phase: 'request' | 'response';
|
||||
timestamp: number;
|
||||
durationMs?: number;
|
||||
payload: any;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks for intercepting TypedRequest traffic
|
||||
*/
|
||||
export interface ITypedRouterHooks {
|
||||
onOutgoingRequest?: (entry: ITypedRequestLogEntry) => void;
|
||||
onIncomingResponse?: (entry: ITypedRequestLogEntry) => void;
|
||||
onIncomingRequest?: (entry: ITypedRequestLogEntry) => void;
|
||||
onOutgoingResponse?: (entry: ITypedRequestLogEntry) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A typed router decides on which typed handler to call based on the method
|
||||
* specified in the typed request
|
||||
* This is thought for reusing the same url endpoint for different methods
|
||||
*/
|
||||
export class TypedRouter {
|
||||
// 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 {
|
||||
const current = TypedRouter.globalHooks;
|
||||
TypedRouter.globalHooks = { ...current, ...hooks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all global hooks
|
||||
*/
|
||||
public static clearGlobalHooks(): void {
|
||||
(globalThis as any).__typedRouterGlobalHooks = {};
|
||||
}
|
||||
|
||||
// Instance-level hooks (for per-router monitoring)
|
||||
public hooks: ITypedRouterHooks = {};
|
||||
|
||||
/**
|
||||
* Set instance-level hooks for monitoring traffic through this router
|
||||
*/
|
||||
public setHooks(hooks: ITypedRouterHooks): void {
|
||||
this.hooks = { ...this.hooks, ...hooks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to call both global and instance hooks
|
||||
*/
|
||||
private callHook(
|
||||
hookName: keyof ITypedRouterHooks,
|
||||
entry: ITypedRequestLogEntry
|
||||
): void {
|
||||
try {
|
||||
// Call global hooks
|
||||
TypedRouter.globalHooks[hookName]?.(entry);
|
||||
// Call instance hooks
|
||||
this.hooks[hookName]?.(entry);
|
||||
} catch (err) {
|
||||
console.error(`TypedRouter hook error (${hookName}):`, err);
|
||||
}
|
||||
}
|
||||
|
||||
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
|
||||
public handlerMap = new plugins.lik.ObjectMap<
|
||||
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
|
||||
@@ -109,6 +188,18 @@ export class TypedRouter {
|
||||
|
||||
// lets do normal routing
|
||||
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
|
||||
const requestStartTime = Date.now();
|
||||
|
||||
// Hook: incoming request
|
||||
this.callHook('onIncomingRequest', {
|
||||
correlationId: typedRequestArg.correlation?.id || 'unknown',
|
||||
method: typedRequestArg.method,
|
||||
direction: 'incoming',
|
||||
phase: 'request',
|
||||
timestamp: requestStartTime,
|
||||
payload: typedRequestArg.request,
|
||||
});
|
||||
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
@@ -124,6 +215,19 @@ export class TypedRouter {
|
||||
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
|
||||
// Hook: outgoing response (error - no handler)
|
||||
this.callHook('onOutgoingResponse', {
|
||||
correlationId: typedRequestArg.correlation?.id || 'unknown',
|
||||
method: typedRequestArg.method,
|
||||
direction: 'outgoing',
|
||||
phase: 'response',
|
||||
timestamp: Date.now(),
|
||||
durationMs: Date.now() - requestStartTime,
|
||||
payload: typedRequestArg.response,
|
||||
error: typedRequestArg.error?.text,
|
||||
});
|
||||
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
||||
@@ -133,8 +237,32 @@ export class TypedRouter {
|
||||
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
|
||||
// Hook: outgoing response (success)
|
||||
this.callHook('onOutgoingResponse', {
|
||||
correlationId: typedRequestArg.correlation?.id || 'unknown',
|
||||
method: typedRequestArg.method,
|
||||
direction: 'outgoing',
|
||||
phase: 'response',
|
||||
timestamp: Date.now(),
|
||||
durationMs: Date.now() - requestStartTime,
|
||||
payload: typedRequestArg.response,
|
||||
error: typedRequestArg.error?.text,
|
||||
});
|
||||
|
||||
return typedRequestArg;
|
||||
} else if (typedRequestArg?.correlation?.phase === 'response') {
|
||||
// Hook: incoming response
|
||||
this.callHook('onIncomingResponse', {
|
||||
correlationId: typedRequestArg.correlation?.id || 'unknown',
|
||||
method: typedRequestArg.method,
|
||||
direction: 'incoming',
|
||||
phase: 'response',
|
||||
timestamp: Date.now(),
|
||||
payload: typedRequestArg.response,
|
||||
error: typedRequestArg.error?.text,
|
||||
});
|
||||
|
||||
this.fireEventInterestMap
|
||||
.findInterest(typedRequestArg.correlation.id)
|
||||
?.fullfillInterest(typedRequestArg);
|
||||
|
||||
@@ -3,4 +3,7 @@ export * from './classes.typedhandler.js';
|
||||
export * from './classes.typedrouter.js';
|
||||
export * from './classes.typedresponseerror.js';
|
||||
export * from './classes.typedtarget.js';
|
||||
export * from './classes.virtualstream.js';
|
||||
export * from './classes.virtualstream.js';
|
||||
|
||||
// Re-export hook interfaces from typedrouter
|
||||
export type { ITypedRequestLogEntry, ITypedRouterHooks } from './classes.typedrouter.js';
|
||||
Reference in New Issue
Block a user