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 <otavio2204@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: cristhianzl <cristhian.lousa@gmail.com>
This commit is contained in:
parent
6443354720
commit
6b3f34da77
7 changed files with 45 additions and 64 deletions
|
|
@ -41,6 +41,7 @@ export default function App() {
|
||||||
isError: isErrorHealth,
|
isError: isErrorHealth,
|
||||||
refetch,
|
refetch,
|
||||||
} = useGetHealthQuery();
|
} = useGetHealthQuery();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!dark) {
|
if (!dark) {
|
||||||
document.getElementById("body")!.classList.remove("dark");
|
document.getElementById("body")!.classList.remove("dark");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { useMutationFunctionType } from "@/types/api";
|
import { useMutationFunctionType } from "@/types/api";
|
||||||
import { UseMutationResult } from "@tanstack/react-query";
|
|
||||||
import { api } from "../../api";
|
import { api } from "../../api";
|
||||||
import { getURL } from "../../helpers/constants";
|
import { getURL } from "../../helpers/constants";
|
||||||
import { UseRequestProcessor } from "../../services/request-processor";
|
import { UseRequestProcessor } from "../../services/request-processor";
|
||||||
|
|
@ -15,19 +14,13 @@ export const usePostAddApiKey: useMutationFunctionType<IPostAddApiKey> = (
|
||||||
const { mutate } = UseRequestProcessor();
|
const { mutate } = UseRequestProcessor();
|
||||||
|
|
||||||
const postAddApiKeyFn = async (payload: IPostAddApiKey): Promise<any> => {
|
const postAddApiKeyFn = async (payload: IPostAddApiKey): Promise<any> => {
|
||||||
return await api.post<any>(`${getURL("API_KEY")}/store`, {
|
const res = await api.post<any>(`${getURL("API_KEY")}/store`, {
|
||||||
api_key: payload.key,
|
api_key: payload.key,
|
||||||
});
|
});
|
||||||
|
return res.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const mutation: UseMutationResult<any, any, IPostAddApiKey> = mutate(
|
const mutation = mutate(["usePostAddApiKey"], postAddApiKeyFn, options);
|
||||||
["usePostAddApiKey"],
|
|
||||||
async (payload: IPostAddApiKey) => {
|
|
||||||
const res = await postAddApiKeyFn(payload);
|
|
||||||
return res.data;
|
|
||||||
},
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
|
|
||||||
return mutation;
|
return mutation;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,13 @@ export interface DownloadImagesResponse {
|
||||||
export const useGetDownloadImagesQuery: useQueryFunctionType<
|
export const useGetDownloadImagesQuery: useQueryFunctionType<
|
||||||
DownloadImagesQueryParams,
|
DownloadImagesQueryParams,
|
||||||
DownloadImagesResponse
|
DownloadImagesResponse
|
||||||
> = ({ flowId, fileName }) => {
|
> = (params) => {
|
||||||
const { query } = UseRequestProcessor();
|
const { query } = UseRequestProcessor();
|
||||||
|
|
||||||
const getDownloadImagesFn = async () => {
|
const getDownloadImagesFn = async () => {
|
||||||
|
if (!params) return;
|
||||||
const response = await api.get<DownloadImagesResponse>(
|
const response = await api.get<DownloadImagesResponse>(
|
||||||
`${getURL("FILES")}/images/${flowId}/${fileName}`,
|
`${getURL("FILES")}/images/${params.flowId}/${params.fileName}`,
|
||||||
);
|
);
|
||||||
return response["data"];
|
return response["data"];
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import { UseRequestProcessor } from "../../services/request-processor";
|
||||||
interface TransactionsQueryParams {
|
interface TransactionsQueryParams {
|
||||||
id: string;
|
id: string;
|
||||||
params?: Record<string, unknown>;
|
params?: Record<string, unknown>;
|
||||||
|
mode?: "union" | "intersection";
|
||||||
|
excludedColumns?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TransactionsResponse {
|
interface TransactionsResponse {
|
||||||
|
|
@ -19,23 +21,16 @@ interface TransactionsResponse {
|
||||||
export const useGetTransactionsQuery: useQueryFunctionType<
|
export const useGetTransactionsQuery: useQueryFunctionType<
|
||||||
TransactionsQueryParams,
|
TransactionsQueryParams,
|
||||||
TransactionsResponse
|
TransactionsResponse
|
||||||
> = ({ id, params }, onFetch) => {
|
> = ({ id, excludedColumns, mode, params }, options) => {
|
||||||
|
// Function body remains unchanged
|
||||||
const { query } = UseRequestProcessor();
|
const { query } = UseRequestProcessor();
|
||||||
|
|
||||||
const responseFn = (data: any) => {
|
const responseFn = (data: object[]) => {
|
||||||
if (!onFetch) return data;
|
if (mode) {
|
||||||
if (typeof onFetch === "function") return onFetch(data);
|
const columns = extractColumnsFromRows(data, mode, excludedColumns);
|
||||||
switch (onFetch) {
|
return { rows: data, columns };
|
||||||
case "TableUnion": {
|
} else {
|
||||||
const columns = extractColumnsFromRows(data.data, "union");
|
return data;
|
||||||
return { rows: data.data, columns };
|
|
||||||
}
|
|
||||||
case "TableIntersection": {
|
|
||||||
const columns = extractColumnsFromRows(data.data, "intersection");
|
|
||||||
return { rows: data.data, columns };
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -46,16 +41,14 @@ export const useGetTransactionsQuery: useQueryFunctionType<
|
||||||
config["params"] = { ...config["params"], ...params };
|
config["params"] = { ...config["params"], ...params };
|
||||||
}
|
}
|
||||||
|
|
||||||
const rows = await api.get<TransactionsResponse>(
|
const result = await api.get<object[]>(`${getURL("TRANSACTIONS")}`, config);
|
||||||
`${getURL("TRANSACTIONS")}`,
|
|
||||||
config,
|
|
||||||
);
|
|
||||||
|
|
||||||
return responseFn(rows);
|
return responseFn(result.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryResult = query(["useGetTransactionsQuery"], getTransactionsFn, {
|
const queryResult = query(["useGetTransactionsQuery"], getTransactionsFn, {
|
||||||
placeholderData: keepPreviousData,
|
placeholderData: keepPreviousData,
|
||||||
|
...options,
|
||||||
});
|
});
|
||||||
|
|
||||||
return queryResult;
|
return queryResult;
|
||||||
|
|
|
||||||
|
|
@ -12,30 +12,21 @@ interface versionQueryResponse {
|
||||||
export const useGetVersionQuery: useQueryFunctionType<
|
export const useGetVersionQuery: useQueryFunctionType<
|
||||||
undefined,
|
undefined,
|
||||||
versionQueryResponse
|
versionQueryResponse
|
||||||
> = (_, onFetch) => {
|
> = (_, options) => {
|
||||||
const { query } = UseRequestProcessor();
|
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 () => {
|
const getVersionFn = async () => {
|
||||||
return await api.get<versionQueryResponse>(`${getURL("VERSION")}`);
|
return await api.get<versionQueryResponse>(`${getURL("VERSION")}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryResult = query(["useGetVersionQuery"], async () => {
|
const responseFn = async () => {
|
||||||
const { data } = await getVersionFn();
|
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;
|
return queryResult;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,10 @@ export default function FlowLogsModal({
|
||||||
const [columns, setColumns] = useState<Array<ColDef | ColGroupDef>>([]);
|
const [columns, setColumns] = useState<Array<ColDef | ColGroupDef>>([]);
|
||||||
const [rows, setRows] = useState<any>([]);
|
const [rows, setRows] = useState<any>([]);
|
||||||
|
|
||||||
const { data, isLoading, refetch } = useGetTransactionsQuery(
|
const { data, isLoading, refetch } = useGetTransactionsQuery({
|
||||||
{
|
id: currentFlowId,
|
||||||
id: currentFlowId,
|
mode: "union",
|
||||||
},
|
});
|
||||||
"TableUnion",
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (data) {
|
if (data) {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
import {
|
import {
|
||||||
MutationFunction,
|
|
||||||
UndefinedInitialDataOptions,
|
|
||||||
UseMutationOptions,
|
UseMutationOptions,
|
||||||
UseMutationResult,
|
UseMutationResult,
|
||||||
|
UseQueryOptions,
|
||||||
UseQueryResult,
|
UseQueryResult,
|
||||||
} from "@tanstack/react-query";
|
} from "@tanstack/react-query";
|
||||||
import { Edge, Node, Viewport } from "reactflow";
|
import { Edge, Node, Viewport } from "reactflow";
|
||||||
|
|
@ -232,20 +231,25 @@ export type ResponseErrorTypeAPI = {
|
||||||
export type ResponseErrorDetailAPI = {
|
export type ResponseErrorDetailAPI = {
|
||||||
response: { data: { detail: string } };
|
response: { data: { detail: string } };
|
||||||
};
|
};
|
||||||
|
|
||||||
export type useQueryFunctionType<T = undefined, R = any> = T extends undefined
|
export type useQueryFunctionType<T = undefined, R = any> = T extends undefined
|
||||||
? (props?: T, onFetch?: ((data: R) => void) | string) => UseQueryResult<R>
|
? (
|
||||||
: (props: T, onFetch?: ((data: R) => void) | string) => UseQueryResult<R>;
|
params?: T,
|
||||||
|
options?: Omit<UseQueryOptions, "queryFn" | "queryKey">,
|
||||||
|
) => UseQueryResult<R>
|
||||||
|
: (
|
||||||
|
params: T,
|
||||||
|
options?: Omit<UseQueryOptions, "queryFn" | "queryKey">,
|
||||||
|
) => UseQueryResult<R>;
|
||||||
|
|
||||||
export type QueryFunctionType = (
|
export type QueryFunctionType = (
|
||||||
queryKey: UndefinedInitialDataOptions["queryKey"],
|
queryKey: UseQueryOptions["queryKey"],
|
||||||
queryFn: UndefinedInitialDataOptions["queryFn"],
|
queryFn: UseQueryOptions["queryFn"],
|
||||||
options?: Omit<UndefinedInitialDataOptions, "queryKey" | "queryFn">,
|
options?: Omit<UseQueryOptions, "queryKey" | "queryFn">,
|
||||||
) => UseQueryResult<any>;
|
) => UseQueryResult<any>;
|
||||||
|
|
||||||
export type MutationFunctionType = (
|
export type MutationFunctionType = (
|
||||||
mutationKey: UseMutationOptions["mutationKey"],
|
mutationKey: UseMutationOptions["mutationKey"],
|
||||||
mutationFn: MutationFunction<any, any>,
|
mutationFn: UseMutationOptions<any, any, any>["mutationFn"],
|
||||||
options?: Omit<UseMutationOptions<any, any>, "mutationFn" | "mutationKey">,
|
options?: Omit<UseMutationOptions<any, any>, "mutationFn" | "mutationKey">,
|
||||||
) => UseMutationResult<any, any, any, any>;
|
) => UseMutationResult<any, any, any, any>;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue