The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
import { useQuery } from '@tanstack/react-query'
import type { components } from 'api-types'
import { organizationKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, 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 = ResponseError
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
}