typedrequest/ts/typedrequest.classes.typedrequest.ts

104 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-02-20 16:40:30 +00:00
import * as plugins from './plugins.js';
2024-02-21 17:29:35 +00:00
import { VirtualStream } from './typedrequest.classes.virtualstream.js';
2022-03-24 19:02:58 +00:00
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
import { TypedTarget } from './typedrequest.classes.typedtarget.js';
2019-08-23 16:01:37 +00:00
2020-12-18 18:14:29 +00:00
const webrequestInstance = new plugins.webrequest.WebRequest();
2020-07-14 16:37:44 +00:00
2019-08-23 16:01:37 +00:00
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
2020-07-14 16:37:44 +00:00
/**
* in case we post against a url endpoint
*/
public urlEndPoint?: string;
/**
2020-12-18 18:14:29 +00:00
* in case we post against a TypedTarget
2020-07-14 16:37:44 +00:00
*/
2020-12-18 18:14:29 +00:00
typedTarget: TypedTarget;
2019-08-23 16:01:37 +00:00
public method: string;
2020-12-18 18:14:29 +00:00
/**
* @param postEndPointArg
2022-10-09 16:57:31 +00:00
* @param methodArg
2020-12-18 18:14:29 +00:00
*/
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
if (typeof postTarget === 'string') {
this.urlEndPoint = postTarget;
2020-07-14 16:37:44 +00:00
} else {
2020-12-18 18:14:29 +00:00
this.typedTarget = postTarget;
2020-07-14 16:37:44 +00:00
}
2019-08-23 16:01:37 +00:00
this.method = methodArg;
}
/**
2020-07-14 02:20:15 +00:00
* fires the request
2019-08-23 16:01:37 +00:00
*/
2022-05-29 19:01:56 +00:00
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
2024-02-21 17:29:35 +00:00
let payloadSending: 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
};
2024-02-21 17:29:35 +00:00
// lets preprocess the payload
payloadSending = VirtualStream.encodePayloadForNetwork(payloadSending, {
2024-02-24 11:20:13 +00:00
sendMethod: (payloadArg: plugins.typedRequestInterfaces.IStreamRequest) => {
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
2024-02-21 17:29:35 +00:00
}
});
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
payloadReceiving = await this.postTrObject(payloadSending, useCacheArg);
// lets preprocess the response
payloadReceiving = VirtualStream.decodePayloadFromNetwork(payloadReceiving, {
2024-02-24 11:20:13 +00:00
sendMethod: (payloadArg: plugins.typedRequestInterfaces.IStreamRequest) => {
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
2024-02-21 17:29:35 +00:00
}
});
return payloadReceiving.response;
}
private async postTrObject(payloadSendingArg: plugins.typedRequestInterfaces.ITypedRequest, useCacheArg: boolean = false) {
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
2020-07-14 16:37:44 +00:00
if (this.urlEndPoint) {
2024-02-21 17:29:35 +00:00
const response = await webrequestInstance.postJson(
this.urlEndPoint,
payloadSendingArg,
useCacheArg
);
payloadReceiving = response;
2020-07-14 16:37:44 +00:00
} else {
2024-02-21 17:29:35 +00:00
payloadReceiving = await this.typedTarget.post(payloadSendingArg);
2020-07-14 16:37:44 +00:00
}
2024-02-21 17:29:35 +00:00
if (payloadReceiving.error) {
2020-06-25 23:53:05 +00:00
console.error(
2024-02-21 17:29:35 +00:00
`Got an error ${payloadReceiving.error.text} with data ${JSON.stringify(
payloadReceiving.error.data,
null,
2
2020-06-25 23:53:05 +00:00
)}`
);
2024-02-21 17:29:35 +00:00
if (!payloadReceiving.retry) {
throw new TypedResponseError(payloadReceiving.error.text, payloadReceiving.error.data);
2020-06-16 22:00:38 +00:00
}
2020-02-11 18:55:07 +00:00
return null;
2019-09-01 13:39:08 +00:00
}
2024-02-21 17:29:35 +00:00
if (payloadReceiving.retry) {
console.log(
`server requested retry for the following reason: ${payloadReceiving.retry.reason}`
);
await plugins.smartdelay.delayFor(payloadReceiving.retry.waitForMs);
2020-02-12 08:29:43 +00:00
// tslint:disable-next-line: no-return-await
2024-02-21 17:29:35 +00:00
payloadReceiving = await this.postTrObject(payloadSendingArg, useCacheArg);
2020-02-12 08:29:43 +00:00
}
2024-02-21 17:29:35 +00:00
return payloadReceiving;
2019-08-23 16:01:37 +00:00
}
}