Files
supabase/apps/studio/data/oauth/oauth-app-create-mutation.ts
T
Joshen Lim 98a5f06070 Chore/next round of refactoring rqs to use data fetchers (#33556)
* Update several files to use data fetcher methods

* Remove unused files from lib/common/fetch

* Revert "Remove unused files from lib/common/fetch"

This reverts commit b314b05f06.

* Fix GraphQL

* Fix types

* Add IS_PLATFORM check in useOrgUsageQuery

* Fix type issues
2025-02-17 15:23:38 +08:00

75 lines
1.9 KiB
TypeScript

import type { OAuthScope } from '@supabase/shared-types/out/constants'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { components } from 'api-types'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { oauthAppKeys } from './keys'
export type OAuthAppCreateVariables = {
slug: string
name: string
website: string
icon?: string
scopes?: OAuthScope[]
redirect_uris: string[]
}
export type OAuthAppCreateResponse = components['schemas']['CreateOAuthAppResponse']
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<
UseMutationOptions<OAuthAppCreateData, ResponseError, OAuthAppCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<OAuthAppCreateData, ResponseError, OAuthAppCreateVariables>(
(vars) => createOAuthApp(vars),
{
async onSuccess(data, variables, context) {
const { slug } = variables
await queryClient.invalidateQueries(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,
}
)
}