Files
supabase/apps/studio/data/oauth-secrets/client-secret-create-mutation.ts
T
Jonathan Summers-MuirandJoshen Lim aefb34fd9c Feat: Adds Oauth client management (#34591)
* adds Oauth client update

* moar

* Update OAuthSecrets.tsx

* Fix TS issues

* moar

* Minor refactors

* Minor refactors

* Tidy up

* Clean up

* Clean up

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-04-07 17:43:46 +08:00

50 lines
1.4 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { handleError, post } from 'data/fetchers'
import { toast } from 'sonner'
import { ResponseError } from 'types'
import { clientSecretKeys } from './keys'
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<
UseMutationOptions<any, ResponseError, ClientSecretCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<any, ResponseError, ClientSecretCreateVariables>(
(vars) => createClientSecret(vars),
{
async onSuccess(data, variables, context) {
const { slug, appId } = variables
await queryClient.invalidateQueries(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,
}
)
}