mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 03:15:06 +03:00
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { PermissionAction } from '@supabase/shared-types/out/constants'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { components } from 'api-types'
|
|
|
|
import { organizationKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type OrganizationPaymentMethodsVariables = { slug?: string }
|
|
export type OrganizationPaymentMethod = components['schemas']['PaymentsResponse']['data'][0]
|
|
|
|
export async function getOrganizationPaymentMethods(
|
|
{ slug }: OrganizationPaymentMethodsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!slug) throw new Error('slug is required')
|
|
|
|
const { data, error } = await get(`/platform/organizations/{slug}/payments`, {
|
|
params: {
|
|
path: {
|
|
slug,
|
|
},
|
|
},
|
|
headers: {
|
|
Version: '2',
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type OrganizationPaymentMethodsData = Awaited<
|
|
ReturnType<typeof getOrganizationPaymentMethods>
|
|
>
|
|
export type OrganizationPaymentMethodsError = ResponseError
|
|
|
|
export const useOrganizationPaymentMethodsQuery = <TData = OrganizationPaymentMethodsData>(
|
|
{ slug }: OrganizationPaymentMethodsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<
|
|
OrganizationPaymentMethodsData,
|
|
OrganizationPaymentMethodsError,
|
|
TData
|
|
> = {}
|
|
) => {
|
|
const { can: canReadSubscriptions } = useAsyncCheckPermissions(
|
|
PermissionAction.BILLING_READ,
|
|
'stripe.payment_methods'
|
|
)
|
|
return useQuery<OrganizationPaymentMethodsData, OrganizationPaymentMethodsError, TData>({
|
|
queryKey: organizationKeys.paymentMethods(slug),
|
|
queryFn: ({ signal }) => getOrganizationPaymentMethods({ slug }, signal),
|
|
enabled: enabled && typeof slug !== 'undefined' && canReadSubscriptions,
|
|
...options,
|
|
})
|
|
}
|