feat(ui): added dynamic secret and lease api hook

This commit is contained in:
Akhil Mohan
2024-03-21 20:22:30 +05:30
parent ab48c3b4fe
commit 177cd385cc
14 changed files with 449 additions and 3 deletions
@@ -0,0 +1,72 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { dynamicSecretLeaseKeys } from "./queries";
import {
TCreateDynamicSecretLeaseDTO,
TDynamicSecretLease,
TRenewDynamicSecretLeaseDTO,
TRevokeDynamicSecretLeaseDTO
} from "./types";
export const useCreateDynamicSecretLease = () => {
const queryClient = useQueryClient();
return useMutation<
{ lease: TDynamicSecretLease; data: unknown },
{},
TCreateDynamicSecretLeaseDTO
>({
mutationFn: async (dto) => {
const { data } = await apiRequest.post<{ lease: TDynamicSecretLease; data: unknown }>(
"/api/v1/dynamic-secrets/leases",
dto
);
return data;
},
onSuccess: (_, { path, environment, projectId, slug }) => {
queryClient.invalidateQueries(
dynamicSecretLeaseKeys.list({ path, projectId, environment, slug })
);
}
});
};
export const useRenewDynamicSecretLease = () => {
const queryClient = useQueryClient();
return useMutation<{}, {}, TRenewDynamicSecretLeaseDTO>({
mutationFn: async (dto) => {
const { data } = await apiRequest.post<{ lease: TDynamicSecretLease }>(
`/api/v1/dynamic-secrets/leases/${dto.leaseId}/renew`,
dto
);
return data.lease;
},
onSuccess: (_, { path, environment, projectId, slug }) => {
queryClient.invalidateQueries(
dynamicSecretLeaseKeys.list({ path, projectId, environment, slug })
);
}
});
};
export const useRevokeDynamicSecretLease = () => {
const queryClient = useQueryClient();
return useMutation<{}, {}, TRevokeDynamicSecretLeaseDTO>({
mutationFn: async (dto) => {
const { data } = await apiRequest.delete<{ lease: TDynamicSecretLease }>(
`/api/v1/dynamic-secrets/leases/${dto.leaseId}`,
{ data: dto }
);
return data.lease;
},
onSuccess: (_, { path, environment, projectId, slug }) => {
queryClient.invalidateQueries(
dynamicSecretLeaseKeys.list({ path, projectId, environment, slug })
);
}
});
};