typedrequest/ts/typedrequest.classes.typedrouter.ts

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-08-25 15:19:12 +00:00
import * as plugins from './typedrequest.plugins';
import { TypedHandler } from './typedrequest.classes.typedhandler';
2020-07-14 02:20:15 +00:00
import { TypedRequest } from './typedrequest.classes.typedrequest';
2019-08-25 15:19:12 +00:00
/**
* 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 {
2020-06-15 16:36:53 +00:00
public upstreamTypedRouter: TypedRouter;
2020-06-15 16:39:48 +00:00
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
2020-06-15 16:36:53 +00:00
2020-06-15 16:39:48 +00:00
public handlerMap = new plugins.lik.ObjectMap<
2020-12-18 18:14:29 +00:00
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
2019-08-25 15:19:12 +00:00
>();
2020-07-14 16:37:44 +00:00
public fireEventInterestMap = new plugins.lik.InterestMap<
string,
plugins.typedRequestInterfaces.ITypedRequest
>((correlationId: string) => correlationId);
2020-07-14 02:20:15 +00:00
2019-08-25 15:19:12 +00:00
/**
* adds the handler to the routing map
2020-06-15 16:36:53 +00:00
* @param typedHandlerArg
2019-08-25 15:19:12 +00:00
*/
2020-02-11 18:55:07 +00:00
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(
2020-06-15 16:36:53 +00:00
typedHandlerArg: TypedHandler<T>
2020-02-11 18:55:07 +00:00
) {
2020-06-15 16:36:53 +00:00
// lets check for deduplication
const existingTypedHandler = this.getTypedHandlerForMethod(typedHandlerArg.method);
if (existingTypedHandler) {
2020-06-15 16:39:48 +00:00
throw new Error(
`a TypedHandler for ${typedHandlerArg.method} alredy exists! Can't add another one.`
);
2020-06-15 16:36:53 +00:00
}
this.handlerMap.add(typedHandlerArg);
}
/**
* adds another sub typedRouter
2020-06-15 16:39:48 +00:00
* @param typedRequest
2020-06-15 16:36:53 +00:00
*/
public addTypedRouter(typedRouterArg: TypedRouter) {
this.routerMap.add(typedRouterArg);
2019-08-25 15:19:12 +00:00
}
2020-06-15 16:36:53 +00:00
public setUpstreamTypedRouter(typedRouterArg: TypedRouter) {
this.upstreamTypedRouter = typedRouterArg;
}
public checkForTypedHandler(methodArg: string): boolean {
2020-06-15 16:39:48 +00:00
return !!this.getTypedHandlerForMethod(methodArg);
2020-06-15 16:36:53 +00:00
}
/**
* gets a typed Router from the router chain, upstream and downstream
* @param methodArg
2020-06-15 16:39:48 +00:00
* @param checkUpstreamRouter
2020-06-15 16:36:53 +00:00
*/
2020-06-15 16:39:48 +00:00
public getTypedHandlerForMethod(
methodArg: string,
checkUpstreamRouter = true
): TypedHandler<any> {
2020-06-15 16:36:53 +00:00
let typedHandler: TypedHandler<any>;
2020-06-15 16:39:48 +00:00
2020-06-15 16:36:53 +00:00
if (this.upstreamTypedRouter && checkUpstreamRouter) {
typedHandler = this.upstreamTypedRouter.getTypedHandlerForMethod(methodArg);
} else {
2021-09-27 10:14:43 +00:00
typedHandler = this.handlerMap.findSync((handler) => {
2020-06-15 16:36:53 +00:00
return handler.method === methodArg;
});
2020-06-15 16:39:48 +00:00
2020-06-15 16:36:53 +00:00
if (!typedHandler) {
2020-07-14 02:20:15 +00:00
this.routerMap.getArray().forEach((typedRouter) => {
2020-06-15 16:36:53 +00:00
if (!typedHandler) {
typedHandler = typedRouter.getTypedHandlerForMethod(methodArg, false);
}
});
}
}
return typedHandler;
}
/**
2020-07-14 02:20:15 +00:00
* 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
2020-06-15 16:36:53 +00:00
* @param typedRequestArg
*/
2021-11-07 14:40:01 +00:00
public async routeAndAddResponse<T extends plugins.typedRequestInterfaces.ITypedRequest = any>(typedRequestArg: T): Promise<T> {
2020-07-30 08:46:45 +00:00
if (!typedRequestArg?.correlation?.phase || typedRequestArg.correlation.phase === 'request') {
2020-07-14 02:20:15 +00:00
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
2019-08-31 12:11:56 +00:00
2020-07-14 02:20:15 +00:00
if (!typedHandler) {
console.log(`Cannot find handler for methodname ${typedRequestArg.method}`);
typedRequestArg.error = {
text: 'There is no available method for this call on the server side',
data: {},
};
return typedRequestArg;
}
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
} else if (typedRequestArg.correlation.phase === 'response') {
2020-07-26 13:48:57 +00:00
this.fireEventInterestMap
.findInterest(typedRequestArg.correlation.id)
?.fullfillInterest(typedRequestArg);
2020-10-06 21:33:46 +00:00
} else {
console.log('received weirdly shaped request');
console.log(typedRequestArg);
2021-11-07 14:40:01 +00:00
return null
2020-07-14 02:20:15 +00:00
}
2021-11-07 14:40:01 +00:00
return typedRequestArg;
2019-08-25 15:19:12 +00:00
}
}