mirror of
https://github.com/supabase/supabase.git
synced 2026-09-07 02:20:52 +08:00
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { clientSecretKeys } from './keys'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type ClientSecretCreateVariables = {
|
|
slug: string
|
|
appId: string
|
|
}
|
|
|
|
export async function createClientSecret({ slug, appId }: ClientSecretCreateVariables) {
|
|
const { data, error } = await post(
|
|
'/platform/organizations/{slug}/oauth/apps/{app_id}/client-secrets',
|
|
{ params: { path: { slug, app_id: appId } } }
|
|
)
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export const useClientSecretCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<any, ResponseError, ClientSecretCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<any, ResponseError, ClientSecretCreateVariables>({
|
|
mutationFn: (vars) => createClientSecret(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { slug, appId } = variables
|
|
await queryClient.invalidateQueries({ queryKey: clientSecretKeys.list(slug, appId) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create client secret: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|