feat: secret sharing

This commit is contained in:
ShubhamPalriwala
2024-05-29 14:10:46 +05:30
parent 81b0c8bc12
commit 387981ea87
42 changed files with 1497 additions and 2 deletions
@@ -0,0 +1,3 @@
export * from "./mutations";
export * from "./queries";
export * from "./types";
@@ -0,0 +1,35 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TCreateSharedSecretRequest, TDeleteSharedSecretRequest, TSharedSecret } from "./types";
export const useCreateSharedSecret = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (inputData: TCreateSharedSecretRequest) => {
const { data } = await apiRequest.post<TSharedSecret>("/api/v1/secret-sharing", inputData);
return data;
},
onSuccess: () => queryClient.invalidateQueries(["sharedSecrets"])
});
};
export const useDeleteSharedSecret = () => {
const queryClient = useQueryClient();
return useMutation<
TSharedSecret,
{ message: string },
{ sharedSecretId: string; workspaceId: string }
>({
mutationFn: async ({ sharedSecretId, workspaceId }: TDeleteSharedSecretRequest) => {
const { data } = await apiRequest.delete<TSharedSecret>(
`/api/v1/secret-sharing/${workspaceId}/${sharedSecretId}`
);
return data;
},
onSuccess: () => {
queryClient.invalidateQueries(["sharedSecrets"]);
}
});
};
@@ -0,0 +1,32 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TSharedSecret, TViewSharedSecretResponse } from "./types";
export const useGetSharedSecrets = (workspaceId: string) => {
return useQuery({
queryKey: ["sharedSecrets"],
queryFn: async () => {
const { data } = await apiRequest.get<TSharedSecret[]>(
`/api/v1/secret-sharing/${workspaceId}`
);
return data;
}
});
};
export const useGetActiveSharedSecretById = (id: string) => {
return useQuery<TViewSharedSecretResponse, [string]>({
queryFn: async () => {
const { data } = await apiRequest.get<TViewSharedSecretResponse>(
`/api/v1/secret-sharing/public/${id}`
);
return {
name: data.name,
signedValue: data.signedValue,
expiresAt: data.expiresAt
};
}
});
};
@@ -0,0 +1,27 @@
export type TSharedSecret = {
id: string;
name: string;
signedValue: string;
userId: string;
expiresAt: Date;
createdAt: Date;
updatedAt: Date;
};
export type TCreateSharedSecretRequest = {
name: string;
signedValue: string;
expiresAt: Date;
workspaceId: string;
};
export type TViewSharedSecretResponse = {
name: string;
signedValue: string;
expiresAt: Date;
};
export type TDeleteSharedSecretRequest = {
sharedSecretId: string;
workspaceId: string;
};