mirror of
https://github.com/supabase/supabase.git
synced 2026-05-23 01:39:34 +08:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { PermissionAction } from '@supabase/shared-types/out/constants'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
|
|
import { UseCustomQueryOptions } from 'types'
|
|
import { subscriptionKeys } from './keys'
|
|
|
|
export type OrgPlansVariables = {
|
|
orgSlug?: string
|
|
}
|
|
|
|
export async function getOrgPlans({ orgSlug }: OrgPlansVariables, signal?: AbortSignal) {
|
|
if (!orgSlug) throw new Error('orgSlug is required')
|
|
|
|
const { error, data } = await get('/platform/organizations/{slug}/billing/plans', {
|
|
params: { path: { slug: orgSlug } },
|
|
signal,
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type OrgPlansData = Awaited<ReturnType<typeof getOrgPlans>>
|
|
export type OrgPlansError = unknown
|
|
|
|
export const useOrgPlansQuery = <TData = OrgPlansData>(
|
|
{ orgSlug }: OrgPlansVariables,
|
|
{ enabled = true, ...options }: UseCustomQueryOptions<OrgPlansData, OrgPlansError, TData> = {}
|
|
) => {
|
|
const { can: canReadSubscriptions } = useAsyncCheckPermissions(
|
|
PermissionAction.BILLING_READ,
|
|
'stripe.subscriptions'
|
|
)
|
|
|
|
return useQuery<OrgPlansData, OrgPlansError, TData>({
|
|
queryKey: subscriptionKeys.orgPlans(orgSlug),
|
|
queryFn: ({ signal }) => getOrgPlans({ orgSlug }, signal),
|
|
enabled: enabled && typeof orgSlug !== 'undefined' && canReadSubscriptions,
|
|
...options,
|
|
})
|
|
}
|