Farmbot-Web-App/frontend/interceptors.ts

79 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2019-06-24 15:39:49 -06:00
import { SafeError, isSafeError } from "./interceptor_support";
2017-06-29 12:54:02 -06:00
import { API } from "./api/index";
import { AuthState } from "./auth/interfaces";
2017-08-02 09:14:08 -06:00
import { AxiosRequestConfig, AxiosResponse } from "axios";
import { Content } from "./constants";
import { dispatchNetworkUp, dispatchNetworkDown } from "./connectivity/index";
2017-12-22 08:19:30 -07:00
import { Dictionary } from "farmbot";
import { outstandingRequests } from "./connectivity/data_consistency";
2018-03-29 08:54:37 -06:00
import { Session } from "./session";
2019-02-04 07:32:26 -07:00
import { get } from "lodash";
2019-04-02 13:59:37 -06:00
import { t } from "./i18next_wrapper";
2019-06-24 15:39:49 -06:00
import { error } from "./toast/toast";
import { now } from "./devices/connectivity/qos";
2017-06-29 12:54:02 -06:00
2017-07-05 14:42:41 -06:00
export function responseFulfilled(input: AxiosResponse): AxiosResponse {
dispatchNetworkUp("user.api", now());
2017-06-29 12:54:02 -06:00
return input;
}
2019-02-04 07:32:26 -07:00
/** These will raise type errors if our get usage ever requires changing. */
2018-03-29 08:54:37 -06:00
const request: keyof SafeError = "request";
const responseUrl: keyof SafeError["request"] = "responseURL";
export const isLocalRequest = (x: SafeError) =>
2019-02-04 07:32:26 -07:00
get(x, [request, responseUrl], "").includes(API.current.baseUrl);
2018-03-29 08:54:37 -06:00
let ONLY_ONCE = true;
2017-06-29 12:54:02 -06:00
export function responseRejected(x: SafeError | undefined) {
if (x && isSafeError(x)) {
dispatchNetworkUp("user.api", now());
const a = ![451, 401, 422].includes(x.response.status);
const b = x.response.status > 399;
2017-06-29 12:54:02 -06:00
// Openfarm API was sending too many 404's.
2019-02-04 07:32:26 -07:00
const c = !get(x, "response.config.url", "").includes("openfarm.cc/");
2017-06-29 12:54:02 -06:00
if (a && b && c) {
setTimeout(() => {
// Explicitly throw error so error reporting tool will save it.
const msg = `Bad response: ${x.response.status} ${JSON.stringify(x.response)}`;
2017-06-29 12:54:02 -06:00
throw new Error(msg);
}, 1);
}
switch (x.response.status) {
2018-03-29 08:54:37 -06:00
case 401:
isLocalRequest(x) && Session.clear();
break;
2017-06-29 12:54:02 -06:00
case 404:
// Log 404's, but don't generate any noise for the user.
break;
case 500:
error(t("Unexpected error occurred, we've been notified of the problem."));
break;
case 451:
// DONT REFACTOR: I want to use alert() because it's blocking.
ONLY_ONCE && alert(t(Content.TOS_UPDATE));
ONLY_ONCE = false;
2018-05-15 20:38:52 -06:00
window.location.assign("/tos_update");
2017-06-29 12:54:02 -06:00
break;
}
return Promise.reject(x);
} else {
dispatchNetworkDown("user.api", now());
2017-06-29 12:54:02 -06:00
return Promise.reject(x);
}
}
export function requestFulfilled(auth: AuthState) {
return (config: AxiosRequestConfig) => {
const req = config.url || "";
const isAPIRequest = req.includes(API.current.baseUrl);
2017-06-29 12:54:02 -06:00
if (isAPIRequest) {
config.headers = config.headers || {};
2017-12-22 08:19:30 -07:00
const headers: Dictionary<string> = config.headers;
headers["X-Farmbot-Rpc-Id"] = outstandingRequests.last;
headers.Authorization = auth.token.encoded || "CANT_FIND_TOKEN";
2017-06-29 12:54:02 -06:00
}
return config;
};
}