From 65749b2402373f99e5f1cc60cff4667497cb254f Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62335616+lucaseduoli@users.noreply.github.com> Date: Fri, 5 Jul 2024 09:44:10 -0300 Subject: [PATCH] fix: refactor of api structure (#2544) * Refactored mutation and query types and callables * Refactored version and other queries to new way of calling function * update type declaration to support options * update getVersionQuery * [autofix.ci] apply automated fixes * update type declaration to remove on Fetch options * remove onFetch from version * update transactions query * [autofix.ci] apply automated fixes --------- Co-authored-by: anovazzi1 Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: cristhianzl --- src/frontend/src/App.tsx | 1 + .../queries/api-keys/use-post-add-api-key.ts | 13 ++------ .../queries/files/use-get-download-images.ts | 5 +-- .../transactions/use-get-transactions.ts | 33 ++++++++----------- .../API/queries/version/use-get-version.ts | 25 +++++--------- .../src/modals/flowLogsModal/index.tsx | 10 +++--- src/frontend/src/types/api/index.ts | 22 ++++++++----- 7 files changed, 45 insertions(+), 64 deletions(-) diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 6271458b4e5f..1c6a71893afa 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -41,6 +41,7 @@ export default function App() { isError: isErrorHealth, refetch, } = useGetHealthQuery(); + useEffect(() => { if (!dark) { document.getElementById("body")!.classList.remove("dark"); diff --git a/src/frontend/src/controllers/API/queries/api-keys/use-post-add-api-key.ts b/src/frontend/src/controllers/API/queries/api-keys/use-post-add-api-key.ts index fd0eb5d35d44..4394c04e012d 100644 --- a/src/frontend/src/controllers/API/queries/api-keys/use-post-add-api-key.ts +++ b/src/frontend/src/controllers/API/queries/api-keys/use-post-add-api-key.ts @@ -1,5 +1,4 @@ import { useMutationFunctionType } from "@/types/api"; -import { UseMutationResult } from "@tanstack/react-query"; import { api } from "../../api"; import { getURL } from "../../helpers/constants"; import { UseRequestProcessor } from "../../services/request-processor"; @@ -15,19 +14,13 @@ export const usePostAddApiKey: useMutationFunctionType = ( const { mutate } = UseRequestProcessor(); const postAddApiKeyFn = async (payload: IPostAddApiKey): Promise => { - return await api.post(`${getURL("API_KEY")}/store`, { + const res = await api.post(`${getURL("API_KEY")}/store`, { api_key: payload.key, }); + return res.data; }; - const mutation: UseMutationResult = mutate( - ["usePostAddApiKey"], - async (payload: IPostAddApiKey) => { - const res = await postAddApiKeyFn(payload); - return res.data; - }, - options, - ); + const mutation = mutate(["usePostAddApiKey"], postAddApiKeyFn, options); return mutation; }; diff --git a/src/frontend/src/controllers/API/queries/files/use-get-download-images.ts b/src/frontend/src/controllers/API/queries/files/use-get-download-images.ts index e1173d876d24..d255b8d3850b 100644 --- a/src/frontend/src/controllers/API/queries/files/use-get-download-images.ts +++ b/src/frontend/src/controllers/API/queries/files/use-get-download-images.ts @@ -16,12 +16,13 @@ export interface DownloadImagesResponse { export const useGetDownloadImagesQuery: useQueryFunctionType< DownloadImagesQueryParams, DownloadImagesResponse -> = ({ flowId, fileName }) => { +> = (params) => { const { query } = UseRequestProcessor(); const getDownloadImagesFn = async () => { + if (!params) return; const response = await api.get( - `${getURL("FILES")}/images/${flowId}/${fileName}`, + `${getURL("FILES")}/images/${params.flowId}/${params.fileName}`, ); return response["data"]; }; diff --git a/src/frontend/src/controllers/API/queries/transactions/use-get-transactions.ts b/src/frontend/src/controllers/API/queries/transactions/use-get-transactions.ts index 74a175e5cbf0..aead30844525 100644 --- a/src/frontend/src/controllers/API/queries/transactions/use-get-transactions.ts +++ b/src/frontend/src/controllers/API/queries/transactions/use-get-transactions.ts @@ -9,6 +9,8 @@ import { UseRequestProcessor } from "../../services/request-processor"; interface TransactionsQueryParams { id: string; params?: Record; + mode?: "union" | "intersection"; + excludedColumns?: string[]; } interface TransactionsResponse { @@ -19,23 +21,16 @@ interface TransactionsResponse { export const useGetTransactionsQuery: useQueryFunctionType< TransactionsQueryParams, TransactionsResponse -> = ({ id, params }, onFetch) => { +> = ({ id, excludedColumns, mode, params }, options) => { + // Function body remains unchanged const { query } = UseRequestProcessor(); - const responseFn = (data: any) => { - if (!onFetch) return data; - if (typeof onFetch === "function") return onFetch(data); - switch (onFetch) { - case "TableUnion": { - const columns = extractColumnsFromRows(data.data, "union"); - return { rows: data.data, columns }; - } - case "TableIntersection": { - const columns = extractColumnsFromRows(data.data, "intersection"); - return { rows: data.data, columns }; - } - default: - return data; + const responseFn = (data: object[]) => { + if (mode) { + const columns = extractColumnsFromRows(data, mode, excludedColumns); + return { rows: data, columns }; + } else { + return data; } }; @@ -46,16 +41,14 @@ export const useGetTransactionsQuery: useQueryFunctionType< config["params"] = { ...config["params"], ...params }; } - const rows = await api.get( - `${getURL("TRANSACTIONS")}`, - config, - ); + const result = await api.get(`${getURL("TRANSACTIONS")}`, config); - return responseFn(rows); + return responseFn(result.data); }; const queryResult = query(["useGetTransactionsQuery"], getTransactionsFn, { placeholderData: keepPreviousData, + ...options, }); return queryResult; diff --git a/src/frontend/src/controllers/API/queries/version/use-get-version.ts b/src/frontend/src/controllers/API/queries/version/use-get-version.ts index dea78c99e3cd..1947a8aa16d0 100644 --- a/src/frontend/src/controllers/API/queries/version/use-get-version.ts +++ b/src/frontend/src/controllers/API/queries/version/use-get-version.ts @@ -12,30 +12,21 @@ interface versionQueryResponse { export const useGetVersionQuery: useQueryFunctionType< undefined, versionQueryResponse -> = (_, onFetch) => { +> = (_, options) => { const { query } = UseRequestProcessor(); - const responseFn = (data: versionQueryResponse) => { - if (!onFetch) return data; - if (typeof onFetch === "function") return onFetch(data); - const refreshVersion = useDarkStore.getState().refreshVersion; - switch (onFetch) { - case "updateState": { - return refreshVersion(data.version); - } - default: - return data; - } - }; - const getVersionFn = async () => { return await api.get(`${getURL("VERSION")}`); }; - const queryResult = query(["useGetVersionQuery"], async () => { + const responseFn = async () => { const { data } = await getVersionFn(); - return responseFn(data); - }); + const refreshVersion = useDarkStore.getState().refreshVersion; + refreshVersion(data.version); + return data; + }; + + const queryResult = query(["useGetVersionQuery"], responseFn, { ...options }); return queryResult; }; diff --git a/src/frontend/src/modals/flowLogsModal/index.tsx b/src/frontend/src/modals/flowLogsModal/index.tsx index 6cab4efd39a1..9094d5ea8c2e 100644 --- a/src/frontend/src/modals/flowLogsModal/index.tsx +++ b/src/frontend/src/modals/flowLogsModal/index.tsx @@ -16,12 +16,10 @@ export default function FlowLogsModal({ const [columns, setColumns] = useState>([]); const [rows, setRows] = useState([]); - const { data, isLoading, refetch } = useGetTransactionsQuery( - { - id: currentFlowId, - }, - "TableUnion", - ); + const { data, isLoading, refetch } = useGetTransactionsQuery({ + id: currentFlowId, + mode: "union", + }); useEffect(() => { if (data) { diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index 00f4abe720bf..6a74f9b446e9 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -1,8 +1,7 @@ import { - MutationFunction, - UndefinedInitialDataOptions, UseMutationOptions, UseMutationResult, + UseQueryOptions, UseQueryResult, } from "@tanstack/react-query"; import { Edge, Node, Viewport } from "reactflow"; @@ -232,20 +231,25 @@ export type ResponseErrorTypeAPI = { export type ResponseErrorDetailAPI = { response: { data: { detail: string } }; }; - export type useQueryFunctionType = T extends undefined - ? (props?: T, onFetch?: ((data: R) => void) | string) => UseQueryResult - : (props: T, onFetch?: ((data: R) => void) | string) => UseQueryResult; + ? ( + params?: T, + options?: Omit, + ) => UseQueryResult + : ( + params: T, + options?: Omit, + ) => UseQueryResult; export type QueryFunctionType = ( - queryKey: UndefinedInitialDataOptions["queryKey"], - queryFn: UndefinedInitialDataOptions["queryFn"], - options?: Omit, + queryKey: UseQueryOptions["queryKey"], + queryFn: UseQueryOptions["queryFn"], + options?: Omit, ) => UseQueryResult; export type MutationFunctionType = ( mutationKey: UseMutationOptions["mutationKey"], - mutationFn: MutationFunction, + mutationFn: UseMutationOptions["mutationFn"], options?: Omit, "mutationFn" | "mutationKey">, ) => UseMutationResult;