mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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.
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import type { components } from 'api-types'
|
|
|
|
import { organizationKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomQueryOptions } from '@/types'
|
|
|
|
export const FIXED_ROLE_ORDER = ['Owner', 'Administrator', 'Developer', 'Read-only', 'None']
|
|
export type OrganizationRolesVariables = { slug?: string }
|
|
export type OrganizationRolesResponse = components['schemas']['OrganizationRoleResponse_Output']
|
|
export type OrganizationRole =
|
|
components['schemas']['OrganizationRoleResponse_Output']['org_scoped_roles'][0]
|
|
|
|
export async function getOrganizationRoles(
|
|
{ slug }: OrganizationRolesVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!slug) throw new Error('slug is required')
|
|
|
|
const { data, error } = await get('/platform/organizations/{slug}/roles', {
|
|
params: { path: { slug } },
|
|
headers: { Version: 2 },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
|
|
// [Joshen] Temp while API has versioning on this endpoint
|
|
return data as unknown as OrganizationRolesResponse
|
|
}
|
|
|
|
export type OrganizationRolesData = Awaited<ReturnType<typeof getOrganizationRoles>>
|
|
export type OrganizationRolesError = StudioError
|
|
|
|
export const useOrganizationRolesV2Query = <TData = OrganizationRolesData>(
|
|
{ slug }: OrganizationRolesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<OrganizationRolesData, OrganizationRolesError, TData> = {}
|
|
) =>
|
|
useQuery<OrganizationRolesData, OrganizationRolesError, TData>({
|
|
queryKey: organizationKeys.rolesV2(slug),
|
|
queryFn: ({ signal }) => getOrganizationRoles({ slug }, signal),
|
|
enabled: enabled && typeof slug !== 'undefined',
|
|
select: (data) => {
|
|
return {
|
|
...data,
|
|
org_scoped_roles: [...data.org_scoped_roles]
|
|
.sort((a, b) => {
|
|
return FIXED_ROLE_ORDER.indexOf(a.name) - FIXED_ROLE_ORDER.indexOf(b.name)
|
|
})
|
|
.filter((role) => FIXED_ROLE_ORDER.includes(role.name))
|
|
.map((role) => ({ ...role, name: getOverrideRoleName(role.name) })),
|
|
} as TData
|
|
},
|
|
...options,
|
|
})
|
|
|
|
const OVERRIDE_ROLE_NAMES: Record<string, string> = {
|
|
None: 'No-access',
|
|
}
|
|
|
|
function getOverrideRoleName(rolename: string) {
|
|
return OVERRIDE_ROLE_NAMES[rolename] ?? rolename
|
|
}
|