mirror of
https://github.com/supabase/supabase.git
synced 2026-07-04 04:54:37 +08:00
* chore: increase react-query stale time * keep staleTime: 0 for table rows * use staleTime: 0 for all user sql queries * use staleTime: 0 for all pg-meta queries * Some fixes * fix updating tables * fix bug while editing column names * Fix deleting column in database/tables column list not revalidating UI * Fix updating column in database/tables column list throwing ane rror --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
89 lines
2.1 KiB
TypeScript
89 lines
2.1 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import toast from 'react-hot-toast'
|
|
|
|
import { post } from 'lib/common/fetch'
|
|
import { ResponseError } from 'types'
|
|
import { authKeys } from './keys'
|
|
|
|
export type UserCreateVariables = {
|
|
projectRef?: string
|
|
protocol: string
|
|
endpoint: string
|
|
serviceApiKey: string
|
|
user: {
|
|
email: string
|
|
password: string
|
|
autoConfirmUser: string
|
|
}
|
|
}
|
|
|
|
export type UserCreateResponse = {
|
|
id: string
|
|
phone: string
|
|
role: string
|
|
updated_at: string
|
|
app_metadata: {
|
|
provider: string
|
|
providers: string[]
|
|
}
|
|
aud: string
|
|
created_at: string
|
|
email: string
|
|
email_confirmed_at: string
|
|
identities: any[]
|
|
user_metadata: any
|
|
}
|
|
|
|
export async function createUser({ protocol, endpoint, serviceApiKey, user }: UserCreateVariables) {
|
|
const response = await post(
|
|
`${protocol}://${endpoint}/auth/v1/admin/users`,
|
|
{
|
|
email: user.email,
|
|
password: user.password,
|
|
email_confirm: user.autoConfirmUser,
|
|
},
|
|
{
|
|
headers: {
|
|
apikey: serviceApiKey,
|
|
Authorization: `Bearer ${serviceApiKey}`,
|
|
},
|
|
}
|
|
)
|
|
if (response.error) {
|
|
throw response.error
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
type UserCreateData = Awaited<ReturnType<typeof createUser>>
|
|
|
|
export const useUserCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<UserCreateData, ResponseError, UserCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<UserCreateData, ResponseError, UserCreateVariables>(
|
|
(vars) => createUser(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(authKeys.users(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create user: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|