fleet/frontend/services/index.ts
Martavis Parker 53dbb2ad50
#1496 fixed sorting using API (#1536)
* #1496 fixed sorting using API

* #1496 added try catch

* #1496 removed test that no longer serves a purpose

* #1496 fixed linting

* #1496 cleanup

* #1496 added loading indicator back

* #1496 fix lint issues

* #1496 added changes log

* #1496 minor fixes
2021-08-03 12:42:48 -07:00

44 lines
1005 B
TypeScript

import axios from "axios";
// @ts-ignore
import local from "utilities/local";
import URL_PREFIX from "router/url_prefix";
const createErrorMessage = (error: any): string => {
if (error.response) {
return error.response.data;
} else if (error.request) {
return "A connection error occurred. Please try again or contact us.";
}
return "Something went wrong. Please try again or contact us.";
};
const sendRequest = async (
method: "GET" | "POST" | "PATCH" | "DELETE",
path: string,
data?: any
) => {
const { origin } = global.window.location;
const url = `${origin}${URL_PREFIX}/api${path}`;
const token = local.getItem("auth_token");
try {
const response = await axios({
method,
url,
data,
headers: {
Authorization: `Bearer ${token}`,
},
});
return Promise.resolve(response.data);
} catch (error) {
const message = createErrorMessage(error);
return Promise.reject(message);
}
};
export default sendRequest;