import type { ClientPerspective, ContentSourceMap, QueryParams, ResponseQueryOptions, } from '@sanity/client' import type {ResolveStudioUrl, StudioUrl} from '@sanity/client/csm' import type { createQueryStore as createCoreQueryStore, EnableLiveModeOptions, QueryStoreState, } from '@sanity/core-loader' import type {EncodeDataAttributeFunction} from '@sanity/core-loader/encode-data-attribute' export type * from '@sanity/core-loader' type WithEncodeDataAttribute = { encodeDataAttribute: EncodeDataAttributeFunction } export type UseQueryHook = ( query: string, params?: QueryParams, options?: UseQueryOptions, ) => QueryStoreState & WithEncodeDataAttribute export interface QueryResponseInitial { data: QueryResponseResult sourceMap: ContentSourceMap | undefined /** * The perspective used to fetch the data, if not provided it'll assume 'published' */ perspective?: ClientPerspective } export interface UseQueryOptions { /** * Initial `data` and `sourceMap`, used with SSR hydration and is required if `ssr: true` * and an optional speed optimization if `ssr: false`. * It's recommended to set `initial` to the return value of `loadQuery()`. * @example * ```ts * const query = `*[_type == "author" && slug.current == $slug][0]` * export const getServerSideProps = async ({params}) => { * const initial = await loadQuery(query, params) * return { props: { params, initial } } * } * export default function Page({params, initial}) { * const {data} = useQuery(query, params, {initial}) * } * ``` */ initial?: QueryResponseInitial } export interface UseQueryOptionsUndefinedInitial { /** * Initial `data` and `sourceMap`, used with SSR hydration and is required if `ssr: true` * and an optional speed optimization if `ssr: false`. * It's recommended to set `initial` to the return value of `loadQuery()`. * @example * ```ts * const query = `*[_type == "author" && slug.current == $slug][0]` * export const getServerSideProps = async ({params}) => { * const initial = await loadQuery(query, params) * return { props: { params, initial } } * } * export default function Page({params, initial}) { * const {data} = useQuery(query, params, {initial}) * } * ``` */ initial?: undefined } export type NonUndefinedGuard = T extends undefined ? never : T export interface UseQueryOptionsDefinedInitial { /** * Initial `data` and `sourceMap`, used with SSR hydration and is required if `ssr: true` * and an optional speed optimization if `ssr: false`. * It's recommended to set `initial` to the return value of `loadQuery()`. * @example * ```ts * const query = `*[_type == "author" && slug.current == $slug][0]` * export const getServerSideProps = async ({params}) => { * const initial = await loadQuery(query, params) * return { props: { params, initial } } * } * export default function Page({params, initial}) { * const {data} = useQuery(query, params, {initial}) * } * ``` */ initial: NonUndefinedGuard> } export type UseLiveModeHook = ( options: EnableLiveModeOptions & { /** * Set this option to activate `encodeDataAttribute` on `useQuery` hooks when stega isn't used. */ studioUrl?: StudioUrl | ResolveStudioUrl | undefined }, ) => void export interface QueryStore { loadQuery: ( query: string, params?: QueryParams, options?: Pick< ResponseQueryOptions, 'perspective' | 'cache' | 'next' | 'useCdn' | 'stega' | 'tag' | 'headers' >, ) => Promise> setServerClient: ReturnType['setServerClient'] useQuery: { ( query: string, params?: QueryParams, options?: UseQueryOptionsUndefinedInitial, ): QueryStoreState & WithEncodeDataAttribute ( query: string, params?: QueryParams, options?: UseQueryOptionsDefinedInitial, ): Omit, 'data'> & { data: QueryResponseResult } & WithEncodeDataAttribute // ( // query: string, // params?: QueryParams, // options?: UseQueryOptions, // ): QueryStoreState & WithEncodeDataAttribute } useLiveMode: UseLiveModeHook }