mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 12:55:06 +03: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.
69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import { QueryClient, useQuery } from '@tanstack/react-query'
|
|
|
|
import { components } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { MANAGED_BY, ManagedBy } from 'lib/constants/infrastructure'
|
|
import { useProfile } from 'lib/profile'
|
|
import type { Organization, ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { organizationKeys } from './keys'
|
|
|
|
export type OrganizationBase = components['schemas']['OrganizationResponse']
|
|
|
|
export function castOrganizationResponseToOrganization(org: OrganizationBase): Organization {
|
|
return {
|
|
...org,
|
|
billing_email: org.billing_email ?? 'Unknown',
|
|
managed_by: getManagedBy(org),
|
|
partner_id: org.slug.startsWith('vercel_') ? org.slug.replace('vercel_', '') : undefined,
|
|
}
|
|
}
|
|
|
|
function getManagedBy(org: OrganizationBase): ManagedBy {
|
|
switch (org.billing_partner) {
|
|
case 'vercel_marketplace':
|
|
return MANAGED_BY.VERCEL_MARKETPLACE
|
|
case 'aws_marketplace':
|
|
return MANAGED_BY.AWS_MARKETPLACE
|
|
default:
|
|
return MANAGED_BY.SUPABASE
|
|
}
|
|
}
|
|
|
|
export async function getOrganizations({
|
|
signal,
|
|
headers,
|
|
}: {
|
|
signal?: AbortSignal
|
|
headers?: Record<string, string>
|
|
}): Promise<Organization[]> {
|
|
const { data, error } = await get('/platform/organizations', { signal, headers })
|
|
|
|
if (error) handleError(error)
|
|
if (!Array.isArray(data)) return []
|
|
|
|
return data
|
|
.map(castOrganizationResponseToOrganization)
|
|
.sort((a, b) => a.name.localeCompare(b.name))
|
|
}
|
|
|
|
export type OrganizationsData = Awaited<ReturnType<typeof getOrganizations>>
|
|
export type OrganizationsError = ResponseError
|
|
|
|
export const useOrganizationsQuery = <TData = OrganizationsData>({
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<OrganizationsData, OrganizationsError, TData> = {}) => {
|
|
const { profile } = useProfile()
|
|
return useQuery<OrganizationsData, OrganizationsError, TData>({
|
|
queryKey: organizationKeys.list(),
|
|
queryFn: ({ signal }) => getOrganizations({ signal }),
|
|
enabled: enabled && profile !== undefined,
|
|
...options,
|
|
staleTime: 30 * 60 * 1000,
|
|
})
|
|
}
|
|
|
|
export function invalidateOrganizationsQuery(client: QueryClient) {
|
|
return client.invalidateQueries({ queryKey: organizationKeys.list() })
|
|
}
|