Files
supabase/apps/studio/data/auth/validate-spam-mutation.ts
Alaister YoungandJoshen Lim 8855d05803 chore(studio): swap react-query to object syntax (#39842)
* chore(studio): swap react-query to object syntax

* Fix small issues found

* Fix realtime settings

* Nit

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-27 09:38:27 +01:00

50 lines
1.5 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { components } from 'data/api'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
export type ValidateSpamVariables = {
projectRef: string
template: components['schemas']['ValidateSpamBody']
}
export type ValidateSpamResponse = components['schemas']['ValidateSpamResponse']
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<
UseMutationOptions<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,
})
}