mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
3575d34507
* base logic to handle rendering a notification without redux * removed dispatches for new flash mesage triggers * query page wrapper is no longer needed * refactored confirm invite page to remove redux * refactored email token redirect to functional and typescript * refactored pack composer page to functional and typescript * clean up * lint fixes * tests no longer needed * fixed confirm sso invite test * test fix * fixed test * fixed tests * removed redux from flash on rebased pages
40 lines
933 B
TypeScript
40 lines
933 B
TypeScript
import axios, { AxiosError, AxiosResponse } from "axios";
|
|
// @ts-ignore
|
|
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;
|