fix: vite config and routes to support no slashes, and api interceptor for fetch (#3824)
* Fix vite config and routes to support no slashes * Added fetch intercept to add custom headers into fetch calls
This commit is contained in:
parent
2f1f1808b9
commit
3e11d8a64d
5 changed files with 26 additions and 6 deletions
7
src/frontend/package-lock.json
generated
7
src/frontend/package-lock.json
generated
|
|
@ -48,6 +48,7 @@
|
||||||
"elkjs": "^0.9.3",
|
"elkjs": "^0.9.3",
|
||||||
"emoji-regex": "^10.3.0",
|
"emoji-regex": "^10.3.0",
|
||||||
"esbuild": "^0.21.5",
|
"esbuild": "^0.21.5",
|
||||||
|
"fetch-intercept": "^2.4.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
"framer-motion": "^11.2.10",
|
"framer-motion": "^11.2.10",
|
||||||
"fuse.js": "^7.0.0",
|
"fuse.js": "^7.0.0",
|
||||||
|
|
@ -8617,6 +8618,12 @@
|
||||||
"node": "^12.20 || >= 14.13"
|
"node": "^12.20 || >= 14.13"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/fetch-intercept": {
|
||||||
|
"version": "2.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/fetch-intercept/-/fetch-intercept-2.4.0.tgz",
|
||||||
|
"integrity": "sha512-BPZ2LM9Dh1ua2ovQf03N6rhWg1qxdVD5qK/G4llvcemt6M+jjxCuIDxJ+6IiG+uz//3UQmgfKEv0gOGvYIxZ7g==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/fetch-retry": {
|
"node_modules/fetch-retry": {
|
||||||
"version": "6.0.0",
|
"version": "6.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz",
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@
|
||||||
"elkjs": "^0.9.3",
|
"elkjs": "^0.9.3",
|
||||||
"emoji-regex": "^10.3.0",
|
"emoji-regex": "^10.3.0",
|
||||||
"esbuild": "^0.21.5",
|
"esbuild": "^0.21.5",
|
||||||
|
"fetch-intercept": "^2.4.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
"framer-motion": "^11.2.10",
|
"framer-motion": "^11.2.10",
|
||||||
"fuse.js": "^7.0.0",
|
"fuse.js": "^7.0.0",
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
|
||||||
import { useCustomApiHeaders } from "@/customization/hooks/use-custom-api-headers";
|
import { useCustomApiHeaders } from "@/customization/hooks/use-custom-api-headers";
|
||||||
import useAuthStore from "@/stores/authStore";
|
import useAuthStore from "@/stores/authStore";
|
||||||
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
|
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
|
||||||
|
import * as fetchIntercept from "fetch-intercept";
|
||||||
import { useContext, useEffect } from "react";
|
import { useContext, useEffect } from "react";
|
||||||
import { Cookies } from "react-cookie";
|
import { Cookies } from "react-cookie";
|
||||||
import { BuildStatus } from "../../constants/enums";
|
import { BuildStatus } from "../../constants/enums";
|
||||||
|
|
@ -27,6 +28,20 @@ function ApiInterceptor() {
|
||||||
const customHeaders = useCustomApiHeaders();
|
const customHeaders = useCustomApiHeaders();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const unregister = fetchIntercept.register({
|
||||||
|
request: function (url, config) {
|
||||||
|
const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
||||||
|
if (accessToken && !isAuthorizedURL(config?.url)) {
|
||||||
|
config.headers["Authorization"] = `Bearer ${accessToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(customHeaders)) {
|
||||||
|
config.headers[key] = value;
|
||||||
|
}
|
||||||
|
return [url, config];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const interceptor = api.interceptors.response.use(
|
const interceptor = api.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
async (error: AxiosError) => {
|
async (error: AxiosError) => {
|
||||||
|
|
@ -127,6 +142,7 @@ function ApiInterceptor() {
|
||||||
// Clean up the interceptors when the component unmounts
|
// Clean up the interceptors when the component unmounts
|
||||||
api.interceptors.response.eject(interceptor);
|
api.interceptors.response.eject(interceptor);
|
||||||
api.interceptors.request.eject(requestInterceptor);
|
api.interceptors.request.eject(requestInterceptor);
|
||||||
|
unregister();
|
||||||
};
|
};
|
||||||
}, [accessToken, setErrorData, customHeaders, autoLogin]);
|
}, [accessToken, setErrorData, customHeaders, autoLogin]);
|
||||||
|
|
||||||
|
|
@ -222,10 +238,6 @@ async function performStreamingRequest({
|
||||||
// this flag is fundamental to ensure server stops tasks when client disconnects
|
// this flag is fundamental to ensure server stops tasks when client disconnects
|
||||||
Connection: "close",
|
Connection: "close",
|
||||||
};
|
};
|
||||||
const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
|
||||||
if (accessToken) {
|
|
||||||
headers["Authorization"] = `Bearer ${accessToken}`;
|
|
||||||
}
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
useFlowStore.getState().setBuildController(controller);
|
useFlowStore.getState().setBuildController(controller);
|
||||||
const params = {
|
const params = {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ const SignUp = lazy(() => import("./pages/SignUpPage"));
|
||||||
const router = createBrowserRouter(
|
const router = createBrowserRouter(
|
||||||
createRoutesFromElements([
|
createRoutesFromElements([
|
||||||
<Route
|
<Route
|
||||||
path={ENABLE_CUSTOM_PARAM ? "/:customParam" : "/"}
|
path={ENABLE_CUSTOM_PARAM ? "/:customParam?" : "/"}
|
||||||
element={
|
element={
|
||||||
<ContextWrapper>
|
<ContextWrapper>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ export default defineConfig(({ mode }) => {
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
basename: (BASENAME || "") + "/",
|
basename: BASENAME || "",
|
||||||
build: {
|
build: {
|
||||||
outDir: "build",
|
outDir: "build",
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue