mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 21:40:31 +00:00
feat(ui): added dynamic secret and lease api hook
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export { useCreateDynamicSecret, useDeleteDynamicSecret, useUpdateDynamicSecret } from "./mutation";
|
||||
export { useGetDynamicSecretDetails,useGetDynamicSecrets } from "./queries";
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { apiRequest } from "@app/config/request";
|
||||
|
||||
import { dynamicSecretKeys } from "./queries";
|
||||
import {
|
||||
TCreateDynamicSecretDTO,
|
||||
TDeleteDynamicSecretDTO,
|
||||
TDynamicSecret,
|
||||
TUpdateDynamicSecretDTO
|
||||
} from "./types";
|
||||
|
||||
export const useCreateDynamicSecret = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<{}, {}, TCreateDynamicSecretDTO>({
|
||||
mutationFn: async (dto) => {
|
||||
const { data } = await apiRequest.post<{ dynamicSecret: TDynamicSecret }>(
|
||||
"/api/v1/dynamic-secrets",
|
||||
dto
|
||||
);
|
||||
return data.dynamicSecret;
|
||||
},
|
||||
onSuccess: (_, { path, environment, projectId }) => {
|
||||
queryClient.invalidateQueries(dynamicSecretKeys.list({ path, projectId, environment }));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateDynamicSecret = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<{}, {}, TUpdateDynamicSecretDTO>({
|
||||
mutationFn: async (dto) => {
|
||||
const { data } = await apiRequest.patch<{ dynamicSecret: TDynamicSecret }>(
|
||||
`/api/v1/dynamic-secrets/${dto.slug}`,
|
||||
dto
|
||||
);
|
||||
return data.dynamicSecret;
|
||||
},
|
||||
onSuccess: (_, { path, environment, projectId }) => {
|
||||
queryClient.invalidateQueries(dynamicSecretKeys.list({ path, projectId, environment }));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteDynamicSecret = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<{}, {}, TDeleteDynamicSecretDTO>({
|
||||
mutationFn: async (dto) => {
|
||||
const { data } = await apiRequest.delete<{ dynamicSecret: TDynamicSecret }>(
|
||||
`/api/v1/dynamic-secrets/${dto.slug}`,
|
||||
{ data: dto }
|
||||
);
|
||||
return data.dynamicSecret;
|
||||
},
|
||||
onSuccess: (_, { path, environment, projectId }) => {
|
||||
queryClient.invalidateQueries(dynamicSecretKeys.list({ path, projectId, environment }));
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
import { apiRequest } from "@app/config/request";
|
||||
|
||||
import { TDetailsDynamicSecretDTO, TDynamicSecret, TListDynamicSecretDTO } from "./types";
|
||||
|
||||
export const dynamicSecretKeys = {
|
||||
list: ({
|
||||
projectId,
|
||||
environment,
|
||||
path
|
||||
}: Pick<TListDynamicSecretDTO, "path" | "environment" | "projectId">) =>
|
||||
[{ projectId, environment, path }, "dynamic-secrets"] as const,
|
||||
details: ({ path, environment, projectId, slug }: TDetailsDynamicSecretDTO) =>
|
||||
[{ projectId, path, environment, slug }, "dynamic-secret-details"] as const
|
||||
};
|
||||
|
||||
export const useGetDynamicSecrets = ({ projectId, environment, path }: TListDynamicSecretDTO) => {
|
||||
return useQuery({
|
||||
queryKey: dynamicSecretKeys.list({ path, environment, projectId }),
|
||||
enabled: Boolean(projectId && environment && path),
|
||||
queryFn: async () => {
|
||||
const { data } = await apiRequest.get<{ dynamicSecrets: TDynamicSecret[] }>(
|
||||
"/api/v1/dynamic-secrets",
|
||||
{
|
||||
params: {
|
||||
projectId,
|
||||
environment,
|
||||
path
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return data.dynamicSecrets;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetDynamicSecretDetails = ({
|
||||
projectId,
|
||||
environment,
|
||||
path,
|
||||
slug
|
||||
}: TDetailsDynamicSecretDTO) => {
|
||||
return useQuery({
|
||||
queryKey: dynamicSecretKeys.details({ path, environment, projectId, slug }),
|
||||
enabled: Boolean(projectId && environment && path && slug),
|
||||
queryFn: async () => {
|
||||
const { data } = await apiRequest.get<{
|
||||
dynamicSecret: TDynamicSecret & { inputs: unknown };
|
||||
}>(`/api/v1/dynamic-secrets/${slug}`, {
|
||||
params: {
|
||||
projectId,
|
||||
environment,
|
||||
path
|
||||
}
|
||||
});
|
||||
|
||||
return data.dynamicSecret;
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,83 @@
|
||||
export enum DynamicSecretStatus {
|
||||
Deleting = "Revocation in process",
|
||||
FailedDeletion = "Failed to delete"
|
||||
}
|
||||
// TODO(akhilmhdh): When we switch to monorepo all the server api ts will be in a shared repo
|
||||
export type TDynamicSecret = {
|
||||
id: string;
|
||||
slug: string;
|
||||
type: DynamicSecretProviders;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
defaultTTL: string;
|
||||
status?: DynamicSecretStatus;
|
||||
statusDetails?: string;
|
||||
maxTTL: string;
|
||||
};
|
||||
|
||||
export enum DynamicSecretProviders {
|
||||
SqlDatabase = "sql-database"
|
||||
}
|
||||
|
||||
export enum SqlProviders {
|
||||
Postgres = "postgres"
|
||||
}
|
||||
|
||||
export type TDynamicSecretProvider = {
|
||||
type: DynamicSecretProviders;
|
||||
inputs: {
|
||||
client: SqlProviders;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
username: string;
|
||||
password: string;
|
||||
creationStatement: string;
|
||||
revocationStatement: string;
|
||||
renewStatement: string;
|
||||
ca?: string | undefined;
|
||||
};
|
||||
};
|
||||
|
||||
export type TCreateDynamicSecretDTO = {
|
||||
projectId: string;
|
||||
provider: TDynamicSecretProvider;
|
||||
defaultTTL: string;
|
||||
maxTTL?: string;
|
||||
path: string;
|
||||
environment: string;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export type TUpdateDynamicSecretDTO = {
|
||||
slug: string;
|
||||
projectId: string;
|
||||
path: string;
|
||||
environment: string;
|
||||
data: {
|
||||
newSlug?: string;
|
||||
defaultTTL?: string;
|
||||
maxTTL?: string | null;
|
||||
inputs?: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
export type TListDynamicSecretDTO = {
|
||||
projectId: string;
|
||||
path: string;
|
||||
environment: string;
|
||||
};
|
||||
|
||||
export type TDeleteDynamicSecretDTO = {
|
||||
projectId: string;
|
||||
path: string;
|
||||
environment: string;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export type TDetailsDynamicSecretDTO = {
|
||||
projectId: string;
|
||||
path: string;
|
||||
environment: string;
|
||||
slug: string;
|
||||
};
|
||||
Reference in New Issue
Block a user