Files
supabase/apps/studio/data/config/project-postgrest-config-query.ts
Saxon FletcherandCharis Lam 0dab4d93fe Enable or disable Data API access per table (#41218)
* feat(studio): add mutation to update table access privileges

Adds a mutation to update table grants for `anon` and `authenticated`
roles.

* feat(studio): add data api toggles

Add toggles to the Table Editor, allowing fine-grained grants to the
anon and authenticated roles.

* fix(studio): don't show rls policies warning if table not exposed

RLS policies warning was showing in Table Editor side panel even if
table was not exposed due to no grants to anon/authenticated.

* fixup! feat(studio): add data api toggles

* fixup! feat(studio): add data api toggles

* fix(studio): revalidate rls lints when table grants are toggled

---------

Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-01-07 12:12:15 -05:00

60 lines
2.0 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { configKeys } from './keys'
/**
* Parses the exposed schema string returned from PostgREST config.
*
* @param schemaString - e.g., `public,graphql_public`
*/
export const parseDbSchemaString = (schemaString: string): string[] => {
return schemaString
.split(',')
.map((s) => s.trim())
.filter((s) => s.length > 0)
}
export type ProjectPostgrestConfigVariables = {
projectRef?: string
}
type PostgrestConfigResponse = components['schemas']['GetPostgrestConfigResponse'] & {
db_pool: number | null
}
export async function getProjectPostgrestConfig(
{ projectRef }: ProjectPostgrestConfigVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/platform/projects/{ref}/config/postgrest', {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
// [Joshen] Not sure why but db_pool isn't part of the API typing
// https://github.com/supabase/platform/blob/develop/api/src/routes/platform/projects/ref/config/postgrest.dto.ts#L6
return data as unknown as PostgrestConfigResponse
}
export type ProjectPostgrestConfigData = Awaited<ReturnType<typeof getProjectPostgrestConfig>>
export type ProjectPostgrestConfigError = ResponseError
export const useProjectPostgrestConfigQuery = <TData = ProjectPostgrestConfigData>(
{ projectRef }: ProjectPostgrestConfigVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ProjectPostgrestConfigData, ProjectPostgrestConfigError, TData> = {}
) =>
useQuery<ProjectPostgrestConfigData, ProjectPostgrestConfigError, TData>({
queryKey: configKeys.postgrest(projectRef),
queryFn: ({ signal }) => getProjectPostgrestConfig({ projectRef }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})