PromucFlow_constructor/app/client/src/api/Api.tsx

179 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-09-09 09:08:54 +00:00
import _ from "lodash";
2019-12-16 08:49:10 +00:00
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import {
REQUEST_TIMEOUT_MS,
2019-12-16 08:49:10 +00:00
API_REQUEST_HEADERS,
2019-11-25 05:07:27 +00:00
} from "constants/ApiConstants";
2019-11-13 07:34:59 +00:00
import { ActionApiResponse } from "./ActionAPI";
import {
AUTH_LOGIN_URL,
PAGE_NOT_FOUND_URL,
SERVER_ERROR_URL,
} from "constants/routes";
2020-04-15 14:19:39 +00:00
import history from "utils/history";
2019-12-16 08:49:10 +00:00
//TODO(abhinav): Refactor this to make more composable.
export const apiRequestConfig = {
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
baseURL: "/api/",
timeout: REQUEST_TIMEOUT_MS,
2019-12-16 08:49:10 +00:00
headers: API_REQUEST_HEADERS,
withCredentials: true,
2019-12-16 08:49:10 +00:00
};
const axiosInstance: AxiosInstance = axios.create();
const executeActionRegex = /actions\/execute/;
const currentUserRegex = /\/me$/;
axiosInstance.interceptors.request.use((config: any) => {
return { ...config, timer: performance.now() };
});
2019-11-12 09:43:13 +00:00
const makeExecuteActionResponse = (response: any): ActionApiResponse => ({
...response.data,
2019-11-12 09:43:13 +00:00
clientMeta: {
size: response.headers["content-length"],
duration: Number(performance.now() - response.config.timer).toFixed(),
},
});
const is404orAuthPath = () => {
const pathName = window.location.pathname;
return /^\/404/.test(pathName) || /^\/user\/\w+/.test(pathName);
};
axiosInstance.interceptors.response.use(
(response: any): any => {
if (response.config.url.match(executeActionRegex)) {
return makeExecuteActionResponse(response);
}
// Do something with response data
2019-09-09 09:08:54 +00:00
return response.data;
},
function(error: any) {
2020-01-16 11:46:21 +00:00
if (error.code === "ECONNABORTED") {
if (error.config.url.match(currentUserRegex)) {
history.replace({ pathname: SERVER_ERROR_URL });
}
2020-02-06 07:01:25 +00:00
return Promise.reject({
message: "Please check your internet connection",
});
2020-01-16 11:46:21 +00:00
}
if (error.config.url.match(executeActionRegex)) {
return makeExecuteActionResponse(error.response);
}
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);
if (!is404orAuthPath()) {
const currentUrl = `${window.location.href}`;
if (error.response.status === 401) {
// 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({
code: 401,
message: "Unauthorized. Redirecting to login page...",
show: false,
});
}
const errorData = error.response.data.responseMeta;
if (errorData.status === 404 && errorData.error.code === 4028) {
history.replace({
pathname: PAGE_NOT_FOUND_URL,
search: `redirectTo=${currentUrl}`,
});
return Promise.reject({
code: 404,
message: "Resource Not Found",
show: false,
});
}
}
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);
} 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);
} else {
// Something happened in setting up the request that triggered an Error
console.error("Error", error.message);
}
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
},
);
class Api {
2019-12-16 08:49:10 +00:00
static get(
url: string,
queryParams?: any,
config?: Partial<AxiosRequestConfig>,
) {
2019-09-09 09:08:54 +00:00
return axiosInstance.get(
url + this.convertObjectToQueryParams(queryParams),
2019-12-16 08:49:10 +00:00
_.merge(apiRequestConfig, config),
2019-09-09 09:08:54 +00:00
);
}
2019-12-16 08:49:10 +00:00
static post(
url: string,
body?: any,
queryParams?: any,
config?: Partial<AxiosRequestConfig>,
) {
return axiosInstance.post(
url + this.convertObjectToQueryParams(queryParams),
2019-09-09 09:08:54 +00:00
body,
2019-12-16 08:49:10 +00:00
_.merge(apiRequestConfig, config),
2019-09-09 09:08:54 +00:00
);
}
2019-12-16 08:49:10 +00:00
static put(
url: string,
body?: any,
queryParams?: any,
config?: Partial<AxiosRequestConfig>,
) {
return axiosInstance.put(
url + this.convertObjectToQueryParams(queryParams),
body,
2019-12-16 08:49:10 +00:00
_.merge(apiRequestConfig, config),
);
}
2019-12-16 08:49:10 +00:00
static delete(
url: string,
queryParams?: any,
config?: Partial<AxiosRequestConfig>,
) {
2019-10-21 15:12:45 +00:00
return axiosInstance.delete(
url + this.convertObjectToQueryParams(queryParams),
2019-12-16 08:49:10 +00:00
_.merge(apiRequestConfig, config),
2019-10-21 15:12:45 +00:00
);
}
static convertObjectToQueryParams(object: any): string {
if (!_.isNil(object)) {
const paramArray: string[] = _.map(_.keys(object), key => {
2019-09-09 09:08:54 +00:00
return encodeURIComponent(key) + "=" + encodeURIComponent(object[key]);
});
return "?" + _.join(paramArray, "&");
} else {
2019-09-09 09:08:54 +00:00
return "";
}
}
}
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
2019-09-09 09:08:54 +00:00
export default Api;