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.
Generates a request for the asset endpoint.
Link to this section Functions
Specs
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
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
.
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)
Specs
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.
Specs
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}]
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 isfalse
.:dataset
- Sanity dataset.:http_options
- Options to be passed toFinch.request/3
. The default value is[receive_timeout: 30000]
.:project_id
- Sanity project ID.:token
- Sanity auth token.
Specs
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.
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}
Specs
Generates a request for the asset endpoint.
options
Options
:asset_type
- Either:image
or:file
. The default value is:image
.:content_type
- Optionalcontent-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
- Labeltitle
- Titledescription
- Descriptionfilename
- Original filenamemeta
- ???creditLine
- The credit to person(s) and/or organization(s) required by the supplier of the image to be used when published