feat(typedrouter): add middleware support to TypedRouter and export middleware type
This commit is contained in:
@@ -3,6 +3,7 @@ import { VirtualStream } from './classes.virtualstream.js';
|
||||
|
||||
import { TypedHandler } from './classes.typedhandler.js';
|
||||
import { TypedRequest } from './classes.typedrequest.js';
|
||||
import { TypedResponseError } from './classes.typedresponseerror.js';
|
||||
|
||||
/**
|
||||
* Log entry for TypedRequest traffic monitoring
|
||||
@@ -28,12 +29,20 @@ export interface ITypedRouterHooks {
|
||||
onOutgoingResponse?: (entry: ITypedRequestLogEntry) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Middleware function that runs before the handler.
|
||||
* Throw TypedResponseError to reject the request.
|
||||
*/
|
||||
export type TMiddlewareFunction<TReq extends Partial<plugins.typedRequestInterfaces.ITypedRequest> = plugins.typedRequestInterfaces.ITypedRequest> = (
|
||||
typedRequest: plugins.typedRequestInterfaces.ITypedRequest & TReq
|
||||
) => Promise<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 {
|
||||
export class TypedRouter<TReqConstraint extends Partial<plugins.typedRequestInterfaces.ITypedRequest> = plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
// Use globalThis for cross-bundle hook sharing
|
||||
public static get globalHooks(): ITypedRouterHooks {
|
||||
if (!(globalThis as any).__typedRouterGlobalHooks) {
|
||||
@@ -89,12 +98,23 @@ export class TypedRouter {
|
||||
}
|
||||
}
|
||||
|
||||
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
|
||||
public routerMap = new plugins.lik.ObjectMap<TypedRouter<any>>();
|
||||
public handlerMap = new plugins.lik.ObjectMap<
|
||||
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
|
||||
>();
|
||||
public registeredVirtualStreams = new plugins.lik.ObjectMap<VirtualStream<any>>();
|
||||
|
||||
// Middleware chain
|
||||
private middlewares: TMiddlewareFunction<TReqConstraint>[] = [];
|
||||
|
||||
/**
|
||||
* Adds a middleware function that runs before the handler on this router.
|
||||
* Throw TypedResponseError to reject the request.
|
||||
*/
|
||||
public addMiddleware(fn: TMiddlewareFunction<TReqConstraint>): void {
|
||||
this.middlewares.push(fn);
|
||||
}
|
||||
|
||||
public fireEventInterestMap = new plugins.lik.InterestMap<
|
||||
string,
|
||||
plugins.typedRequestInterfaces.ITypedRequest
|
||||
@@ -104,7 +124,7 @@ export class TypedRouter {
|
||||
* adds the handler to the routing map
|
||||
* @param typedHandlerArg
|
||||
*/
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest & TReqConstraint>(
|
||||
typedHandlerArg: TypedHandler<T>
|
||||
) {
|
||||
// lets check for deduplication
|
||||
@@ -122,7 +142,7 @@ export class TypedRouter {
|
||||
* adds another sub typedRouter
|
||||
* @param typedRequest
|
||||
*/
|
||||
public addTypedRouter(typedRouterArg: TypedRouter) {
|
||||
public addTypedRouter(typedRouterArg: TypedRouter<any>) {
|
||||
const routerExists = this.routerMap.findSync((routerArg) => routerArg === typedRouterArg);
|
||||
if (!routerExists) {
|
||||
this.routerMap.add(typedRouterArg);
|
||||
@@ -141,7 +161,7 @@ export class TypedRouter {
|
||||
*/
|
||||
public getTypedHandlerForMethod(
|
||||
methodArg: string,
|
||||
checkedRouters: TypedRouter[] = []
|
||||
checkedRouters: TypedRouter<any>[] = []
|
||||
): TypedHandler<any> {
|
||||
checkedRouters.push(this);
|
||||
|
||||
@@ -162,6 +182,26 @@ export class TypedRouter {
|
||||
return typedHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the handler AND its owning router for a given method.
|
||||
* Needed to know which router's middleware to run.
|
||||
*/
|
||||
public getTypedHandlerAndRouter(
|
||||
methodArg: string,
|
||||
checkedRouters: TypedRouter<any>[] = []
|
||||
): { handler: TypedHandler<any>; router: TypedRouter<any> } | null {
|
||||
checkedRouters.push(this);
|
||||
const handler = this.handlerMap.findSync((h) => h.method === methodArg);
|
||||
if (handler) return { handler, router: this };
|
||||
for (const child of this.routerMap.getArray()) {
|
||||
if (!checkedRouters.includes(child)) {
|
||||
const result = child.getTypedHandlerAndRouter(methodArg, checkedRouters);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for routeAndAddResponse
|
||||
*/
|
||||
@@ -214,9 +254,9 @@ export class TypedRouter {
|
||||
});
|
||||
}
|
||||
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
const result = this.getTypedHandlerAndRouter(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
if (!result) {
|
||||
console.log(`Cannot find handler for methodname ${typedRequestArg.method}`);
|
||||
typedRequestArg.error = {
|
||||
text: 'There is no available method for this call on the server side',
|
||||
@@ -247,6 +287,43 @@ export class TypedRouter {
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
||||
const { handler: typedHandler, router: owningRouter } = result;
|
||||
|
||||
// Run owning router's middleware chain
|
||||
if (owningRouter.middlewares.length > 0) {
|
||||
try {
|
||||
for (const mw of owningRouter.middlewares) {
|
||||
await mw(typedRequestArg as any);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof TypedResponseError) {
|
||||
typedRequestArg.error = { text: e.errorText, data: e.errorData || {} };
|
||||
} else {
|
||||
typedRequestArg.error = { text: (e as Error).message || 'Middleware error', data: {} };
|
||||
}
|
||||
typedRequestArg.correlation.phase = 'response';
|
||||
typedRequestArg.localData = null;
|
||||
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
||||
typedRequestArg.localData = null;
|
||||
// encode again before handing back
|
||||
|
||||
Reference in New Issue
Block a user