From adad99f6bf5cef6f5e3b13078093c7438aabe1ef Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 18 Dec 2020 18:14:29 +0000 Subject: [PATCH] fix(core): update --- ts/typedrequest.classes.typedrequest.ts | 67 ++++++--------------- ts/typedrequest.classes.typedrouter.ts | 2 +- ts/typedrequest.classes.typedtarget.ts | 78 +++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 ts/typedrequest.classes.typedtarget.ts diff --git a/ts/typedrequest.classes.typedrequest.ts b/ts/typedrequest.classes.typedrequest.ts index 652d1f5..d8bcc8b 100644 --- a/ts/typedrequest.classes.typedrequest.ts +++ b/ts/typedrequest.classes.typedrequest.ts @@ -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; - -export type IPostMethodWithTypedRouter = ( - typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest -) => Promise | Promise; +const webrequestInstance = new plugins.webrequest.WebRequest(); export class TypedRequest { - /** - * 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; - // 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( diff --git a/ts/typedrequest.classes.typedrouter.ts b/ts/typedrequest.classes.typedrouter.ts index 9f50710..a25afe4 100644 --- a/ts/typedrequest.classes.typedrouter.ts +++ b/ts/typedrequest.classes.typedrouter.ts @@ -14,7 +14,7 @@ export class TypedRouter { public routerMap = new plugins.lik.ObjectMap(); public handlerMap = new plugins.lik.ObjectMap< - TypedHandler + TypedHandler >(); public fireEventInterestMap = new plugins.lik.InterestMap< diff --git a/ts/typedrequest.classes.typedtarget.ts b/ts/typedrequest.classes.typedtarget.ts new file mode 100644 index 0000000..44f6d58 --- /dev/null +++ b/ts/typedrequest.classes.typedtarget.ts @@ -0,0 +1,78 @@ +import { TypedRouter } from './typedrequest.classes.typedrouter'; +import * as plugins from './typedrequest.plugins'; + +export type IPostMethod = ( + typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest +) => Promise; + +/** + * 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 | Promise; + +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(payloadArg: T): Promise { + 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; + } +}