mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 23:19:23 +08:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { documentKeys } from './keys'
|
|
|
|
export type DocType = 'standard-security-questionnaire' | 'soc2-type-2-report'
|
|
|
|
export type DocumentVariables = {
|
|
orgSlug?: string
|
|
docType?: DocType
|
|
}
|
|
|
|
export async function getDocument({ orgSlug, docType }: DocumentVariables, signal?: AbortSignal) {
|
|
if (!orgSlug) throw new Error('orgSlug is required')
|
|
|
|
if (docType === 'standard-security-questionnaire') {
|
|
const { data, error } = await get(
|
|
`/platform/organizations/{slug}/documents/standard-security-questionnaire`,
|
|
{
|
|
params: { path: { slug: orgSlug } },
|
|
signal,
|
|
}
|
|
)
|
|
|
|
if (error) handleError(error)
|
|
return data as { fileUrl: string }
|
|
}
|
|
|
|
if (docType === 'soc2-type-2-report') {
|
|
const { data, error } = await get(
|
|
`/platform/organizations/{slug}/documents/soc2-type-2-report`,
|
|
{
|
|
params: { path: { slug: orgSlug } },
|
|
signal,
|
|
}
|
|
)
|
|
if (error) throw error
|
|
|
|
return data as { fileUrl: string }
|
|
}
|
|
}
|
|
|
|
export type DocumentData = Awaited<ReturnType<typeof getDocument>>
|
|
export type DocumentError = ResponseError
|
|
|
|
export const useDocumentQuery = <TData = DocumentData>(
|
|
{ orgSlug, docType }: DocumentVariables,
|
|
{ enabled = true, ...options }: UseCustomQueryOptions<DocumentData, DocumentError, TData> = {}
|
|
) =>
|
|
useQuery<DocumentData, DocumentError, TData>({
|
|
queryKey: documentKeys.resource(orgSlug, docType),
|
|
queryFn: ({ signal }) => getDocument({ orgSlug, docType }, signal),
|
|
enabled: enabled && typeof orgSlug !== 'undefined' && typeof docType !== 'undefined',
|
|
...options,
|
|
})
|