mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { QueryClient, useQuery } from '@tanstack/react-query'
|
|
import { platformComponents as components } from 'api-types'
|
|
|
|
import { organizationKeys } from './keys'
|
|
import { getManagedByFromOrganizationPartner } from './managed-by-utils'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { useProfile } from '@/lib/profile'
|
|
import type { Organization, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type OrganizationBase = components['schemas']['OrganizationResponse_Output']
|
|
|
|
export function castOrganizationResponseToOrganization(org: OrganizationBase): Organization {
|
|
return {
|
|
...org,
|
|
billing_email: org.billing_email ?? 'Unknown',
|
|
managed_by: getManagedByFromOrganizationPartner(org.billing_partner, org.integration_source),
|
|
partner_id: org.slug.startsWith('vercel_') ? org.slug.replace('vercel_', '') : undefined,
|
|
}
|
|
}
|
|
|
|
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 = StudioError
|
|
|
|
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() })
|
|
}
|