Skip to content

Commit

Permalink
fix: vite config and routes to support no slashes, and api intercepto…
Browse files Browse the repository at this point in the history
…r for fetch (langflow-ai#3824)

* Fix vite config and routes to support no slashes

* Added fetch intercept to add custom headers into fetch calls
  • Loading branch information
lucaseduoli authored and diogocabral committed Nov 26, 2024
1 parent 00e9f83 commit 37fbcfd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"elkjs": "^0.9.3",
"emoji-regex": "^10.3.0",
"esbuild": "^0.21.5",
"fetch-intercept": "^2.4.0",
"file-saver": "^2.0.5",
"framer-motion": "^11.2.10",
"fuse.js": "^7.0.0",
Expand Down
20 changes: 16 additions & 4 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LANGFLOW_ACCESS_TOKEN } from "@/constants/constants";
import { useCustomApiHeaders } from "@/customization/hooks/use-custom-api-headers";
import useAuthStore from "@/stores/authStore";
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
import * as fetchIntercept from "fetch-intercept";
import { useContext, useEffect } from "react";
import { Cookies } from "react-cookie";
import { BuildStatus } from "../../constants/enums";
Expand All @@ -27,6 +28,20 @@ function ApiInterceptor() {
const customHeaders = useCustomApiHeaders();

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(
(response) => response,
async (error: AxiosError) => {
Expand Down Expand Up @@ -127,6 +142,7 @@ function ApiInterceptor() {
// Clean up the interceptors when the component unmounts
api.interceptors.response.eject(interceptor);
api.interceptors.request.eject(requestInterceptor);
unregister();
};
}, [accessToken, setErrorData, customHeaders, autoLogin]);

Expand Down Expand Up @@ -222,10 +238,6 @@ async function performStreamingRequest({
// this flag is fundamental to ensure server stops tasks when client disconnects
Connection: "close",
};
const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
if (accessToken) {
headers["Authorization"] = `Bearer ${accessToken}`;
}
const controller = new AbortController();
useFlowStore.getState().setBuildController(controller);
const params = {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const SignUp = lazy(() => import("./pages/SignUpPage"));
const router = createBrowserRouter(
createRoutesFromElements([
<Route
path={ENABLE_CUSTOM_PARAM ? "/:customParam" : "/"}
path={ENABLE_CUSTOM_PARAM ? "/:customParam?" : "/"}
element={
<ContextWrapper>
<Outlet />
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineConfig(({ mode }) => {
}, {});

return {
basename: (BASENAME || "") + "/",
basename: BASENAME || "",
build: {
outDir: "build",
},
Expand Down

0 comments on commit 37fbcfd

Please sign in to comment.