Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { getSecretSyncsNewlyCoveringPaths, TItemMove } from "@app/helpers/secretSyncCoverage";
import { SecretSync, TSecretSyncOption } from "@app/hooks/api/secretSyncs";
import {
TListSecretSyncOptions,
TListSecretSyncs,
TSecretSync,
TSecretSyncResponse
} from "@app/hooks/api/secretSyncs/types";
export const secretSyncKeys = {
all: ["secret-sync"] as const,
options: () => [...secretSyncKeys.all, "options"] as const,
list: (projectId: string) => [...secretSyncKeys.all, "list", projectId] as const,
byId: (destination: SecretSync, syncId: string) =>
[...secretSyncKeys.all, destination, "by-id", syncId] as const,
duplicateCheck: (
destination: SecretSync,
destinationConfig: unknown,
connectionId?: string,
excludeSyncId?: string
) =>
[
...secretSyncKeys.all,
destination,
"duplicate-check",
destinationConfig,
connectionId,
excludeSyncId
] as const,
recursiveConflicts: (
destination: SecretSync,
projectId: string,
environment?: string,
secretPath?: string,
keySchema?: string
) =>
[
...secretSyncKeys.all,
destination,
"recursive-conflicts",
projectId,
environment,
secretPath,
keySchema
] as const,
coveringMove: (projectId: string, moves: TItemMove[]) =>
[...secretSyncKeys.all, "covering-move", projectId, moves] as const
};
export type TSecretSyncRecursiveConflict = {
key: string;
paths: string[];
};
export const useSecretSyncOptions = (
options?: Omit<
UseQueryOptions<
TSecretSyncOption[],
unknown,
TSecretSyncOption[],
ReturnType<typeof secretSyncKeys.options>
>,
"queryKey" | "queryFn"
>
) => {
return useQuery({
queryKey: secretSyncKeys.options(),
queryFn: async () => {
const { data } = await apiRequest.get<TListSecretSyncOptions>("/api/v1/secret-syncs/options");
return data.secretSyncOptions;
},
...options
});
};
export const useSecretSyncOption = (destination?: SecretSync) => {
const { data: syncOptions, isPending } = useSecretSyncOptions();
const syncOption = syncOptions?.find((option) => option.destination === destination);
return { syncOption, isPending };
};
export const fetchSecretSyncsByProjectId = async (projectId: string) => {
const { data } = await apiRequest.get<TListSecretSyncs>("/api/v1/secret-syncs", {
params: { projectId }
});
return data.secretSyncs;
};
export const useListSecretSyncs = (
projectId: string,
options?: Omit<
UseQueryOptions<TSecretSync[], unknown, TSecretSync[], ReturnType<typeof secretSyncKeys.list>>,
"queryKey" | "queryFn"
>
) => {
return useQuery({
queryKey: secretSyncKeys.list(projectId),
queryFn: () => fetchSecretSyncsByProjectId(projectId),
...options
});
};
// keyed on the moves so a new destination always fetches a fresh list, never a cached one that
// predates a sync created since
export const useListSecretSyncsCoveringMove = (
projectId: string,
moves: TItemMove[],
{ enabled }: { enabled: boolean }
) =>
useQuery({
queryKey: secretSyncKeys.coveringMove(projectId, moves),
queryFn: () => fetchSecretSyncsByProjectId(projectId),
select: (secretSyncs) => getSecretSyncsNewlyCoveringPaths(secretSyncs, moves),
enabled,
staleTime: 0,
gcTime: 0
});
export const useGetSecretSync = (
destination: SecretSync,
syncId: string,
options?: Omit<
UseQueryOptions<TSecretSync, unknown, TSecretSync, ReturnType<typeof secretSyncKeys.byId>>,
"queryKey" | "queryFn"
>
) => {
return useQuery({
queryKey: secretSyncKeys.byId(destination, syncId),
queryFn: async () => {
const { data } = await apiRequest.get<TSecretSyncResponse>(
`/api/v1/secret-syncs/${destination}/${syncId}`
);
return data.secretSync;
},
...options
});
};
export const useCheckDuplicateDestination = (
destination: SecretSync,
destinationConfig: unknown,
projectId: string,
excludeSyncId?: string,
connectionId?: string,
options?: Omit<
UseQueryOptions<
{ hasDuplicate: boolean; duplicateProjectId?: string },
unknown,
{ hasDuplicate: boolean; duplicateProjectId?: string },
ReturnType<typeof secretSyncKeys.duplicateCheck>
>,
"queryKey" | "queryFn"
>
) => {
return useQuery({
queryKey: secretSyncKeys.duplicateCheck(
destination,
destinationConfig,
connectionId,
excludeSyncId
),
queryFn: async () => {
const { data } = await apiRequest.post<{
hasDuplicate: boolean;
duplicateProjectId?: string;
}>(`/api/v1/secret-syncs/${destination}/check-destination`, {
destinationConfig,
connectionId,
excludeSyncId,
projectId
});
return data;
},
enabled: Boolean(destinationConfig) && Object.keys(destinationConfig || {}).length > 0,
...options
});
};
export const useCheckRecursiveConflicts = (
destination: SecretSync,
projectId: string,
environment?: string,
secretPath?: string,
keySchema?: string,
options?: Omit<
UseQueryOptions<
{ conflicts: TSecretSyncRecursiveConflict[] },
unknown,
{ conflicts: TSecretSyncRecursiveConflict[] },
ReturnType<typeof secretSyncKeys.recursiveConflicts>
>,
"queryKey" | "queryFn"
>
) => {
return useQuery({
queryKey: secretSyncKeys.recursiveConflicts(
destination,
projectId,
environment,
secretPath,
keySchema
),
queryFn: async () => {
const { data } = await apiRequest.post<{ conflicts: TSecretSyncRecursiveConflict[] }>(
`/api/v1/secret-syncs/${destination}/recursive-conflicts`,
{ projectId, environment, secretPath, keySchema }
);
return data;
},
enabled: Boolean(environment) && Boolean(secretPath),
...options
});
};