typedrequest/ts/typedrequest.classes.typedrequest.ts

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-08-23 16:01:37 +00:00
import * as plugins from './typedrequest.plugins';
2020-06-16 22:00:38 +00:00
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
2020-07-14 02:20:15 +00:00
import { TypedRouter } from './typedrequest.classes.typedrouter';
2019-08-23 16:01:37 +00:00
2020-07-14 16:37:44 +00:00
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => void | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
2019-08-23 16:01:37 +00:00
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
2020-07-14 02:20:15 +00:00
public typedRouterRef: TypedRouter;
2020-06-25 23:53:05 +00:00
public webrequest = new plugins.webrequest.WebRequest();
2020-07-14 16:37:44 +00:00
/**
* 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;
2019-08-23 16:01:37 +00:00
public method: string;
// STATIC
2020-07-14 16:37:44 +00:00
constructor(
postEndPointArg: string | IPostMethod,
methodArg: T['method'],
typedrouterRefArg?: TypedRouter
) {
if (typeof postEndPointArg === 'string') {
this.urlEndPoint = postEndPointArg;
} else {
this.postMethod = postEndPointArg;
}
2019-08-23 16:01:37 +00:00
this.method = methodArg;
2020-07-14 16:37:44 +00:00
this.typedRouterRef = typedrouterRefArg;
2019-08-23 16:01:37 +00:00
}
/**
2020-07-14 02:20:15 +00:00
* fires the request
2019-08-23 16:01:37 +00:00
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
2020-07-14 02:20:15 +00:00
const payload: plugins.typedRequestInterfaces.ITypedRequest = {
2020-06-25 23:53:05 +00:00
method: this.method,
request: fireArg,
2020-07-14 02:20:15 +00:00
response: null,
correlation: {
id: plugins.isounique.uni(),
2020-07-14 16:37:44 +00:00
phase: 'request',
},
2020-07-14 02:20:15 +00:00
};
2020-07-14 16:37:44 +00:00
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;
}
}
2019-09-01 13:39:08 +00:00
if (responseBody.error) {
2020-06-25 23:53:05 +00:00
console.error(
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
responseBody.error.data
)}`
);
2020-06-16 22:00:38 +00:00
if (!responseBody.retry) {
throw new TypedResponseError(responseBody.error.text, responseBody.error.data);
}
2020-02-11 18:55:07 +00:00
return null;
2019-09-01 13:39:08 +00:00
}
2020-02-11 18:55:07 +00:00
if (responseBody.retry) {
2020-02-12 08:29:43 +00:00
console.log(`server requested retry for the following reason: ${responseBody.retry.reason}`);
await plugins.smartdelay.delayFor(responseBody.retry.waitForMs);
// tslint:disable-next-line: no-return-await
return await this.fire(fireArg);
}
2020-02-11 18:55:07 +00:00
return responseBody.response;
2019-08-23 16:01:37 +00:00
}
}