View Source Sanity (sanity v0.7.0)

Client library for Sanity CMS. See the README for examples.

Link to this section Summary

Functions

Generates a request for the Doc endpoint.

Generates a request for the Mutate endpoint.

Generates a request to the Query endpoint. Requests to this endpoint may be authenticated or unauthenticated. Unauthenticated requests to a dataset with private visibility will succeed but will not return any documents.

Submits a request to the Sanity API. Returns {:ok, response} upon success or {:error, response} if a non-exceptional (4xx) error occurs. A Sanity.Error will be raised if an exceptional error, such as a 5xx response code or a network timeout, occurs.

Like request/2, but raises a Sanity.Error instead of returning and error tuple.

Returns the result from a Sanity.Response struct.

Link to this section Functions

Specs

Generates a request for the Doc endpoint.

The Sanity docs suggest using this endpoint sparingly because it is "less scalable/performant" than using query/3.

Link to this function

mutate(mutations, query_params \\ [])

View Source

Specs

mutate([map()], keyword() | map()) :: Sanity.Request.t()

Generates a request for the Mutate endpoint.

example

Example

Sanity.mutate(
  [
    %{
      create: %{
        _type: "product",
        title: "Test product"
      }
    }
  ],
  return_ids: true
)
|> Sanity.request(config)
Link to this function

query(query, variables \\ %{}, query_params \\ [])

View Source

Specs

query(String.t(), keyword() | map(), keyword() | map()) :: Sanity.Request.t()

Generates a request to the Query endpoint. Requests to this endpoint may be authenticated or unauthenticated. Unauthenticated requests to a dataset with private visibility will succeed but will not return any documents.

Link to this function

request(request, opts \\ [])

View Source

Specs

request(
  Sanity.Request.t(),
  keyword()
) :: {:ok, Sanity.Response.t()} | {:error, Sanity.Response.t()}

Submits a request to the Sanity API. Returns {:ok, response} upon success or {:error, response} if a non-exceptional (4xx) error occurs. A Sanity.Error will be raised if an exceptional error, such as a 5xx response code or a network timeout, occurs.

options

Options

  • :api_version - The default value is "v2021-03-25".

  • :cdn - Should the CDN be used? See the Sanity docs for details. The default value is false.

  • :dataset - Sanity dataset.

  • :http_options - Options to be passed to Finch.request/3. The default value is [].

  • :project_id - Sanity project ID.

  • :token - Sanity auth token.

Link to this function

request!(request, opts \\ [])

View Source

Specs

Like request/2, but raises a Sanity.Error instead of returning and error tuple.

See request/2 for supported options.

Specs

result!(Sanity.Response.t()) :: any()

Returns the result from a Sanity.Response struct.

examples

Examples

iex> Sanity.result!(%Sanity.Response{body: %{"result" => []}}) []

iex> Sanity.result!(%Sanity.Response{body: %{}}) ** (Sanity.Error) %Sanity.Response{body: %{}, headers: nil}

Link to this function

upload_asset(body, opts \\ [], query_params \\ [])

View Source

Specs

upload_asset(iodata(), keyword() | map(), keyword() | map()) ::
  Sanity.Request.t()

Generates a request for the asset endpoint.

options

Options

  • :asset_type - Either :image or :file. The default value is :image.

  • :content_type - Optional content-type header. It appears that Sanity is able to infer image types.

query-params

Query params

Sanity doesn't document the query params very well at this time, but the Sanity Javascript client lists several possible query params:

  • label - Label
  • title - Title
  • description - Description
  • filename - Original filename
  • meta - ???
  • creditLine - The credit to person(s) and/or organization(s) required by the supplier of the image to be used when published