fleet/frontend/services/index.ts
Roberto Dip 8acf14ab43
adjust installers endpoint to avoid AJAX downloads (#7226)
Related to #7206, this delegates the handling of the download to the browser
2022-08-16 12:54:41 -03:00

45 lines
1.1 KiB
TypeScript

import axios, {
AxiosError,
AxiosResponse,
ResponseType as AxiosResponseType,
} from "axios";
import local from "utilities/local";
import URL_PREFIX from "router/url_prefix";
const sendRequest = async (
method: "GET" | "POST" | "PATCH" | "DELETE" | "HEAD",
path: string,
data?: unknown,
responseType: AxiosResponseType = "json"
): 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,
responseType,
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: unknown): string => {
const r = response as AxiosResponse;
return r.data?.errors?.[0]?.reason || ""; // TODO: check if any callers rely on empty return value
};
export default sendRequest;