From 21440578a78e97a4e766cab0e2b383413d2c39ab Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 11 Jun 2023 12:17:00 -0300 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=A7=20chore(API):=20add=20/api/v1/?= =?UTF-8?q?=20prefix=20to=20API=20routes=20=E2=9C=A8=20feat(API):=20add=20?= =?UTF-8?q?getVersion=20function=20to=20retrieve=20the=20version=20of=20th?= =?UTF-8?q?e=20API=20The=20API=20routes=20have=20been=20updated=20to=20inc?= =?UTF-8?q?lude=20the=20/api/v1/=20prefix=20to=20improve=20semantics=20and?= =?UTF-8?q?=20avoid=20conflicts=20with=20other=20routes.=20The=20getVersio?= =?UTF-8?q?n=20function=20has=20been=20added=20to=20retrieve=20the=20versi?= =?UTF-8?q?on=20of=20the=20API.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/controllers/API/index.ts | 38 ++++++++++++++++++++--- src/frontend/vite.config.ts | 12 +++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/frontend/src/controllers/API/index.ts b/src/frontend/src/controllers/API/index.ts index ffa9eef72..794d3aa97 100644 --- a/src/frontend/src/controllers/API/index.ts +++ b/src/frontend/src/controllers/API/index.ts @@ -3,24 +3,52 @@ import { APIObjectType, sendAllProps } from "../../types/api/index"; import axios, { AxiosResponse } from "axios"; import { FlowType } from "../../types/flow"; +// when serving with static files +// We need to add /api/v1/ to the url in the axios calls + +/** + * Retrieves all data from the API. + * @returns {Promise>} A promise that resolves to an AxiosResponse object containing the API data. + */ export async function getAll(): Promise> { - return await axios.get(`/all`); + return await axios.get(`/api/v1/all`); } export async function sendAll(data: sendAllProps) { - return await axios.post(`/predict`, data); + return await axios.post(`/api/v1/predict`, data); } -export async function checkCode( +export async function postValidateCode( code: string ): Promise> { - return await axios.post("/validate/code", { code }); + return await axios.post("/api/v1/validate/code", { code }); +} + +export async function postValidateNode( + nodeId: string, + data: any +): Promise> { + return await axios.post(`/api/v1/validate/node/${nodeId}`, { data }); } export async function checkPrompt( template: string ): Promise> { - return await axios.post("/validate/prompt", { template }); + return await axios.post("/api/v1/validate/prompt", { template }); +} + +/** + * Retrieves the version of the API. + * @returns {Promise>} A promise that resolves to an AxiosResponse object containing the API version. + * @example + * const response = await getVersion(); + * console.log(response.data.version); + * // 0.1.0 + */ +export async function getVersion(): Promise< + AxiosResponse<{ version: string }> +> { + return await axios.get("/api/v1/version"); } export async function getExamples(): Promise { diff --git a/src/frontend/vite.config.ts b/src/frontend/vite.config.ts index b1e7bdd66..6917ecc28 100644 --- a/src/frontend/vite.config.ts +++ b/src/frontend/vite.config.ts @@ -2,11 +2,11 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react-swc"; import svgr from "vite-plugin-svgr"; const apiRoutes = [ - "/all", - "/predict", - "^/validate/*", - "^/chat/*", - "/version", + "/api/v1/all", + "/api/v1/predict", + "/api/v1/validate/*", + "/api/v1/chat/*", + "/api/v1/version", "/health", ]; @@ -19,7 +19,7 @@ const proxyTargets = apiRoutes.reduce((proxyObj, route) => { changeOrigin: true, secure: false, ws: true, - rewrite: (path) => `/api/v1${path}`, + // rewrite: (path) => `/api/v1${path}`, }; return proxyObj; }, {}); From 10fce79bc366acaae07673ba0e1a5d215ee0f1d0 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 11 Jun 2023 12:17:16 -0300 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20fix(App.tsx):=20replace=20fe?= =?UTF-8?q?tch=20call=20with=20getVersion=20function=20call=20to=20retriev?= =?UTF-8?q?e=20app=20version=20=E2=9C=A8=20feat(App.tsx):=20add=20getVersi?= =?UTF-8?q?on=20function=20to=20retrieve=20app=20version=20from=20API=20Th?= =?UTF-8?q?e=20fetch=20call=20to=20retrieve=20the=20app=20version=20has=20?= =?UTF-8?q?been=20replaced=20with=20a=20call=20to=20the=20getVersion=20fun?= =?UTF-8?q?ction.=20This=20function=20retrieves=20the=20app=20version=20fr?= =?UTF-8?q?om=20the=20API.=20This=20change=20improves=20the=20code's=20rea?= =?UTF-8?q?dability=20and=20reduces=20the=20amount=20of=20code=20needed=20?= =?UTF-8?q?to=20retrieve=20the=20app=20version.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(GenericNode/index.tsx): replace fetch call with postValidateNode function call to validate node ✨ feat(GenericNode/index.tsx): add postValidateNode function to validate node via API The fetch call to validate the node has been replaced with a call to the postValidateNode function. This function validates the node via the API. This change improves the code's readability and reduces the amount of code needed to validate the node. 🐛 fix(codeAreaModal/index.tsx): replace checkCode function call with postValidateCode function call to validate code ✨ feat(codeAreaModal/index.tsx): add postValidateCode function to validate code via API The checkCode function call to validate the code has been replaced with a call to the postValidateCode function. This function validates the code via the API. This change improves the code's readability and reduces the amount of code needed to validate the code. --- src/frontend/src/App.tsx | 9 ++++--- .../src/CustomNodes/GenericNode/index.tsx | 24 ++++++++----------- .../src/modals/codeAreaModal/index.tsx | 4 ++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index f68c3ea73..e081cf490 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -14,6 +14,7 @@ import TabsManagerComponent from "./pages/FlowPage/components/tabsManagerCompone import { ErrorBoundary } from "react-error-boundary"; import CrashErrorComponent from "./components/CrashErrorComponent"; import { TabsContext } from "./contexts/tabsContext"; +import { getVersion } from "./controllers/API"; export default function App() { let { setCurrent, setShowSideBar, setIsStackedOpen } = @@ -49,11 +50,9 @@ export default function App() { // Initialize state variable for the version const [version, setVersion] = useState(""); useEffect(() => { - fetch("/version") - .then((res) => res.json()) - .then((data) => { - setVersion(data.version); - }); + getVersion().then((response) => { + setVersion(response.data.version); + }); }, []); // Use effect hook to update alertsList when a new alert is added useEffect(() => { diff --git a/src/frontend/src/CustomNodes/GenericNode/index.tsx b/src/frontend/src/CustomNodes/GenericNode/index.tsx index 86eaf299d..c449296ef 100644 --- a/src/frontend/src/CustomNodes/GenericNode/index.tsx +++ b/src/frontend/src/CustomNodes/GenericNode/index.tsx @@ -33,6 +33,7 @@ import { NodeToolbar } from "reactflow"; import NodeToolbarComponent from "../../pages/FlowPage/components/nodeToolbarComponent"; import ShadTooltip from "../../components/ShadTooltipComponent"; +import { postValidateNode } from "../../controllers/API"; export default function GenericNode({ data, selected, @@ -62,17 +63,13 @@ export default function GenericNode({ const validateNode = useCallback( debounce(async () => { try { - const response = await fetch(`/validate/node/${data.id}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(reactFlowInstance.toObject()), - }); + const response = await postValidateNode( + data.id, + reactFlowInstance.toObject() + ); if (response.status === 200) { - let jsonResponse = await response.json(); - let jsonResponseParsed = await JSON.parse(jsonResponse); + let jsonResponseParsed = await JSON.parse(response.data); setValidationStatus(jsonResponseParsed); } } catch (error) { @@ -148,11 +145,10 @@ export default function GenericNode({ "Validating..." ) : (
- {validationStatus.params - .split("\n") - .map((line, index) => ( -
{line}
- ))} + {validationStatus.params || + "" + .split("\n") + .map((line, index) =>
{line}
)}
) } diff --git a/src/frontend/src/modals/codeAreaModal/index.tsx b/src/frontend/src/modals/codeAreaModal/index.tsx index 405187647..e74806c7b 100644 --- a/src/frontend/src/modals/codeAreaModal/index.tsx +++ b/src/frontend/src/modals/codeAreaModal/index.tsx @@ -8,7 +8,7 @@ import "ace-builds/src-noconflict/theme-twilight"; import "ace-builds/src-noconflict/ext-language_tools"; // import "ace-builds/webpack-resolver"; import { darkContext } from "../../contexts/darkContext"; -import { checkCode } from "../../controllers/API"; +import { postValidateCode } from "../../controllers/API"; import { alertContext } from "../../contexts/alertContext"; import { TabsContext } from "../../contexts/tabsContext"; import { @@ -81,7 +81,7 @@ export default function CodeAreaModal({