Skip to content

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

1.9 KB
Raw Blame
import type { OAuthScope } from '@supabase/shared-types/out/constants'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { components } from 'api-types'
import { toast } from 'sonner'

import { oauthAppKeys } from './keys'
import { handleError, post } from '@/data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from '@/types'

export type OAuthAppCreateVariables = {
  slug: string
  name: string
  website: string
  icon?: string
  scopes: OAuthScope[]
  redirect_uris: string[]
}

export type OAuthAppCreateResponse = components['schemas']['CreateOAuthAppResponse_Output']

export async function createOAuthApp({
  slug,
  name,
  website,
  icon,
  scopes,
  redirect_uris,
}: OAuthAppCreateVariables) {
  const { data, error } = await post('/platform/organizations/{slug}/oauth/apps', {
    params: { path: { slug } },
    body: {
      name,
      website,
      icon,
      scopes,
      redirect_uris,
    },
  })

  if (error) handleError(error)
  return data
}

type OAuthAppCreateData = Awaited<ReturnType<typeof createOAuthApp>>

export const useOAuthAppCreateMutation = ({
  onSuccess,
  onError,
  ...options
}: Omit<
  UseCustomMutationOptions<OAuthAppCreateData, ResponseError, OAuthAppCreateVariables>,
  'mutationFn'
> = {}) => {
  const queryClient = useQueryClient()

  return useMutation<OAuthAppCreateData, ResponseError, OAuthAppCreateVariables>({
    mutationFn: (vars) => createOAuthApp(vars),
    async onSuccess(data, variables, context) {
      const { slug } = variables
      await queryClient.invalidateQueries({ queryKey: oauthAppKeys.oauthApps(slug) })
      await onSuccess?.(data, variables, context)
    },
    async onError(data, variables, context) {
      if (onError === undefined) {
        toast.error(`Failed to create OAuth application: ${data.message}`)
      } else {
        onError(data, variables, context)
      }
    },
    ...options,
  })
}