import { useMutation } from '@tanstack/react-query' import { toast } from 'sonner' import type { components } from '@/data/api' import { handleError, post } from '@/data/fetchers' import type { ResponseError, UseCustomMutationOptions } from '@/types' export type ValidateSpamVariables = { projectRef: string template: components['schemas']['ValidateSpamBody'] } export type ValidateSpamResponse = components['schemas']['ValidateSpamResponse_Output'] export async function validateSpam({ projectRef, template }: ValidateSpamVariables) { const { data, error } = await post('/platform/auth/{ref}/validate/spam', { // @ts-expect-error API doesnt have the param check cause it's not being used in the controller params: { path: { ref: projectRef } }, body: template, }) if (error) handleError(error) return data } type ValidateSpamData = Awaited> export const useValidateSpamMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { return useMutation({ mutationFn: (vars) => validateSpam(vars), async onSuccess(data, variables, context) { await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to validate template: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }