Files
supabase/apps/studio/data/content/sql-folder-contents-query.ts
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

75 lines
2.3 KiB
TypeScript

import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
import { contentKeys } from './keys'
import { SNIPPET_PAGE_LIMIT, withSavedStatus } from './sql-folders-query'
import { get, handleError } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomInfiniteQueryOptions } from '@/types'
export type SQLSnippetFolderContentsVariables = {
projectRef?: string
folderId?: string
cursor?: string
name?: string
sort?: 'name' | 'inserted_at'
}
export async function getSQLSnippetFolderContents(
{ projectRef, folderId, cursor, sort, name }: SQLSnippetFolderContentsVariables,
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
if (typeof folderId === 'undefined') throw new Error('folderId is required')
const sortOrder = sort === 'name' ? 'asc' : 'desc'
const { data, error } = await get('/platform/projects/{ref}/content/folders/{id}', {
params: {
path: { ref: projectRef, id: folderId },
query: {
cursor,
limit: SNIPPET_PAGE_LIMIT.toString(),
sort_by: sort,
sort_order: sortOrder,
name,
},
},
signal,
})
if (error) handleError(error)
return {
...data.data,
contents: (data.data.contents ?? []).map(withSavedStatus),
cursor: data.cursor,
}
}
export type SQLSnippetFolderContentsData = Awaited<ReturnType<typeof getSQLSnippetFolderContents>>
export type SQLSnippetFolderContentsError = StudioError
export const useSQLSnippetFolderContentsQuery = <TData = SQLSnippetFolderContentsData>(
{ projectRef, folderId, name, sort }: Omit<SQLSnippetFolderContentsVariables, 'cursor'>,
{
enabled = true,
...options
}: UseCustomInfiniteQueryOptions<
SQLSnippetFolderContentsData,
SQLSnippetFolderContentsError,
InfiniteData<TData>,
readonly unknown[],
string | undefined
> = {}
) =>
useInfiniteQuery({
queryKey: contentKeys.folderContents(projectRef, folderId, { name, sort }),
queryFn: ({ signal, pageParam }) =>
getSQLSnippetFolderContents({ projectRef, folderId, cursor: pageParam, name, sort }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof folderId !== 'undefined',
initialPageParam: undefined,
getNextPageParam(lastPage) {
return lastPage.cursor
},
...options,
})