View Source Sanity (sanity v0.10.0)

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

Link to this section Summary

Functions

Deeply traverses nested maps and lists and converts string keys to atoms in underscore_case.

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.

Replaces Sanity references with the referenced document. The input can be a single document or list of documents. References can be deeply nested within the documents. Documents should have atomized keys. See atomize_and_underscore/1.

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

Link to this function

atomize_and_underscore(map)

View Source

Specs

atomize_and_underscore(any()) :: any()

Deeply traverses nested maps and lists and converts string keys to atoms in underscore_case.

Untrusted user data should not be passed to this function. See warning in String.to_atom/1 for details.

examples

Examples

iex> Sanity.atomize_and_underscore(%{"_id" => "123", "myField" => [%{"aB" => "aB"}]})
%{_id: "123", my_field: [%{a_b: "aB"}]}

iex> Sanity.atomize_and_underscore([%{"AbcDef" => 1}])
[%{abc_def: 1}]

iex> Sanity.atomize_and_underscore(%{"already_underscore" => 1})
%{already_underscore: 1}

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.

examples

Examples

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

replace_references(doc_or_docs, func)

View Source

Specs

replace_references(list() | map(), (... -> any())) :: list() | map()

Replaces Sanity references with the referenced document. The input can be a single document or list of documents. References can be deeply nested within the documents. Documents should have atomized keys. See atomize_and_underscore/1.

examples

Examples

iex> Sanity.replace_references(%{_ref: "abc", _type: "reference"}, fn "abc" -> %{_id: "abc"} end)
%{_id: "abc"}

iex> Sanity.replace_references(%{_ref: "abc"}, fn "abc" -> %{_id: "abc"} end)
%{_id: "abc"}

iex> Sanity.replace_references([%{_ref: "abc", _type: "reference"}], fn _ -> %{_id: "abc"} end)
[%{_id: "abc"}]

iex> Sanity.replace_references([%{a: %{_ref: "abc", _type: "reference"}, b: 1}], fn _ -> %{_id: "abc"} end)
[%{a: %{_id: "abc"}, b: 1}]
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 [receive_timeout: 30000].

  • :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