import type { SeamHttpApiError, SeamHttpEndpointsWithoutWorkspace, SeamHttpEndpointWithoutWorkspaceMutationPaths, SeamHttpInvalidInputError, } from '@seamapi/http' import { useMutation, type UseMutationOptions, type UseMutationResult, } from '@tanstack/react-query' import { NullSeamClientError, useSeamClient } from 'lib/use-seam-client.js' export type UseSeamMutationWithoutWorkspaceVariables< T extends SeamHttpEndpointWithoutWorkspaceMutationPaths, > = Parameters[0] export type UseSeamMutationWithoutWorkspaceResult< T extends SeamHttpEndpointWithoutWorkspaceMutationPaths, > = UseMutationResult< MutationData, MutationError, UseSeamMutationWithoutWorkspaceVariables > export function useSeamMutationWithoutWorkspace< T extends SeamHttpEndpointWithoutWorkspaceMutationPaths, >( endpointPath: T, options: Parameters[1] & MutationOptions< MutationData, MutationError, UseSeamMutationWithoutWorkspaceVariables > = {}, ): UseSeamMutationWithoutWorkspaceResult { const { endpointClient: client } = useSeamClient() return useMutation({ ...options, mutationFn: async (variables) => { if (client === null) throw new NullSeamClientError() // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory. // Type assertion is needed here for performance reasons. The types are correct at runtime. const endpoint = client[endpointPath] as (...args: any) => Promise return await endpoint(variables, options) }, }) } type MutationData = Awaited> type MutationError = Error | SeamHttpApiError | SeamHttpInvalidInputError type MutationOptions = Omit, 'mutationFn'>