Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vite config and routes to support no slashes, and api interceptor for fetch #3824

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading