DocSpace-client/packages/common/utils/axiosClient.js

161 lines
3.9 KiB
JavaScript
Raw Normal View History

import axios from "axios";
import { AppServerConfig } from "../constants";
import combineUrl from "./combineUrl";
2022-11-13 19:38:33 +00:00
const { proxyURL, apiOrigin, apiPrefix, apiTimeout } = AppServerConfig;
class AxiosClient {
constructor() {
if (typeof window !== "undefined") this.initCSR();
}
initCSR = () => {
this.isSSR = false;
2022-11-13 19:38:33 +00:00
const origin = apiOrigin || window.location.origin;
2022-11-14 16:46:07 +00:00
let headers = null;
2022-11-13 19:38:33 +00:00
if (apiOrigin !== "") {
2022-11-14 16:46:07 +00:00
headers = {
"Access-Control-Allow-Credentials": true,
2022-11-14 16:46:07 +00:00
};
2022-11-13 19:38:33 +00:00
}
const apiBaseURL = combineUrl(origin, proxyURL, apiPrefix);
const paymentsURL = combineUrl(
proxyURL,
"/portal-settings/payments/portal-payments"
);
this.paymentsURL = paymentsURL;
2022-07-31 10:52:35 +00:00
// window.AppServer = {
// ...window.AppServer,
// origin,
// proxyURL,
2022-11-13 19:38:33 +00:00
// apiPrefix,
2022-07-31 10:52:35 +00:00
// apiBaseURL,
// apiTimeout,
// paymentsURL,
// };
2022-11-13 19:38:33 +00:00
const config = {
baseURL: apiBaseURL,
responseType: "json",
timeout: apiTimeout, // default is `0` (no timeout)
withCredentials: true,
2022-11-13 19:38:33 +00:00
};
2022-11-14 16:46:07 +00:00
if (headers) {
2022-11-13 19:38:33 +00:00
config.headers = headers;
}
this.client = axios.create(config);
};
initSSR = (headers) => {
this.isSSR = true;
const xRewriterUrl = headers["x-rewriter-url"];
2022-11-13 19:38:33 +00:00
const origin = apiOrigin || xRewriterUrl;
const apiBaseURL = combineUrl(origin, proxyURL, apiPrefix);
this.client = axios.create({
baseURL: apiBaseURL,
responseType: "json",
timeout: apiTimeout,
headers: headers,
});
};
setWithCredentialsStatus = (state) => {
this.client.defaults.withCredentials = state;
};
setClientBasePath = (path) => {
if (!path) return;
this.client.defaults.baseURL = path;
};
getResponseError = (res) => {
if (!res) return;
if (res.data && res.data.error) {
return res.data.error.message;
}
if (res.isAxiosError && res.message) {
//console.error(res.message);
return res.message;
}
};
request = (options) => {
const onSuccess = (response) => {
const error = this.getResponseError(response);
if (error) throw new Error(error);
if (!response || !response.data || response.isAxiosError) return null;
if (response.data.hasOwnProperty("total"))
return { total: +response.data.total, items: response.data.response };
if (response.request.responseType === "text") return response.data;
return response.data.response;
};
const onError = (error) => {
console.log("Request Failed:", { error });
// let errorText = error.response
// ? this.getResponseError(error.response)
// : error.message;
if (error?.response?.status === 401 && this.isSSR) {
error.response.data.error.message = 401;
}
const loginURL = combineUrl(proxyURL, "/login");
if (!this.isSSR) {
switch (error.response?.status) {
case 401:
if (options.skipUnauthorized) return Promise.resolve();
if (options.skipLogout) return Promise.reject(error);
this.request({
method: "post",
url: "/authentication/logout",
}).then(() => {
this.setWithCredentialsStatus(false);
2022-07-31 10:52:35 +00:00
window.location.href = loginURL;
});
break;
case 402:
if (!window.location.pathname.includes("payments")) {
// window.location.href = this.paymentsURL;
}
break;
default:
break;
}
return Promise.reject(error);
} else {
switch (error.response?.status) {
case 401:
return Promise.resolve();
default:
break;
}
return Promise.reject(error);
}
};
return this.client(options).then(onSuccess).catch(onError);
};
}
export default AxiosClient;