mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Part 1 of 7 in a stack that splits #52331 into reviewable pieces. Resolves part of [FE-4693](https://linear.app/supabase/issue/FE-4693/pilot-migrate-branches-to-grants-8-endpoints-createbranchmodal). There are no functional changes beyond #52331, and the top of the stack is identical to it. See #52331 for the full rationale. ## Stack 1. 👉 #52372 Grant infra for branch variants 2. #52373 Branches list query 3. #52374 Page gates on the list grant 4. #52375 Branch creation 5. #52376 Branch read, delete, reset, and restore 6. #52377 Branch update and push 7. #52378 Branch diff and merge ## Changes Adds `lib/fga-refinements.ts` entries: `production`/`development` variants for the 8 single-branch endpoints, with a new `variantRequired` flag. The fetch wrapper and `assertGrant` reject a grant without a variant. Also adds the `production` variant for `POST /v1/projects/{ref}/branches` when the body sets `is_default` or `with_data`. Adds `GrantForVariants`, the `useBranchGrant` hook, and shared fixtures in `tests/lib/branch-test-utils.ts`. Behavior-neutral: existing branch calls still use `unguarded.migrating`, which skips these checks. ## Testing `guarded-fetch.test.ts` and `useBranchGrant.test.ts`.
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import type { FgaGuardedEndpoint } from 'api-types'
|
|
|
|
import { useGrant, type GrantResult, type VariantOf } from '@/lib/grants'
|
|
|
|
type BranchVariant = 'production' | 'development'
|
|
|
|
/** Endpoints on a single branch, whose grant needs a production or development variant. */
|
|
export type BranchVariantEndpoint = {
|
|
[E in FgaGuardedEndpoint]: [VariantOf<E>] extends [BranchVariant]
|
|
? [BranchVariant] extends [VariantOf<E>]
|
|
? E
|
|
: never
|
|
: never
|
|
}[FgaGuardedEndpoint]
|
|
|
|
type GrantResultForVariant<E extends FgaGuardedEndpoint, V> =
|
|
V extends VariantOf<E> ? GrantResult<E, V> : never
|
|
|
|
export type BranchGrantResult<E extends BranchVariantEndpoint> = GrantResultForVariant<
|
|
E,
|
|
BranchVariant
|
|
>
|
|
|
|
type BranchGrantScope = { projectRef: string | undefined; isDefault: boolean | undefined }
|
|
|
|
/**
|
|
* Grant for an action on a single branch. The backend requires production permissions for the
|
|
* default branch and development permissions for preview branches. Loading until `isDefault` is
|
|
* known. `projectRef` is the parent project, where branch permissions are evaluated.
|
|
*/
|
|
export function useBranchGrant<E extends BranchVariantEndpoint>(
|
|
endpoint: E,
|
|
scope: BranchGrantScope
|
|
): BranchGrantResult<E>
|
|
export function useBranchGrant(
|
|
endpoint: BranchVariantEndpoint,
|
|
{ projectRef, isDefault }: BranchGrantScope
|
|
): BranchGrantResult<BranchVariantEndpoint> {
|
|
const production = useGrant(endpoint, { projectRef }, { variant: 'production' })
|
|
const development = useGrant(endpoint, { projectRef }, { variant: 'development' })
|
|
|
|
if (isDefault === undefined) return { status: 'loading' }
|
|
return isDefault ? production : development
|
|
}
|