2019-08-25 15:19:12 +00:00
|
|
|
import * as plugins from './typedrequest.plugins';
|
|
|
|
|
|
|
|
import { TypedHandler } from './typedrequest.classes.typedhandler';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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<
|
2019-08-25 15:19:12 +00:00
|
|
|
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
|
|
|
|
>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
typedHandler = this.handlerMap.find(handler => {
|
|
|
|
return handler.method === methodArg;
|
|
|
|
});
|
2020-06-15 16:39:48 +00:00
|
|
|
|
2020-06-15 16:36:53 +00:00
|
|
|
if (!typedHandler) {
|
|
|
|
this.routerMap.getArray().forEach(typedRouter => {
|
|
|
|
if (!typedHandler) {
|
|
|
|
typedHandler = typedRouter.getTypedHandlerForMethod(methodArg, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return typedHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* routes a typed request to a handler
|
|
|
|
* @param typedRequestArg
|
|
|
|
*/
|
|
|
|
public async routeAndAddResponse(typedRequestArg: plugins.typedRequestInterfaces.ITypedRequest) {
|
|
|
|
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
2019-08-25 15:19:12 +00:00
|
|
|
|
2019-08-31 12:11:56 +00:00
|
|
|
if (!typedHandler) {
|
|
|
|
const availableMethods: string[] = [];
|
2020-02-11 18:55:07 +00:00
|
|
|
await this.handlerMap.forEach(async handler => {
|
2019-08-31 12:11:56 +00:00
|
|
|
availableMethods.push(handler.method);
|
|
|
|
});
|
|
|
|
console.log(`Cannot find method for ${typedHandler}`);
|
|
|
|
console.log(`Available methods are:`);
|
|
|
|
console.log(availableMethods);
|
2020-06-15 16:36:53 +00:00
|
|
|
typedRequestArg.error = {
|
2019-09-01 13:39:08 +00:00
|
|
|
text: 'There is no available method for this call on the server side',
|
|
|
|
data: {}
|
|
|
|
};
|
2020-06-15 16:36:53 +00:00
|
|
|
return typedRequestArg;
|
2019-08-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 16:36:53 +00:00
|
|
|
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
|
|
|
return typedRequestArg;
|
2019-08-25 15:19:12 +00:00
|
|
|
}
|
|
|
|
}
|