Files
supabase/apps/studio/data/api-keys/api-keys-query.ts
T
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## What

Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.

## Why

Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.

## Notes for reviewers

- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
2026-11-03 11:09:30 -05:00

125 lines
3.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useCallback } from 'react'
import { apiKeysKeys } from './keys'
import { get, handleError, unguarded } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
type LegacyKeys = {
api_key: string
description?: string | null
hash?: string | null
id?: string | null
inserted_at?: string | null
name: string
prefix?: string | null
secret_jwt_template?: { role: string } | null
type: 'legacy' | null
updated_at?: string | null
}
type SecretKeys = {
api_key: string
description?: string
hash: string
id: string
inserted_at: string
name: string
prefix: string
secret_jwt_template: { role: string }
type: 'secret'
updated_at?: string
}
type PublishableKeys = {
api_key: string
description?: string
hash?: string
id: string
inserted_at: string
name: string
prefix?: string
secret_jwt_template?: { role: string } | null
type: 'publishable'
updated_at?: string
}
interface APIKeysVariables {
projectRef?: string
reveal?: boolean
}
export type APIKey = LegacyKeys | SecretKeys | PublishableKeys
async function getAPIKeys({ projectRef, reveal }: APIKeysVariables, signal?: AbortSignal) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get(`/v1/projects/{ref}/api-keys`, {
params: { path: { ref: projectRef }, query: { reveal: reveal ? 'true' : 'false' } },
signal,
grant: unguarded.migrating,
})
if (error) handleError(error)
// [Jonny]: Overriding the types here since some stuff is not actually nullable or optional
return data as unknown as APIKey[]
}
export type APIKeysData = Awaited<ReturnType<typeof getAPIKeys>>
export const useAPIKeysQuery = <TData = APIKeysData>(
{ projectRef, reveal = false }: APIKeysVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<APIKeysData, StudioError, TData> = {}
) => {
return useQuery<APIKeysData, StudioError, TData>({
queryKey: apiKeysKeys.list(projectRef, reveal),
queryFn: ({ signal }) => getAPIKeys({ projectRef, reveal }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})
}
const EmptyKey = {
anonKey: undefined,
serviceKey: undefined,
publishableKey: undefined,
secretKey: undefined,
allSecretKeys: [],
}
export const getKeys = (apiKeys: APIKey[] = []) => {
if (!Array.isArray(apiKeys)) {
return EmptyKey
}
const anonKey = apiKeys.find((x) => x.name === 'anon')
const serviceKey = apiKeys.find((x) => x.name === 'service_role')
// [Joshen] For now I just want 1 of each, I don't need all
const publishableKey = apiKeys.find((x) => x.type === 'publishable')
const secretKey = apiKeys.find((x) => x.type === 'secret')
const allSecretKeys = apiKeys.filter((x) => x.type === 'secret')
return { anonKey, serviceKey, publishableKey, secretKey, allSecretKeys }
}
const PlaceholderData: APIKeysData = []
export const useAPIKeys = (
variables: APIKeysVariables,
options: UseCustomQueryOptions<APIKeysData, StudioError, APIKeysData> = {}
) => {
const select = useCallback(
(data: APIKeysData) => getKeys(options.enabled ? data : []),
[options.enabled]
)
return useAPIKeysQuery(variables, {
...options,
placeholderData: PlaceholderData,
select,
})
}