Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 224dfd7520 | |||
| 271f6bc0a3 | |||
| 472d009de1 | |||
| 0d3d5cb562 | |||
| 2a89e88dd1 | |||
| 2ff5602430 |
21
changelog.md
21
changelog.md
@@ -1,5 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-12-04 - 4.0.0 - BREAKING CHANGE(typedrouter)
|
||||
Introduce options object for TypedRouter.routeAndAddResponse (localRequest, skipHooks); add defaultRouteOptions and make hook calls respect skipHooks; bump package version to 3.2.3
|
||||
|
||||
- Changed TypedRouter.routeAndAddResponse signature to accept an options object ({ localRequest?: boolean; skipHooks?: boolean }) instead of a boolean second argument — this is a breaking API change.
|
||||
- Added TypedRouter.defaultRouteOptions with defaults { localRequest: false, skipHooks: false }.
|
||||
- Routing now respects options.localRequest and options.skipHooks; hook calls (onIncomingRequest, onOutgoingResponse, onIncomingResponse) are skipped when skipHooks is true to avoid hook recursion or duplicate handling (useful for broadcast-received messages).
|
||||
- Bumped package.json version to 3.2.3.
|
||||
|
||||
## 2025-12-04 - 3.2.2 - fix(typedrequest)
|
||||
Add skipHooks flag to TypedRequest to optionally suppress global hooks for internal requests
|
||||
|
||||
- Introduce public skipHooks boolean on TypedRequest (default false) with documentation comment explaining it should be used for internal/logging requests to prevent infinite loops.
|
||||
- Guard calls to global hooks (onOutgoingRequest and onIncomingResponse) in TypedRequest.fire() so hooks are not invoked when skipHooks is true.
|
||||
|
||||
## 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
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@api.global/typedrequest",
|
||||
"version": "3.2.0",
|
||||
"version": "4.0.0",
|
||||
"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.2.0',
|
||||
version: '4.0.0',
|
||||
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
|
||||
public method: string;
|
||||
|
||||
/**
|
||||
* When true, hooks will not be called for this request.
|
||||
* Use this for internal/logging requests to prevent infinite loops.
|
||||
*/
|
||||
public skipHooks: boolean = false;
|
||||
|
||||
/**
|
||||
* @param postEndPointArg
|
||||
* @param methodArg
|
||||
@@ -69,15 +75,17 @@ 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,
|
||||
});
|
||||
// Hook: outgoing request (skip if this is an internal request)
|
||||
if (!this.skipHooks) {
|
||||
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);
|
||||
@@ -89,17 +97,19 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
}
|
||||
});
|
||||
|
||||
// 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,
|
||||
});
|
||||
// Hook: incoming response (skip if this is an internal request)
|
||||
if (!this.skipHooks) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -151,14 +162,26 @@ export class TypedRouter {
|
||||
return typedHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for routeAndAddResponse
|
||||
*/
|
||||
public static defaultRouteOptions = {
|
||||
localRequest: false,
|
||||
skipHooks: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* if typedrequest object has correlation.phase === 'request' -> routes a typed request object to a handler
|
||||
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
|
||||
* @param typedRequestArg
|
||||
* @param optionsArg - Options object with:
|
||||
* - localRequest: treat as local request (default: false)
|
||||
* - skipHooks: skip calling hooks for this routing (default: false, use for broadcast-received messages)
|
||||
*/
|
||||
public async routeAndAddResponse<
|
||||
T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest
|
||||
>(typedRequestArg: T, localRequestArg = false): Promise<T> {
|
||||
>(typedRequestArg: T, optionsArg: { localRequest?: boolean; skipHooks?: boolean } = {}): Promise<T> {
|
||||
const options = { ...TypedRouter.defaultRouteOptions, ...optionsArg };
|
||||
// decoding first
|
||||
typedRequestArg = VirtualStream.decodePayloadFromNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
@@ -176,18 +199,20 @@ export class TypedRouter {
|
||||
}
|
||||
|
||||
// lets do normal routing
|
||||
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
|
||||
if (typedRequestArg?.correlation?.phase === 'request' || options.localRequest) {
|
||||
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,
|
||||
});
|
||||
// Hook: incoming request (skip if routing broadcast-received messages)
|
||||
if (!options.skipHooks) {
|
||||
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);
|
||||
|
||||
@@ -206,16 +231,18 @@ export class TypedRouter {
|
||||
});
|
||||
|
||||
// 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,
|
||||
});
|
||||
if (!options.skipHooks) {
|
||||
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;
|
||||
}
|
||||
@@ -228,29 +255,33 @@ export class TypedRouter {
|
||||
});
|
||||
|
||||
// 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,
|
||||
});
|
||||
if (!options.skipHooks) {
|
||||
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,
|
||||
});
|
||||
if (!options.skipHooks) {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user