fix(core): update

This commit is contained in:
2020-07-14 16:37:44 +00:00
parent 2a681644eb
commit 88022ea14b
5 changed files with 68 additions and 86 deletions

View File

@ -2,16 +2,38 @@ import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
import { TypedRouter } from './typedrequest.classes.typedrouter';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => void | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
public typedRouterRef: TypedRouter;
public webrequest = new plugins.webrequest.WebRequest();
public urlEndPoint: string;
/**
* in case we post against a url endpoint
*/
public urlEndPoint?: string;
/**
* in case we post with some other method, ec ipc communication
*/
public postMethod?: IPostMethod;
public method: string;
// STATIC
constructor(urlEndPointArg: string, methodArg: T['method']) {
this.urlEndPoint = urlEndPointArg;
constructor(
postEndPointArg: string | IPostMethod,
methodArg: T['method'],
typedrouterRefArg?: TypedRouter
) {
if (typeof postEndPointArg === 'string') {
this.urlEndPoint = postEndPointArg;
} else {
this.postMethod = postEndPointArg;
}
this.method = methodArg;
this.typedRouterRef = typedrouterRefArg;
}
/**
@ -24,12 +46,34 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
response: null,
correlation: {
id: plugins.isounique.uni(),
phase: 'request'
}
phase: 'request',
},
};
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
const responseBody: T = response;
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
if (this.urlEndPoint) {
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
responseBody = response;
} else {
let responseInterest: plugins.lik.Interest<
string,
plugins.typedRequestInterfaces.ITypedRequest
>;
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;
}
}
if (responseBody.error) {
console.error(
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
@ -49,13 +93,4 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
}
return responseBody.response;
}
public addTypedRouterForResponse(typedRouterArg: TypedRouter) {
if (!this.typedRouterRef) {
this.typedRouterRef = typedRouterArg;
typedRouterArg.addTypedRequest(this);
}
}
public registerAlternateFireMethod() {}
}

View File

@ -12,21 +12,15 @@ export class TypedRouter {
public upstreamTypedRouter: TypedRouter;
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
public typedRequestMap = new plugins.lik.ObjectMap<TypedRequest<any>>();
public handlerMap = new plugins.lik.ObjectMap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
>();
public fireEventInterestMap = new plugins.lik.InterestMap((correlationId: string) => correlationId);
public addTypedRequest(typedRequestArg: TypedRequest<any>) {
if(!this.typedRequestMap.checkForObject(typedRequestArg)) {
this.typedRequestMap.add(typedRequestArg);
typedRequestArg.addTypedRouterForResponse(this);
}
}
public fireEventInterestMap = new plugins.lik.InterestMap<
string,
plugins.typedRequestInterfaces.ITypedRequest
>((correlationId: string) => correlationId);
/**
* adds the handler to the routing map
@ -112,7 +106,7 @@ export class TypedRouter {
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
} else if (typedRequestArg.correlation.phase === 'response') {
this.fireEventInterestMap.findInterest(typedRequestArg.correlation.id)?.fullfillInterest(typedRequestArg);
}
return typedRequestArg;
}

View File

@ -7,6 +7,7 @@ export { typedRequestInterfaces };
import * as isounique from '@pushrocks/isounique';
import * as lik from '@pushrocks/lik';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise';
import * as webrequest from '@pushrocks/webrequest';
export { isounique, lik, smartdelay, webrequest };
export { isounique, lik, smartdelay, smartpromise, webrequest };