wenson af376beb5b refactor: update API endpoints and improve type safety across hooks
- Refactored `useDebounce` to enhance type safety with generic arguments.
- Updated `FetchOptions` in `api.ts` to use `unknown` instead of `any` for better type safety.
- Changed API endpoints in bucket-related hooks to use new versioned endpoints.
- Improved type definitions in bucket hooks and added specific types for mutation options.
- Enhanced `useConnectNode`, `useAssignNode`, and other cluster hooks to use new API endpoints and improved type safety.
- Updated health check and key management hooks to reflect new API structure.
- Refined utility functions and type definitions for better clarity and maintainability.
2025-07-15 15:59:47 +08:00

111 lines
2.5 KiB
TypeScript

import api from "@/lib/api";
import {
MutationOptions,
useMutation,
UseMutationOptions,
useQuery,
} from "@tanstack/react-query";
import { Bucket, Permissions } from "../types";
export const useBucket = (id?: string | null) => {
return useQuery({
queryKey: ["bucket", id],
queryFn: () => api.get<Bucket>("/v2/GetBucketInfo", { params: { id } }),
enabled: !!id,
});
};
export const useUpdateBucket = (id?: string | null) => {
return useMutation({
mutationFn: (values: Partial<Bucket>) => {
return api.put<Bucket>("/v2/UpdateBucket", { params: { id }, body: values });
},
});
};
export const useAddAlias = (
bucketId?: string | null,
options?: UseMutationOptions<unknown, Error, string>
) => {
return useMutation({
mutationFn: (alias: string) => {
return api.put("/v2/PutBucketGlobalAlias", {
params: { id: bucketId, alias },
});
},
...options,
});
};
export const useRemoveAlias = (
bucketId?: string | null,
options?: UseMutationOptions<unknown, Error, string>
) => {
return useMutation({
mutationFn: (alias: string) => {
return api.delete("/v2/DeleteBucketGlobalAlias", {
params: { id: bucketId, alias },
});
},
...options,
});
};
export const useAllowKey = (
bucketId?: string | null,
options?: MutationOptions<
unknown,
Error,
{ keyId: string; permissions: Permissions }[]
>
) => {
return useMutation({
mutationFn: async (payload) => {
const promises = payload.map(async (key) => {
console.log("test", key);
return api.post("/v2/AllowBucketKey", {
body: {
bucketId,
accessKeyId: key.keyId,
permissions: key.permissions,
},
});
});
const result = await Promise.all(promises);
return result;
},
...options,
});
};
export const useDenyKey = (
bucketId?: string | null,
options?: MutationOptions<
unknown,
Error,
{ keyId: string; permissions: Permissions }
>
) => {
return useMutation({
mutationFn: (payload) => {
return api.post("/v2/DenyBucketKey", {
body: {
bucketId,
accessKeyId: payload.keyId,
permissions: payload.permissions,
},
});
},
...options,
});
};
export const useRemoveBucket = (
options?: MutationOptions<unknown, Error, string>
) => {
return useMutation({
mutationFn: (id) => api.delete("/v2/DeleteBucket", { params: { id } }),
...options,
});
};