Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.5 KB
Raw Blame
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<ReturnType<typeof validateSpam>>

export const useValidateSpamMutation = ({
  onSuccess,
  onError,
  ...options
}: Omit<
  UseCustomMutationOptions<ValidateSpamData, ResponseError, ValidateSpamVariables>,
  'mutationFn'
> = {}) => {
  return useMutation<ValidateSpamData, ResponseError, ValidateSpamVariables>({
    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,
  })
}