Commit Graph
4 Commits
Author SHA1 Message Date
K-Dog (Kevin) 5dfd86d35c fix: regressed studio typecheck performance (#52677)
Heads up: Generated with Claude Opus 5.5

CheckedInit made TypeScript infer each call's init through conditional
and Omit types over the full API paths, at every typed get/post/put/
patch/del call site. A cold `tsc --noEmit` spent 92s type-checking and
peaked at 17 GB of memory.

CheckedUrl already rejects missing, mismatched, and unexpected grants,
so the init check was redundant. Remove CheckedInit and ParseAsContext
(which only worked around CheckedInit breaking parseAs inference) and
type init the way openapi-fetch does.

Cold check time: 92s -> 5.4s. Instantiations: 34.9M -> 16.4M. Peak
memory: 17.3 GB -> 5.5 GB. All guarded-fetch type tests still fail for
the same reasons.
2026-11-03 14:33:17 +08:00
Charis 7ce15123ab feat(studio): grant infra for production and development branch variants (#52372)
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`.
2026-10-27 10:48:24 -04:00
Charis cb4da25dcd feat(studio): readable type errors for missing or mismatched grants (#52308)
## What

Grant errors from the guarded fetch wrappers (`get`, `post`, …) are now
short and say what went wrong.

Studio sets `noErrorTruncation: true`, so when a grant was missing, or
the init was passed as a variable with the wrong grant, TypeScript
printed the full structural type of the init (several times over). With
a real request body this ran to roughly 11–12k characters per error.

## How

- `CheckedUrl` checks the grant on the `url` argument. TypeScript
reports the first failing argument, so the error now prints the short
path literal plus a `GrantError<"…">` message:
- `This endpoint requires a grant or 'unguarded'. The grant is missing
or may be undefined.`
  - `The grant is for a different endpoint or method.`
  - `This endpoint has no FGA annotation. Remove the grant.`
- `GrantProblem` mirrors `GrantOption`, so the `url` check is never
stricter than the existing init check. The init check (`CheckedInit`) is
unchanged and still enforced.
- `Init`'s constraint includes `grant?: unknown`, so an init like `{
grant }` on an endpoint with all-optional options no longer fails the
weak-type check and falls back to the constraint (which hid the real
grant).
- Init optionality is based on `Init` rather than the checked type, so
calling an annotated endpoint with no init reports the grant message
instead of "Expected 2 arguments".

Every case is now under ~430 characters on both TypeScript 6.0.2 and
7.0.2.

## Testing

- Full Studio typecheck passes on TS 7 and TS 6, so every existing
`@ts-expect-error` in `guarded-fetch.test.ts` still fires and accepted
calls still compile.
- `guarded-fetch.test.ts` passes, including the return-type equality
checks against the raw client.

Resolves FE-4722
2026-10-26 10:51:08 -04:00
Charis 254e24b170 feat(studio): require grants in fetch wrappers for FGA-annotated endpoints (#52238)
## Problem

Resolves FE-4689 and FE-4690.

Builds on #52105. Studio's fetch wrappers don't know which FGA
permissions an endpoint requires, so a UI guard and the request it
protects can drift apart. This makes the `grant` minted by
`useGrant`/`getGrant`/`ensureGrant` part of the request itself: calling
an annotated endpoint without a matching grant is a compile error.

## Solution

### What changed

- **Fetch wrappers** (`data/fetchers.ts`, `data/guarded-fetch.ts`):
`get`/`post`/`put`/`patch`/`del` keep their names, so no imports change.
- The `grant` option is required for endpoints annotated with
`x-fga-permissions`, and forbidden for unannotated ones.
- It accepts a `Grant` for that exact endpoint (or one of its variants)
or `unguarded`.
  - The wrapper strips the grant before calling openapi-fetch.
  - `head`/`trace`/`options` are unchanged.
- **Owned params**: refinements can declare query params the wrapper
sets from the grant's variant (`ownedQuery`). These params are removed
from the init type (`Omit<Query, Owned> & { [K in Owned]?: never }`), so
callers can't pass them. Nothing uses this yet: the api-keys `reveal`
variant is the first.
- **`assertGrant`** runs in development only and throws
`GrantAssertionError`. It checks:
  - the endpoint;
  - a missing grant;
  - the variant against `when(init)` predicates;
- the path `ref` against the grant's project, also accepting the parent
project ref for a grant minted on a branch;
  - the path `slug` against the grant's organization.
- **Escape hatches**:
  - `unguarded.migrating` for unmigrated call sites.
- `unguarded.byDesign('reason')` for calls that intentionally skip the
check.
- **Grants** now record `permissionsProjectRef`, the ref permissions
were evaluated against. It's required once the scope is resolved.
- **Readable errors**: a missing or wrong grant reports the caller's own
init type plus the expected `grant` type, instead of openapi-fetch's
full options type. Under `noErrorTruncation`, the old message was tens
of KB per call site.
- **`executeSql`** now requires a grant for `POST
/platform/pg-meta/{ref}/query`, so every caller counts as unmigrated.
- **Codemod**: added `grant: unguarded.migrating` at every existing call
site that hits an annotated endpoint, including all `executeSql`
callers. No behavior change. A follow-up lint rule ratchets the count
down.
- **Dependencies**: adds `openapi-typescript-helpers` (the version
openapi-fetch already uses) for the helper types. Only openapi-fetch's
unexported `InitParam` is copied locally.

### Type-level notes

- The grant is checked in the parameter type rather than in `Init`'s
constraint. That way `Init` is still inferred from the argument, which
keeps both the return type and the error message tied to the caller's
code.
- The grant is replaced rather than intersected. Intersecting a wrong
grant with the expected one can reduce the init type to `never`, which
then accepts the call.
- `NoInfer<Path>` stops a grant's endpoint from becoming an inference
candidate for the path.

## Review instructions

### Tests

- `data/guarded-fetch.test.ts`:
- Type equality with the raw client: required params and body,
template-literal paths, `parseAs`, `signal`/`headers`, optional-init
endpoints, and `unguarded`. An openapi-fetch upgrade that breaks the
local `InitParam` copy fails typecheck.
  - Negative type cases:
    - a missing or `undefined` grant;
    - a missing init on an annotated endpoint;
    - a grant for a different path or method;
    - any grant or `unguarded` on an unannotated endpoint;
    - a forged grant;
    - params and body still checked;
    - an unknown path.
- `guardMethod` and `assertGrant` runtime behavior, including
branch-parent refs and org slugs.
- Variant and owned-param tests are deferred to the api-keys (`reveal`)
and pg-meta (`read`/`write`) PRs, since no refinements exist yet.

1. Run `pnpm typecheck` and confirm Studio passes. Then remove a `grant:
unguarded.migrating` and check that the error is short and names the
expected grant.
2. Run `pnpm test:studio`.
3. Skim the codemod commit: it only adds `grant: unguarded.migrating`
and the `unguarded` import.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-26 07:58:22 -04:00