You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.4 KiB
131 lines
3.4 KiB
import axios, { type InternalAxiosRequestConfig } from "axios";
|
|
import { authBridge } from "./auth-bridge";
|
|
import { getClientCookie } from "./cookies";
|
|
import { isLocale } from "../translations/config";
|
|
|
|
const PROXY_PATH_PARAM = "__proxyPath";
|
|
const LOCALHOST_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);
|
|
|
|
function isAbsoluteUrl(url: string) {
|
|
return /^[a-z][a-z\d+\-.]*:\/\//i.test(url);
|
|
}
|
|
|
|
function shouldUseProxy() {
|
|
if (process.env.NEXT_PUBLIC_DISABLE_PROXY === "true") {
|
|
return false;
|
|
}
|
|
|
|
if (typeof window === "undefined") {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function withProxyPathParam(
|
|
params: InternalAxiosRequestConfig["params"],
|
|
proxyPath: string,
|
|
) {
|
|
if (params instanceof URLSearchParams) {
|
|
const nextParams = new URLSearchParams(params);
|
|
nextParams.set(PROXY_PATH_PARAM, proxyPath);
|
|
return nextParams;
|
|
}
|
|
|
|
if (typeof params === "string") {
|
|
const nextParams = new URLSearchParams(params);
|
|
nextParams.set(PROXY_PATH_PARAM, proxyPath);
|
|
return nextParams;
|
|
}
|
|
|
|
return {
|
|
...(params && typeof params === "object" ? params : {}),
|
|
[PROXY_PATH_PARAM]: proxyPath,
|
|
};
|
|
}
|
|
|
|
export function getApiRequestUrl(path: string) {
|
|
if (!isAbsoluteUrl(path) && shouldUseProxy()) {
|
|
const searchParams = new URLSearchParams({
|
|
[PROXY_PATH_PARAM]: path,
|
|
});
|
|
|
|
return `/api/proxy?${searchParams.toString()}`;
|
|
}
|
|
|
|
return `${process.env.NEXT_PUBLIC_API_BASE_URL}${path}`;
|
|
}
|
|
|
|
export const http = axios.create({
|
|
baseURL: shouldUseProxy()
|
|
? "/api/proxy"
|
|
: process.env.NEXT_PUBLIC_API_BASE_URL,
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
withCredentials: true,
|
|
});
|
|
|
|
http.interceptors.request.use((config) => {
|
|
if (shouldUseProxy() && config.url && !isAbsoluteUrl(config.url)) {
|
|
config.params = withProxyPathParam(config.params, config.url);
|
|
config.url = "";
|
|
}
|
|
|
|
// Cache-busting: add timestamp to GET requests so browser never serves stale data
|
|
if (!config.method || config.method.toLowerCase() === "get") {
|
|
config.params = {
|
|
...(config.params && typeof config.params === "object"
|
|
? config.params
|
|
: {}),
|
|
_t: Date.now(),
|
|
};
|
|
}
|
|
|
|
// Tell browser not to cache API responses
|
|
config.headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
|
|
config.headers["Pragma"] = "no-cache";
|
|
|
|
const stripNoToken = (v: string | null) =>
|
|
v && v !== "NO_TOKEN" ? v : null;
|
|
|
|
const token =
|
|
authBridge.getToken() ??
|
|
stripNoToken(getClientCookie("HABIB_TOKEN")) ??
|
|
stripNoToken(getClientCookie("habib_token"));
|
|
|
|
if (token) {
|
|
config.headers.Authorization = `Token ${token}`;
|
|
}
|
|
|
|
let lang: string | undefined =
|
|
getClientCookie("HABIB_LANGUAGE") ??
|
|
getClientCookie("habib_language") ??
|
|
undefined;
|
|
if (!isLocale(lang)) {
|
|
const pathSegment =
|
|
typeof window !== "undefined"
|
|
? window.location.pathname.split("/")[1]
|
|
: undefined;
|
|
if (isLocale(pathSegment)) {
|
|
lang = pathSegment;
|
|
} else {
|
|
const docLang =
|
|
typeof document !== "undefined"
|
|
? document.documentElement.lang
|
|
: undefined;
|
|
lang = isLocale(docLang) ? docLang : "fa";
|
|
}
|
|
}
|
|
|
|
config.headers["Accept-Language"] = lang;
|
|
config.headers["X-User-Language"] = lang;
|
|
|
|
return config;
|
|
});
|
|
|
|
http.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => Promise.reject(error),
|
|
);
|