Files
supabase/apps/studio/lib/api/self-hosted/api-keys.ts
Alaister YoungandAlaister Young 20a5212c42 [FE-5114] (part 3) – refactor(studio): migrate pg-meta and v1 routes (#54030)
## Problem

Studio's `routes/api/platform/pg-meta/**` and `routes/api/v1/**` routes
still run legacy Next Pages Router handlers through
`compat/next/api.ts`, and 10 legacy `pg-meta` GET proxy routes are no
longer called anywhere in the repo. Part 3 of 7 for
[FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the)
migrates the 9 active routes to native TanStack Start handlers and
deletes the 10 unused proxy routes.

## Solution

**Changed:**

- Migrated `platform/pg-meta/$ref/query/index.ts` and all 8 `v1` routes
(`api-keys`, `api-keys/$id`, `config/auth/signing-keys/{index,legacy}`,
`database/migrations`, `functions/{index,$slug/index}`,
`types/typescript`) to `createApiHandlers`; `query` and
`database/migrations` share `pgMetaErrorResponse`
- Enabled `{ withAuth: true }` on `api-keys`, `api-keys/$id`, and
`config/auth/signing-keys/{index,legacy}` for consistency with the other
`v1` routes
- Updated `api-keys` route tests to use `callRoute`

**Added:**

- `pgMetaErrorResponse(error)` in `lib/api/self-hosted/pg-meta.ts`
(keeps a `PgMetaDatabaseError`'s status and `{ message, formattedError
}` payload expected by `data/error-classification.ts`, otherwise `500`)
- Unit test for `pg-meta/$ref/query`
(`tests/routes/api/platform/pg-meta/$ref/query/index.test.ts`)

**Removed:**

- Removed all 10 unused `pg-meta` GET proxy routes (`column-privileges`,
`extensions`, `foreign-tables`, `materialized-views`, `policies`,
`publications`, `tables`, `triggers`, `types`, `views`), removed their
`FETCH_TYPE_BYPASS_EXCEPTIONS` entries from `eslint.config.cjs`, and
regenerated `routeTree.gen.ts`
- Removed `getPgMetaRedirectUrl` (`lib/api/apiHelpers.ts`) and
`parseRevealQuery` (`lib/api/self-hosted/api-keys.ts`)

**Behavior changes vs master:**

- Routes forward only `cookie` and `x-connection-encrypted` to pg-meta
via `constructHeaders(Object.fromEntries(request.headers))`, matching
what master's adapter forwarded
- `database/migrations` GET now forwards headers to
`listMigrationVersions` (master passed them as the wrong argument)
- `v1/projects/$ref/api-keys*` and `config/auth/signing-keys*` now pass
`{ withAuth: true }` (no-op in self-hosted mode; consistent with the
other `v1` routes)
- The 10 unused `pg-meta` GET proxy routes have been removed
(`pg-meta/$ref/query` and `v1/projects/$ref/types/typescript` remain)

## Review instructions

1. Run `pnpm --filter studio exec vitest run tests/routes/api/v1/
tests/routes/api/platform/pg-meta/`.
2. On self-hosted Studio, load tables/views/policies/extensions, run a
query in the SQL Editor, check API keys, and generate TypeScript types.

## Checklist

- [x] 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)

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 21:44:59 +08:00

94 lines
2.3 KiB
TypeScript

/**
* Both CLI and self-hosted inject the same env vars.
*/
import { assertSelfHosted } from './util'
export type NonPlatformApiKey = {
name: string
api_key: string
id: string
type: 'legacy' | 'publishable' | 'secret'
hash: string
prefix: string
description: string
}
/**
* Length of the identifying prefix shown for a secret key before it is
* revealed. Mirrors the platform management API and the `ApiKeyPill` UI.
*/
const SECRET_KEY_VISIBLE_PREFIX_LENGTH = 15
export function getNonPlatformApiKeys(): NonPlatformApiKey[] {
assertSelfHosted()
const keys: NonPlatformApiKey[] = [
{
name: 'anon',
api_key: process.env.SUPABASE_ANON_KEY ?? '',
id: 'anon',
type: 'legacy',
hash: '',
prefix: '',
description: 'Legacy anon API key',
},
{
name: 'service_role',
api_key: process.env.SUPABASE_SERVICE_KEY ?? '',
id: 'service_role',
type: 'legacy',
hash: '',
prefix: '',
description: 'Legacy service_role API key',
},
]
const publishableKey = process.env.SUPABASE_PUBLISHABLE_KEY
if (publishableKey) {
keys.push({
name: 'publishable',
api_key: publishableKey,
id: 'publishable',
type: 'publishable',
hash: '',
prefix: '',
description: 'Publishable API key (anon role)',
})
}
const secretKey = process.env.SUPABASE_SECRET_KEY
if (secretKey) {
keys.push({
name: 'secret',
api_key: secretKey,
id: 'secret',
type: 'secret',
hash: '',
// Only expose the prefix when the key is genuinely longer than the prefix.
prefix:
secretKey.length > SECRET_KEY_VISIBLE_PREFIX_LENGTH
? secretKey.slice(0, SECRET_KEY_VISIBLE_PREFIX_LENGTH)
: '',
description: 'Secret API key (service_role)',
})
}
return keys
}
export function applyRevealToApiKey(key: NonPlatformApiKey, reveal: boolean): NonPlatformApiKey {
if (key.type !== 'secret' || reveal) return key
return { ...key, api_key: key.prefix }
}
export function getNonPlatformApiKeyById(
id: string,
reveal: boolean
): NonPlatformApiKey | undefined {
const key = getNonPlatformApiKeys().find((entry) => entry.id === id)
if (!key) return undefined
return applyRevealToApiKey(key, reveal)
}