mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 09:20:21 -04:00
671c109fa0
* Draft * Draft * Draft * fix: wrong query key * Final tweaks * Add to other pages * Update apps/docs/content/guides/getting-started/quickstarts/ios-swiftui.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/kotlin.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/nextjs.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/nuxtjs.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/reactjs.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/solidjs.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/sveltekit.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/vue.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/getting-started/quickstarts/refine.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.tsx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.tsx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Prettier --------- Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import type { ResponseError } from '~/types/fetch'
|
|
import { get } from './fetchWrappers'
|
|
import { UseQueryOptions, useQuery } from '@tanstack/react-query'
|
|
|
|
const projectApiKeys = {
|
|
api: (projectRef: string | undefined) => ['projects', projectRef, 'api'] as const,
|
|
settings: (projectRef: string | undefined) => ['projects', projectRef, 'settings'] as const,
|
|
}
|
|
|
|
export interface ProjectApiVariables {
|
|
projectRef?: string
|
|
}
|
|
type ProjectApiError = ResponseError
|
|
|
|
export type ProjectKeys = Awaited<ReturnType<typeof getProjectKeys>>
|
|
|
|
export type ProjectSettings = Awaited<ReturnType<typeof getProjectSettings>>
|
|
|
|
async function getProjectKeys({ projectRef }: ProjectApiVariables, signal?: AbortSignal) {
|
|
if (!projectRef) {
|
|
throw Error('projectRef is required')
|
|
}
|
|
|
|
const { data, error } = await get('/v1/projects/{ref}/api-keys', {
|
|
params: {
|
|
path: { ref: projectRef },
|
|
},
|
|
signal,
|
|
})
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
async function getProjectSettings({ projectRef }: ProjectApiVariables, signal?: AbortSignal) {
|
|
if (!projectRef) {
|
|
throw Error('projectRef is required')
|
|
}
|
|
|
|
const { data, error } = await get('/platform/projects/{ref}/settings', {
|
|
params: {
|
|
path: { ref: projectRef },
|
|
},
|
|
signal,
|
|
})
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
export function useProjectKeysQuery<TData = ProjectKeys>(
|
|
{ projectRef }: ProjectApiVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: Omit<UseQueryOptions<ProjectKeys, ProjectApiError, TData>, 'queryKey'> = {}
|
|
) {
|
|
return useQuery<ProjectKeys, ProjectApiError, TData>({
|
|
queryKey: projectApiKeys.api(projectRef),
|
|
queryFn: ({ signal }) => getProjectKeys({ projectRef }, signal),
|
|
enabled,
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export function useProjectSettingsQuery<TData = ProjectSettings>(
|
|
{ projectRef }: ProjectApiVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: Omit<UseQueryOptions<ProjectSettings, ProjectApiError, TData>, 'queryKey'> = {}
|
|
) {
|
|
return useQuery<ProjectSettings, ProjectApiError, TData>({
|
|
queryKey: projectApiKeys.settings(projectRef),
|
|
queryFn: ({ signal }) => getProjectSettings({ projectRef }, signal),
|
|
enabled,
|
|
...options,
|
|
})
|
|
}
|