2019-12-16 08:49:10 +00:00
|
|
|
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
|
2019-03-26 15:28:24 +00:00
|
|
|
import {
|
|
|
|
|
REQUEST_TIMEOUT_MS,
|
2019-12-16 08:49:10 +00:00
|
|
|
API_REQUEST_HEADERS,
|
2020-12-08 19:13:48 +00:00
|
|
|
API_STATUS_CODES,
|
2019-11-25 05:07:27 +00:00
|
|
|
} from "constants/ApiConstants";
|
2019-11-13 07:34:59 +00:00
|
|
|
import { ActionApiResponse } from "./ActionAPI";
|
2020-11-04 11:40:59 +00:00
|
|
|
import { AUTH_LOGIN_URL, PAGE_NOT_FOUND_URL } from "constants/routes";
|
2020-04-15 14:19:39 +00:00
|
|
|
import history from "utils/history";
|
2020-08-19 09:21:32 +00:00
|
|
|
import { convertObjectToQueryParams } from "utils/AppsmithUtils";
|
2020-11-04 11:40:59 +00:00
|
|
|
import { SERVER_API_TIMEOUT_ERROR } from "../constants/messages";
|
2019-03-26 15:28:24 +00:00
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
//TODO(abhinav): Refactor this to make more composable.
|
|
|
|
|
export const apiRequestConfig = {
|
2020-07-07 10:22:17 +00:00
|
|
|
baseURL: "/api/",
|
2019-03-26 15:28:24 +00:00
|
|
|
timeout: REQUEST_TIMEOUT_MS,
|
2019-12-16 08:49:10 +00:00
|
|
|
headers: API_REQUEST_HEADERS,
|
2019-09-18 10:48:56 +00:00
|
|
|
withCredentials: true,
|
2019-12-16 08:49:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const axiosInstance: AxiosInstance = axios.create();
|
2019-03-26 15:28:24 +00:00
|
|
|
|
2020-11-04 11:40:59 +00:00
|
|
|
export const axiosConnectionAbortedCode = "ECONNABORTED";
|
2019-10-29 12:02:58 +00:00
|
|
|
const executeActionRegex = /actions\/execute/;
|
2020-11-04 11:40:59 +00:00
|
|
|
const timeoutErrorRegex = /timeout of (\d+)ms exceeded/;
|
|
|
|
|
|
2019-10-29 12:02:58 +00:00
|
|
|
axiosInstance.interceptors.request.use((config: any) => {
|
|
|
|
|
return { ...config, timer: performance.now() };
|
|
|
|
|
});
|
|
|
|
|
|
2019-11-12 09:43:13 +00:00
|
|
|
const makeExecuteActionResponse = (response: any): ActionApiResponse => ({
|
2019-10-29 12:02:58 +00:00
|
|
|
...response.data,
|
2019-11-12 09:43:13 +00:00
|
|
|
clientMeta: {
|
|
|
|
|
size: response.headers["content-length"],
|
|
|
|
|
duration: Number(performance.now() - response.config.timer).toFixed(),
|
|
|
|
|
},
|
2019-10-29 12:02:58 +00:00
|
|
|
});
|
|
|
|
|
|
2020-08-03 14:18:48 +00:00
|
|
|
const is404orAuthPath = () => {
|
|
|
|
|
const pathName = window.location.pathname;
|
|
|
|
|
return /^\/404/.test(pathName) || /^\/user\/\w+/.test(pathName);
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-26 15:28:24 +00:00
|
|
|
axiosInstance.interceptors.response.use(
|
2019-10-29 12:02:58 +00:00
|
|
|
(response: any): any => {
|
|
|
|
|
if (response.config.url.match(executeActionRegex)) {
|
|
|
|
|
return makeExecuteActionResponse(response);
|
|
|
|
|
}
|
2019-03-26 15:28:24 +00:00
|
|
|
// Do something with response data
|
2019-09-09 09:08:54 +00:00
|
|
|
return response.data;
|
2019-03-26 15:28:24 +00:00
|
|
|
},
|
|
|
|
|
function(error: any) {
|
2020-11-04 11:40:59 +00:00
|
|
|
// Return if the call was cancelled via cancel token
|
2020-10-15 15:28:54 +00:00
|
|
|
if (axios.isCancel(error)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-04 11:40:59 +00:00
|
|
|
// Return modified response if action execution failed
|
2020-10-15 15:28:54 +00:00
|
|
|
if (error.config && error.config.url.match(executeActionRegex)) {
|
2019-10-29 12:02:58 +00:00
|
|
|
return makeExecuteActionResponse(error.response);
|
|
|
|
|
}
|
2020-11-04 11:40:59 +00:00
|
|
|
// Return error if any timeout happened in other api calls
|
|
|
|
|
if (
|
|
|
|
|
error.code === axiosConnectionAbortedCode &&
|
|
|
|
|
error.message &&
|
|
|
|
|
error.message.match(timeoutErrorRegex)
|
|
|
|
|
) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
...error,
|
|
|
|
|
message: SERVER_API_TIMEOUT_ERROR,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-12-08 19:13:48 +00:00
|
|
|
if (error.response.status === API_STATUS_CODES.SERVER_ERROR) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
...error,
|
|
|
|
|
crash: true,
|
|
|
|
|
message: SERVER_API_TIMEOUT_ERROR,
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-26 15:28:24 +00:00
|
|
|
if (error.response) {
|
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
2019-12-16 08:49:10 +00:00
|
|
|
// console.log(error.response.data);
|
|
|
|
|
// console.log(error.response.status);
|
|
|
|
|
// console.log(error.response.headers);
|
2020-08-03 14:18:48 +00:00
|
|
|
if (!is404orAuthPath()) {
|
2020-08-18 08:48:06 +00:00
|
|
|
const currentUrl = `${window.location.href}`;
|
2020-12-08 19:13:48 +00:00
|
|
|
if (error.response.status === API_STATUS_CODES.REQUEST_NOT_AUTHORISED) {
|
2020-08-03 14:18:48 +00:00
|
|
|
// Redirect to login and set a redirect url.
|
|
|
|
|
history.replace({
|
|
|
|
|
pathname: AUTH_LOGIN_URL,
|
|
|
|
|
search: `redirectTo=${currentUrl}`,
|
|
|
|
|
});
|
2020-05-05 12:16:51 +00:00
|
|
|
return Promise.reject({
|
2020-12-08 19:13:48 +00:00
|
|
|
code: API_STATUS_CODES.REQUEST_NOT_AUTHORISED,
|
2020-05-05 12:16:51 +00:00
|
|
|
message: "Unauthorized. Redirecting to login page...",
|
|
|
|
|
show: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-03 14:18:48 +00:00
|
|
|
const errorData = error.response.data.responseMeta;
|
2020-12-08 19:13:48 +00:00
|
|
|
if (
|
|
|
|
|
errorData.status === API_STATUS_CODES.RESOURCE_NOT_FOUND &&
|
|
|
|
|
errorData.error.code === 4028
|
|
|
|
|
) {
|
2020-08-03 14:18:48 +00:00
|
|
|
history.replace({
|
|
|
|
|
pathname: PAGE_NOT_FOUND_URL,
|
|
|
|
|
search: `redirectTo=${currentUrl}`,
|
|
|
|
|
});
|
|
|
|
|
return Promise.reject({
|
2020-12-08 19:13:48 +00:00
|
|
|
code: API_STATUS_CODES.RESOURCE_NOT_FOUND,
|
2020-08-03 14:18:48 +00:00
|
|
|
message: "Resource Not Found",
|
|
|
|
|
show: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-07-08 10:14:03 +00:00
|
|
|
}
|
2020-02-21 12:16:49 +00:00
|
|
|
if (error.response.data.responseMeta) {
|
|
|
|
|
return Promise.resolve(error.response.data);
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
|
|
|
|
return Promise.reject(error.response.data);
|
2019-03-26 15:28:24 +00:00
|
|
|
} else if (error.request) {
|
|
|
|
|
// The request was made but no response was received
|
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
|
// http.ClientRequest in node.js
|
2019-09-09 09:08:54 +00:00
|
|
|
console.log(error.request);
|
2019-03-26 15:28:24 +00:00
|
|
|
} else {
|
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
2019-10-29 12:02:58 +00:00
|
|
|
console.error("Error", error.message);
|
2019-03-26 15:28:24 +00:00
|
|
|
}
|
2019-09-09 09:08:54 +00:00
|
|
|
console.log(error.config);
|
2020-02-21 12:16:49 +00:00
|
|
|
return Promise.resolve(error);
|
2019-09-09 09:08:54 +00:00
|
|
|
},
|
|
|
|
|
);
|
2019-03-26 15:28:24 +00:00
|
|
|
|
|
|
|
|
class Api {
|
2019-12-16 08:49:10 +00:00
|
|
|
static get(
|
|
|
|
|
url: string,
|
|
|
|
|
queryParams?: any,
|
2020-10-15 11:38:55 +00:00
|
|
|
config: Partial<AxiosRequestConfig> = {},
|
2019-12-16 08:49:10 +00:00
|
|
|
) {
|
2020-10-15 11:38:55 +00:00
|
|
|
return axiosInstance.get(url + convertObjectToQueryParams(queryParams), {
|
|
|
|
|
...apiRequestConfig,
|
|
|
|
|
...config,
|
|
|
|
|
});
|
2019-03-26 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
static post(
|
|
|
|
|
url: string,
|
|
|
|
|
body?: any,
|
|
|
|
|
queryParams?: any,
|
2020-10-15 11:38:55 +00:00
|
|
|
config: Partial<AxiosRequestConfig> = {},
|
2019-12-16 08:49:10 +00:00
|
|
|
) {
|
2019-03-26 15:28:24 +00:00
|
|
|
return axiosInstance.post(
|
2020-08-19 09:21:32 +00:00
|
|
|
url + convertObjectToQueryParams(queryParams),
|
2019-09-09 09:08:54 +00:00
|
|
|
body,
|
2020-10-15 11:38:55 +00:00
|
|
|
{
|
|
|
|
|
...apiRequestConfig,
|
|
|
|
|
...config,
|
|
|
|
|
},
|
2019-09-09 09:08:54 +00:00
|
|
|
);
|
2019-03-26 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
static put(
|
|
|
|
|
url: string,
|
|
|
|
|
body?: any,
|
|
|
|
|
queryParams?: any,
|
2020-10-15 11:38:55 +00:00
|
|
|
config: Partial<AxiosRequestConfig> = {},
|
2019-12-16 08:49:10 +00:00
|
|
|
) {
|
2019-09-18 10:48:56 +00:00
|
|
|
return axiosInstance.put(
|
2020-08-19 09:21:32 +00:00
|
|
|
url + convertObjectToQueryParams(queryParams),
|
2019-09-18 10:48:56 +00:00
|
|
|
body,
|
2020-10-15 11:38:55 +00:00
|
|
|
{
|
|
|
|
|
...apiRequestConfig,
|
|
|
|
|
...config,
|
|
|
|
|
},
|
2019-09-18 10:48:56 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
static delete(
|
|
|
|
|
url: string,
|
|
|
|
|
queryParams?: any,
|
2020-10-15 11:38:55 +00:00
|
|
|
config: Partial<AxiosRequestConfig> = {},
|
2019-12-16 08:49:10 +00:00
|
|
|
) {
|
2020-10-15 11:38:55 +00:00
|
|
|
return axiosInstance.delete(url + convertObjectToQueryParams(queryParams), {
|
|
|
|
|
...apiRequestConfig,
|
|
|
|
|
...config,
|
|
|
|
|
});
|
2019-10-21 15:12:45 +00:00
|
|
|
}
|
2019-03-26 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-13 11:59:45 +00:00
|
|
|
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
|
2019-09-12 11:19:38 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default Api;
|