2019-08-23 16:01:37 +00:00
|
|
|
import * as plugins from './typedrequest.plugins';
|
|
|
|
|
|
|
|
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
|
|
|
public urlEndPoint: string;
|
|
|
|
public method: string;
|
|
|
|
|
|
|
|
// STATIC
|
|
|
|
constructor(urlEndPointArg: string, methodArg: T['method']) {
|
|
|
|
this.urlEndPoint = urlEndPointArg;
|
|
|
|
this.method = methodArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* firest the request
|
|
|
|
*/
|
|
|
|
public async fire(fireArg: T['request']): Promise<T['response']> {
|
2019-09-01 11:17:21 +00:00
|
|
|
const response = await plugins.smartrequest.postJson(this.urlEndPoint, {
|
|
|
|
requestBody: {
|
2019-08-23 16:01:37 +00:00
|
|
|
method: this.method,
|
|
|
|
request: fireArg,
|
|
|
|
response: null
|
2019-09-01 11:17:21 +00:00
|
|
|
}
|
2019-08-23 16:01:37 +00:00
|
|
|
});
|
2019-09-01 13:39:08 +00:00
|
|
|
const responseBody: T = response.body;
|
|
|
|
if (responseBody.error) {
|
|
|
|
console.log(responseBody.error.text);
|
|
|
|
console.log(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) {
|
|
|
|
console.log('server requested retry');
|
|
|
|
};
|
|
|
|
return responseBody.response;
|
2019-08-23 16:01:37 +00:00
|
|
|
}
|
|
|
|
}
|