Sanity (sanity v0.3.0) View Source

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

Link to this section Summary

Functions

Generates a request for the Doc endpoint.

Convenience function for fetching a single document by ID. See doc/1.

Convenience function for fetching a list of documents by ID. See doc/1.

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.

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. See get_document/2 and get_documents/2 for a more convenient interface.

Link to this function

get_document(document_id, opts)

View Source

Specs

get_document(String.t(), keyword()) :: map() | nil

Convenience function for fetching a single document by ID. See doc/1.

See request/2 for supported options.

Link to this function

get_documents(document_ids, opts)

View Source

Specs

get_documents([String.t()], keyword()) :: [map()]

Convenience function for fetching a list of documents by ID. See doc/1.

The order/position of documents is preserved based on the original list of IDs. If any documents cannot be found then the returned list will contain nil for that document.

See request/2 for supported options.

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

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

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