View Source SanityEx.Client (SanityEx v0.1.1)

SanityEx.Client is used for interactions with the Sanity HTTP API.

It provides an interface for making queries, sending patches, and constructing URLs for asset IDs.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Executes a list of patches on the Sanity API.

Executes a GROQ query against the Sanity API.

Executes multiple GROQ queries against the Sanity API asynchronously.

Starts the client with the given options.

Constructs a CDN URL for a given Sanity asset ID.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec init(Keyword.t()) :: {:ok, map()}

Callback implementation for GenServer.init/1.

@spec patch([map()]) :: {:ok, String.t()} | {:error, any()}

Executes a list of patches on the Sanity API.

This function takes a list of patches and sends them to the Sanity API as a single transaction. Each patch should be a map that defines an operation on a document.

params

Params

  • patches: A list of patches. Each patch is a map that defines an operation on a document.

returns

Returns

The function returns the body of the response as a string on success, or an error tuple on failure.

examples

Examples

iex> SanityEx.Client.patch([
  %{
    "patch" => %{
      "id" => "person-1234",
      "set" => %{"name" => "Remington Steele"}
    }
  },
  %{
    "patch" => %{
      "id" => "remingtons",
      "insert" => %{
        "after" => "people[-1]",
        "items" => [
          %{
            "_type" => "reference",
            "_ref" => "person-1234"
          }
        ]
      }
    }
  }
])
Link to this function

query(query_str, api_cdn \\ true)

View Source
@spec query(String.t(), boolean()) ::
  {:ok, String.t()}
  | {:error, {:sanity_error, integer(), String.t()}}
  | {:error, {:fetch_error, String.t()}}
  | {:error, {:params, String.t()}}

Executes a GROQ query against the Sanity API.

Takes a GROQ query as a string and sends it to the Sanity API, returning the body of the response as a string, or an error tuple on failure.

params

Params

  • query_str - The GROQ query string to be executed.
  • api_cdn - Query against Sanity's CDN for faster response times - defaults to true if not specified

examples

Examples

iex> SanityEx.Client.query("*[_type == 'post']{title, slug, _id}")
{:ok, sanity_response} | {:error, {:sanity_error, status_code, message}} | {:error, {:fetch_error, message}} | {:error, {:params, message}}
Link to this function

query_async(queries, api_cdn \\ true)

View Source
@spec query_async([String.t()], boolean()) :: [
  {String.t(), {:ok, String.t()} | {:error, any()}}
]

Executes multiple GROQ queries against the Sanity API asynchronously.

Takes a list of GROQ queries as strings and sends them to the Sanity API. Each query is executed in a separate process. All queries are waited on and returned as a list. Each result is a tuple, where the first element is the query and the second is its result.

params

Params

  • queries: A list of GROQ queries as strings
  • api_cdn: Query against Sanity's CDN for faster response times - defaults to true if not specified

returns

Returns

A list of tuples where the first element is the query string and the second element is the result of that query. The result of a query is the body of the response as a string on success, or an error tuple on failure.

examples

Examples

iex> SanityEx.Client.query_async(["*[_type == 'post']{title, slug, _id}", "*[_type == 'author']{name, _id}"])
[
  {"*[_type == 'post']{title, slug, _id}", {:ok, sanity_response1}},
  {"*[_type == 'author']{name, _id}", {:ok, sanity_response2}}
]
@spec start_link(Keyword.t()) :: {:ok, pid()} | {:error, any()}

Starts the client with the given options.

This function is typically called when your application starts.

options

Options

  • :project_id - The project ID for your Sanity project
  • :api_version - The dated version of the Sanity API to use.
  • :token - The API token for making authorized requests.

The following are optional:

  • :dataset - The dataset to query against. Defaults to production.
  • :asset_url - The base asset URL to use. Defaults to cdn.sanity.io/images/{project_id}/{dataset}/.

examples

Examples

iex> SanityEx.Client.start_link([
    project_id: "your_project_id",
    api_version: "v2021-06-07",
    dataset: "dev",
    token: "your_token"
  ])
{:ok, %{ ... }}
Link to this function

url_for(asset_id, query_params \\ %{})

View Source
@spec url_for(String.t(), map() | String.t()) ::
  {:ok, String.t()} | {:error, {:params, String.t()}}

Constructs a CDN URL for a given Sanity asset ID.

Takes an asset ID and optional query parameters map, and returns the constructed URL as a string. It returns {:ok, url} on success, or an error tuple on failure.

params

Params

  • asset_id - The ID of the asset for which the URL should be constructed.
  • query_params - Optional query parameters map to be appended to the URL.

examples

Examples

iex> SanityEx.Client.url_for("image-asset-id")
{:ok, "https://cdn.sanity.io/images/..."}

iex> SanityEx.Client.url_for("image-asset-id", %{w: 100, h: 100})
{:ok, "https://cdn.sanity.io/images/...?w=100&h=100"}