Class SanityClient

Constructors

Properties

assets: AssetsClient
datasets: DatasetsClient
listen: {
    <R extends Record<string, any> = Record<string, any>>(
        this: ObservableSanityClient | SanityClient,
        query: string,
        params?: ListenParams,
    ): Observable<MutationEvent<R>>;
    <R extends Record<string, any> = Record<string, any>>(
        this: ObservableSanityClient | SanityClient,
        query: string,
        params?: ListenParams,
        options?: ListenOptions,
    ): Observable<ListenEvent<R>>;
} = _listen

Instance properties

Type declaration

Observable version of the Sanity client, with the same configuration as the promise-based one

projects: ProjectsClient

Methods

  • Internal

    Perform an HTTP request a /data sub-endpoint NOTE: Considered internal, thus marked as deprecated. Use request instead.

    Parameters

    • endpoint: string

      Endpoint to hit (mutate, query etc)

    • body: unknown

      Request body

    • Optionaloptions: BaseMutationOptions

      Request options

    Returns Promise<any>

    • Use request() or your own HTTP library instead
  • Get a Sanity API URL for the data operation and path provided

    Parameters

    • operation: string

      Data operation (eg query, mutate, listen or similar)

    • Optionalpath: string

      Path to append after the operation

    Returns string

  • Fetch a single document with the given ID.

    Type Parameters

    • R extends Record<string, any> = Record<string, any>

    Parameters

    • id: string

      Document ID to fetch

    • Optionaloptions: { signal?: AbortSignal; tag?: string }

      Request options

    Returns Promise<undefined | SanityDocument<R>>

  • Fetch multiple documents in one request. Should be used sparingly - performing a query is usually a better option. The order/position of documents is preserved based on the original array of IDs. If any of the documents are missing, they will be replaced by a null entry in the returned array

    Type Parameters

    • R extends Record<string, any> = Record<string, any>

    Parameters

    • ids: string[]

      Document IDs to fetch

    • Optionaloptions: { signal?: AbortSignal; tag?: string }

      Request options

    Returns Promise<(null | SanityDocument<R>)[]>

  • Get a Sanity API URL for the URI provided

    Parameters

    • uri: string

      URI/path to build URL for

    • OptionalcanUseCdn: boolean

      Whether or not to allow using the API CDN for this route

    Returns string

  • Create a new buildable patch of operations to perform

    Parameters

    • documentId: string

      Document ID to patch

    • Optionaloperations: PatchOperations

      Optional object of patch operations to initialize the patch instance with

    Returns Patch

    Patch instance - call .commit() to perform the operations defined

  • Create a new buildable patch of operations to perform

    Parameters

    • documentIds: string[]

      Array of document IDs to patch

    • Optionaloperations: PatchOperations

      Optional object of patch operations to initialize the patch instance with

    Returns Patch

    Patch instance - call .commit() to perform the operations defined

  • Create a new buildable patch of operations to perform

    Parameters

    • selection: MutationSelection

      An object with query and optional params, defining which document(s) to patch

    • Optionaloperations: PatchOperations

      Optional object of patch operations to initialize the patch instance with

    Returns Patch

    Patch instance - call .commit() to perform the operations defined

  • Perform a request against the Sanity API NOTE: Only use this for Sanity API endpoints, not for your own APIs!

    Type Parameters

    • R = any

    Parameters

    Returns Promise<R>

    Promise resolving to the response body

  • Create a new transaction of mutations

    Type Parameters

    • R extends Record<string, any> = Record<string, any>

    Parameters

    • Optionaloperations: Mutation<R>[]

      Optional array of mutation operations to initialize the transaction instance with

    Returns Transaction

  • Clone the client with a new (partial) configuration.

    Parameters

    • OptionalnewConfig: Partial<ClientConfig>

      New client configuration properties, shallowly merged with existing configuration

    Returns SanityClient