mirror of
https://github.com/khairul169/garage-webui.git
synced 2026-09-18 10:23:21 +07:00
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import api from "@/lib/api";
|
|
import {
|
|
useMutation,
|
|
UseMutationOptions,
|
|
useQuery,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
GetObjectsResult,
|
|
PutObjectPayload,
|
|
UseBrowserObjectOptions,
|
|
} from "./types";
|
|
|
|
export const useBrowseObjects = (
|
|
bucket: string,
|
|
options?: UseBrowserObjectOptions
|
|
) => {
|
|
return useQuery({
|
|
queryKey: ["browse", bucket, options],
|
|
queryFn: () =>
|
|
api.get<GetObjectsResult>(`/browse/${bucket}`, { params: options }),
|
|
});
|
|
};
|
|
|
|
export const usePutObject = (
|
|
bucket: string,
|
|
options?: UseMutationOptions<any, Error, PutObjectPayload>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async (body) => {
|
|
const formData = new FormData();
|
|
if (body.file) {
|
|
formData.append("file", body.file);
|
|
}
|
|
|
|
return api.put(`/browse/${bucket}/${body.key}`, { body: formData });
|
|
},
|
|
...options,
|
|
});
|
|
};
|
|
|
|
export const useDeleteObject = (
|
|
bucket: string,
|
|
options?: UseMutationOptions<any, Error, { key: string; recursive?: boolean }>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (data) =>
|
|
api.delete(`/browse/${bucket}/${data.key}`, {
|
|
params: { recursive: data.recursive },
|
|
}),
|
|
...options,
|
|
});
|
|
};
|