import { useMutation } from '@tanstack/react-query' import { components } from 'api-types' import { type TableSyncCopyConfig } from './types' import { buildPipelineApiConfig } from './utils' import { handleError, post } from '@/data/fetchers' import type { ResponseError, UseCustomMutationOptions } from '@/types' type ValidatePipelineParams = { projectRef: string sourceId: number publicationName: string maxFillMs?: number maxTableSyncWorkers?: number maxCopyConnectionsPerTable?: number invalidatedSlotBehavior?: 'error' | 'recreate' tableSyncCopy: TableSyncCopyConfig } type ValidatePipelineResponse = components['schemas']['ValidatePipelineResponse'] async function validatePipeline( { projectRef, sourceId, publicationName, maxFillMs, maxTableSyncWorkers, maxCopyConnectionsPerTable, invalidatedSlotBehavior, tableSyncCopy, }: ValidatePipelineParams, signal?: AbortSignal ): Promise { if (!projectRef) throw new Error('projectRef is required') if (!sourceId) throw new Error('sourceId is required') const { data, error } = await post('/platform/replication/{ref}/pipelines/validate', { params: { path: { ref: projectRef } }, body: { source_id: sourceId, config: buildPipelineApiConfig({ publicationName, maxTableSyncWorkers, maxCopyConnectionsPerTable, invalidatedSlotBehavior, tableSyncCopy, batch: maxFillMs === undefined ? undefined : { maxFillMs }, }), }, signal, }) if (error) handleError(error) return data } type ValidatePipelineData = Awaited> export const useValidatePipelineMutation = ( options?: Omit< UseCustomMutationOptions, 'mutationFn' > ) => { return useMutation({ mutationFn: (vars) => validatePipeline(vars), ...options, }) }