fleet/frontend/services/index.ts
Martavis Parker 384c987389
Removed all traces of Redux from the app! (#5287)
* clean up routes and useless components

* component clean up

* removed redux from routes

* rename file

* moved useDeepEffect hook with others

* removed redux, fleet, app_constants dirs; added types to utilities

* style cleanup

* typo fix

* removed unused ts-ignore comments

* removed redux packages!!!

* formatting

* fixed typing for simple search function

* updated frontend readme
2022-04-22 09:45:35 -07:00

39 lines
919 B
TypeScript

import axios, { AxiosError, AxiosResponse } from "axios";
import local from "utilities/local";
import URL_PREFIX from "router/url_prefix";
const sendRequest = async (
method: "GET" | "POST" | "PATCH" | "DELETE",
path: string,
data?: any
): Promise<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 axiosError = error as AxiosError;
return Promise.reject(axiosError.response);
}
};
// return the first error
export const getError = (response: any): string => {
const r = response as AxiosResponse;
return r.data?.errors[0].reason || "";
};
export default sendRequest;