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,6 @@
export {
useCreateDynamicSecretLease,
useRenewDynamicSecretLease,
useRevokeDynamicSecretLease
} from "./mutation";
export { useGetDynamicSecretLeases } from "./queries";
@@ -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 })
);
}
});
};
@@ -0,0 +1,37 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TDynamicSecretLease, TListDynamicSecretLeaseDTO } from "./types";
export const dynamicSecretLeaseKeys = {
list: ({ projectId, environment, path, slug }: TListDynamicSecretLeaseDTO) =>
[{ projectId, environment, path, slug }, "dynamic-secret-leases"] as const
};
export const useGetDynamicSecretLeases = ({
projectId,
environment,
path,
slug,
enabled = true
}: TListDynamicSecretLeaseDTO) => {
return useQuery({
queryKey: dynamicSecretLeaseKeys.list({ path, environment, projectId, slug }),
enabled: Boolean(projectId && environment && path && slug && enabled),
queryFn: async () => {
const { data } = await apiRequest.get<{ leases: TDynamicSecretLease[] }>(
`/api/v1/dynamic-secrets/leases/${slug}`,
{
params: {
projectId,
environment,
path
}
}
);
return data.leases;
}
});
};
@@ -0,0 +1,47 @@
export enum DynamicSecretLeaseStatus {
FailedDeletion = "Failed to delete"
}
export type TDynamicSecretLease = {
id: string;
version: number;
expireAt: string;
dynamicSecretId: string;
status?: DynamicSecretLeaseStatus;
statusDetails?: string;
createdAt: string;
updatedAt: string;
};
export type TCreateDynamicSecretLeaseDTO = {
slug: string;
projectId: string;
ttl?: string;
path: string;
environment: string;
};
export type TRenewDynamicSecretLeaseDTO = {
leaseId: string;
slug: string;
ttl?: string;
projectId: string;
path: string;
environment: string;
};
export type TListDynamicSecretLeaseDTO = {
slug: string;
projectId: string;
path: string;
environment: string;
enabled?: boolean;
};
export type TRevokeDynamicSecretLeaseDTO = {
leaseId: string;
slug: string;
projectId: string;
path: string;
environment: string;
};