View Source Sanity (sanity v0.11.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.

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 can have either atom or string keys.

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

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.

Returns the result from a Sanity.Response struct.

Link to this section Functions

@spec doc(String.t()) :: Sanity.Request.t()

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
@spec 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
@spec 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
@spec 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 can have either atom or string keys.

examples

Examples

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

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"}, 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
@spec request!(
  Sanity.Request.t(),
  keyword()
) :: Sanity.Response.t()

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

See request/2 for supported options.

Link to this function

request(request, opts \\ [])

View Source
@spec 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.

@spec 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
@spec 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