2021-05-30 16:56:48 +00:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
2020-09-12 21:53:41 +00:00
|
|
|
|
|
|
|
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-05-27 17:09:15 +00:00
|
|
|
constructor() {
|
|
|
|
axios.interceptors.response.use(
|
2021-05-29 06:27:52 +00:00
|
|
|
(response) => Promise.resolve(response),
|
2021-05-27 17:09:15 +00:00
|
|
|
(err) => {
|
|
|
|
if (!err.response) {
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2021-05-30 16:56:48 +00:00
|
|
|
const { response } = err;
|
2021-05-27 17:09:15 +00:00
|
|
|
return Promise.reject(response);
|
2021-05-29 06:27:52 +00:00
|
|
|
},
|
2021-05-27 17:09:15 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-29 06:27:52 +00:00
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
/**
|
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-05-27 14:13:16 +00:00
|
|
|
public async request(config: AxiosRequestConfig, customConfig?: any) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
config.headers = {
|
|
|
|
...this.headers,
|
|
|
|
...config.headers,
|
|
|
|
};
|
2021-05-30 16:56:48 +00:00
|
|
|
return await axios({ ...config, ...customConfig });
|
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,
|
2021-05-29 06:27:52 +00:00
|
|
|
customConfig?: any,
|
2021-02-09 09:33:54 +00:00
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
headers,
|
|
|
|
method: 'GET',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-05-29 06:27:52 +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,
|
2021-05-29 06:27:52 +00:00
|
|
|
customConfig?: any,
|
2021-02-09 09:33:54 +00:00
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'POST',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-05-29 06:27:52 +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,
|
2021-05-29 06:27:52 +00:00
|
|
|
customConfig?: any,
|
2021-02-09 09:33:54 +00:00
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'PUT',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-05-29 06:27:52 +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,
|
2021-05-29 06:27:52 +00:00
|
|
|
customConfig?: any,
|
2021-02-09 09:33:54 +00:00
|
|
|
) {
|
2020-09-12 21:53:41 +00:00
|
|
|
return this.request(
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
method: 'DELETE',
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
},
|
2021-05-29 06:27:52 +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();
|