fix(core): update
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
export * from './typedrequest.classes.typedrequest';
|
||||
export * from './typedrequest.classes.typedhandler';
|
||||
export * from './typedrequest.classes.typedrouter';
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
31
ts/typedrequest.classes.typedrouter.ts
Normal file
31
ts/typedrequest.classes.typedrouter.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -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 };
|
||||
|
Reference in New Issue
Block a user