2020-09-12 21:53:41 +00:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
|
|
|
|
|
|
interface IHTTPHeaders {
|
2021-02-17 08:35:19 +00:00
|
|
|
[headerKey: string]: any;
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IQueryPrams {
|
2021-02-17 08:35:19 +00:00
|
|
|
[paramName: string]: any;
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service to manage all HTTP calls.
|
|
|
|
*/
|
|
|
|
class HTTPService {
|
|
|
|
/**
|
2021-03-22 06:53:19 +00:00
|
|
|
* header object to be append to all api calls.
|
2020-09-12 21:53:41 +00:00
|
|
|
*/
|
|
|
|
private headers: IHTTPHeaders = {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the headers to the given object.
|
|
|
|
*/
|
|
|
|
public setHeaders(headers: IHTTPHeaders) {
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a header to list of headers.
|
|
|
|
*/
|
|
|
|
public appendHeader(key: string, value: string) {
|
|
|
|
this.headers = {
|
|
|
|
...this.headers,
|
|
|
|
[key]: value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the given header.
|
|
|
|
*/
|
|
|
|
public removeHeader(key: string) {
|
|
|
|
this.headers[key] = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns axios interceptors.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
public getInterceptors() {
|
|
|
|
return axios.interceptors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic HTTP request.
|
|
|
|
* This is done so that developer can use any functionality
|
2021-03-22 06:53:19 +00:00
|
|
|
* provided by axios. Here, only the set headers are spread
|
2020-09-12 21:53:41 +00:00
|
|
|
* over what was sent in config.
|
|
|
|
*/
|
2021-03-22 06:53:19 +00:00
|
|
|
public async request(
|
|
|
|
config: AxiosRequestConfig,
|
|
|
|
customConfig?: any,
|
|
|
|
retryCounter = 2
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
config.headers = {
|
|
|
|
...this.headers,
|
|
|
|
...config.headers,
|
|
|
|
};
|
|
|
|
return await axios({ ...config, ...customConfig });
|
|
|
|
} catch (e) {
|
|
|
|
retryCounter > 0 &&
|
|
|
|
(await this.request(config, customConfig, retryCounter - 1));
|
|
|
|
throw e;
|
|
|
|
}
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get request.
|
|
|
|
*/
|
2021-02-09 09:33:54 +00:00
|
|
|
public get(
|
|
|
|
url: string,
|
|
|
|
params?: IQueryPrams,
|
|
|
|
headers?: IHTTPHeaders,
|
|
|
|
customConfig?: any
|
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
headers,
|
|
|
|
method: 'GET',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-02-09 09:33:54 +00:00
|
|
|
customConfig
|
2020-09-12 21:53:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post request
|
|
|
|
*/
|
2021-02-09 09:33:54 +00:00
|
|
|
public post(
|
|
|
|
url: string,
|
|
|
|
data?: any,
|
|
|
|
params?: IQueryPrams,
|
|
|
|
headers?: IHTTPHeaders,
|
|
|
|
customConfig?: any
|
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'POST',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-02-09 09:33:54 +00:00
|
|
|
customConfig
|
2020-09-12 21:53:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put request
|
|
|
|
*/
|
2021-02-09 09:33:54 +00:00
|
|
|
public put(
|
|
|
|
url: string,
|
|
|
|
data: any,
|
|
|
|
params?: IQueryPrams,
|
|
|
|
headers?: IHTTPHeaders,
|
|
|
|
customConfig?: any
|
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'PUT',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-02-09 09:33:54 +00:00
|
|
|
customConfig
|
2020-09-12 21:53:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete request
|
|
|
|
*/
|
2021-02-09 09:33:54 +00:00
|
|
|
public delete(
|
|
|
|
url: string,
|
|
|
|
data: any,
|
|
|
|
params?: IQueryPrams,
|
|
|
|
headers?: IHTTPHeaders,
|
|
|
|
customConfig?: any
|
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'DELETE',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-02-09 09:33:54 +00:00
|
|
|
customConfig
|
2020-09-12 21:53:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a Singleton Service.
|
|
|
|
// This will help me maintain common headers / functionality
|
|
|
|
// at a central place.
|
|
|
|
export default new HTTPService();
|