fix(core): update

This commit is contained in:
2019-08-25 17:19:12 +02:00
parent 9db92be274
commit 386d848d51
6 changed files with 105 additions and 38 deletions

View File

@ -1,2 +1,3 @@
export * from './typedrequest.classes.typedrequest';
export * from './typedrequest.classes.typedhandler';
export * from './typedrequest.classes.typedrouter';

View File

@ -1 +1,29 @@
export class TypedHandler {}
import * as plugins from './typedrequest.plugins';
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (requestArg: T['request']) => Promise<T['response']>;
/**
* typed handler for dealing with typed requests
*/
export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest> {
public method: string;
private handlerFunction: THandlerFunction<T>;
constructor(methodArg: T['method'], handlerFunctionArg: THandlerFunction<T>) {
this.method = methodArg;
this.handlerFunction = handlerFunctionArg;
}
/**
* adds a response to the typedRequest
* @param typedRequestArg
*/
public async addResponse(typedRequestArg: T) {
if (typedRequestArg.method !== this.method) {
throw new Error('this handler has been given a wrong method to answer to. Please use a TypedRouter to filter requests');
}
const response = await this.handlerFunction(typedRequestArg.request);
typedRequestArg.response = response;
return typedRequestArg;
}
}

View File

@ -0,0 +1,31 @@
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 {
public handlerMap = new plugins.lik.Objectmap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
>();
/**
* adds the handler to the routing map
* @param handlerArg
*/
public addTypedHandler(handlerArg: TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>) {
this.handlerMap.add(handlerArg);
}
public async addResponse(typedRequest: plugins.typedRequestInterfaces.ITypedRequest) {
const typedHandler = this.handlerMap.find(handler => {
return handler.method === typedRequest.method;
});
typedRequest = await typedHandler.addResponse(typedRequest);
return typedRequest;
}
}

View File

@ -4,6 +4,8 @@ import * as typedRequestInterfaces from '@apiglobal/typedrequest-interfaces';
export { typedRequestInterfaces };
// pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartjson from '@pushrocks/smartjson';
export { smartrequest };
export { lik, smartrequest, smartjson };