fix(core): update

This commit is contained in:
Philipp Kunz 2020-12-18 18:14:29 +00:00
parent 2771c92e85
commit adad99f6bf
3 changed files with 96 additions and 51 deletions

View File

@ -1,21 +1,12 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
import { TypedRouter } from './typedrequest.classes.typedrouter';
import { TypedTarget } from './typedrequest.classes.typedtarget';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
export type IPostMethodWithTypedRouter = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<void> | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
const webrequestInstance = new plugins.webrequest.WebRequest();
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
/**
* this typedrouter allows us to have easy async request response cycles
*/
public typedRouterRef: TypedRouter;
public webrequest = new plugins.webrequest.WebRequest();
/**
* in case we post against a url endpoint
@ -23,30 +14,24 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
public urlEndPoint?: string;
/**
* in case we post with some other method, ec ipc communication
* in case we post against a TypedTarget
*/
public postMethod?: IPostMethod | IPostMethodWithTypedRouter;
typedTarget: TypedTarget;
public method: string;
// STATIC
constructor(postEndPointArg: string | IPostMethod, methodArg: T['method']);
constructor(
postEndPointArg: string | IPostMethodWithTypedRouter,
methodArg: T['method'],
typedrouterRefArg: TypedRouter
);
constructor(
postEndPointArg: string | IPostMethodWithTypedRouter,
methodArg: T['method'],
typedrouterRefArg?: TypedRouter
) {
if (typeof postEndPointArg === 'string') {
this.urlEndPoint = postEndPointArg;
/**
* note the overloading is thought to deak with promises
* @param postEndPointArg
* @param methodArg
*/
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
if (typeof postTarget === 'string') {
this.urlEndPoint = postTarget;
} else {
this.postMethod = postEndPointArg;
this.typedTarget = postTarget;
}
this.method = methodArg;
this.typedRouterRef = typedrouterRefArg;
}
/**
@ -65,28 +50,10 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
if (this.urlEndPoint) {
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
const response = await webrequestInstance.postJson(this.urlEndPoint, payload);
responseBody = response;
} else {
let responseInterest: plugins.lik.Interest<
string,
plugins.typedRequestInterfaces.ITypedRequest
>;
// having a typedrouter allows us to work with async request response cycles.
if (this.typedRouterRef) {
responseInterest = await this.typedRouterRef.fireEventInterestMap.addInterest(
payload.correlation.id,
payload
);
}
const postMethodReturnValue = await this.postMethod(payload);
if (responseInterest) {
responseBody = await responseInterest.interestFullfilled;
} else if (postMethodReturnValue) {
responseBody = postMethodReturnValue;
} else {
responseBody = payload;
}
responseBody = await this.typedTarget.post(payload);
}
if (responseBody.error) {
console.error(

View File

@ -14,7 +14,7 @@ export class TypedRouter {
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
public handlerMap = new plugins.lik.ObjectMap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
>();
public fireEventInterestMap = new plugins.lik.InterestMap<

View File

@ -0,0 +1,78 @@
import { TypedRouter } from './typedrequest.classes.typedrouter';
import * as plugins from './typedrequest.plugins';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
/**
* enables the use of custom post functions
* used for things like broadcast channels
* e.g. @designestate/dees-comms
* the main difference here is, that the response comes back async and is routed by interest through typedrouter
*/
export type IPostMethodWithTypedRouter = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<void> | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
export interface ITypedTargetConstructorOptions {
url?: string;
postMethod?: IPostMethod;
/**
* a post method that does not return the answer
*/
postMethodWithTypedRouter?: IPostMethodWithTypedRouter;
/**
* this typedrouter allows us to have easy async request response cycles
*/
typedRouterRef?: TypedRouter;
}
/**
* a typed target defines a target for requests
*/
export class TypedTarget {
url: string;
type: 'rest' | 'socket';
options: ITypedTargetConstructorOptions;
constructor(optionsArg: ITypedTargetConstructorOptions) {
if (optionsArg.postMethodWithTypedRouter && !optionsArg.typedRouterRef) {
throw new Error('you have to specify a typedrouter when using postmethod with typedrouter');
}
this.options = optionsArg;
}
/**
* wether calls to this target are bound to the request/response cycle
* if false, always delivers response as result of a call
* if true, delivers response in a separate call
* can only be async when type is 'socket'
*/
public isAsync: boolean;
public async post<T extends plugins.typedRequestInterfaces.ITypedRequest>(payloadArg: T): Promise<T> {
let responseInterest: plugins.lik.Interest<
string,
plugins.typedRequestInterfaces.ITypedRequest
>;
// having a typedrouter allows us to work with async request response cycles.
if (this.options.typedRouterRef) {
responseInterest = await this.options.typedRouterRef.fireEventInterestMap.addInterest(
payloadArg.correlation.id,
payloadArg
);
}
const postMethod = this.options.postMethod || this.options.postMethodWithTypedRouter;
const postMethodReturnValue = await postMethod(payloadArg);
let responseBody: T;
if (responseInterest) {
responseBody = (await responseInterest.interestFullfilled) as T;
} else if (postMethodReturnValue) {
responseBody = postMethodReturnValue as T;
} else {
responseBody = payloadArg;
}
return responseBody;
}
}