Macaw.Component.Proxy (macaw_component v0.13.0)

Copy Markdown View Source

Proxy routes (spec/01 §1.8): media a component needs from another origin, fetched by the host's one same-origin proxy, so a page holds same-origin URLs only.

iex> route = %{name: "tiles", url: "https://tiles.example/{z}/{x}/{y}.png", types: ["image/png"]}
iex> {:ok, digest} = Macaw.Component.Proxy.digest(route)
iex> Macaw.Component.Proxy.path("geo.map", route, %{"z" => "1", "x" => "0", "y" => "0"}) ==
...>   {:ok, "/macaw/proxy/geo.map/tiles/#{digest}?x=0&y=0&z=1"}
true

Summary

Functions

A route's digest (spec/01 §1.8): the first 16 hex digits of Macaw.Base.Encoding.digest/1 of {url, types}, so two routes of one name with different templates or types have different digests. Types that are not data are {:error, faults}, as Encoding.digest/1 answers them.

The same-origin path a render uses for a route of a component: /macaw/proxy/<component>/<route>/<digest>?<values>, the route's digest/1, the values query-encoded in name order. Every placeholder needs a value: {:error, {:placeholders, missing}} otherwise; a route whose types aren't data is {:error, {:bad_route, faults}}.

The placeholders a route's URL template names, in order of first appearance.

Types

route()

@type route() :: %{name: String.t(), url: String.t(), types: [String.t(), ...]}

Functions

digest(map)

@spec digest(route()) :: {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}

A route's digest (spec/01 §1.8): the first 16 hex digits of Macaw.Base.Encoding.digest/1 of {url, types}, so two routes of one name with different templates or types have different digests. Types that are not data are {:error, faults}, as Encoding.digest/1 answers them.

iex> Macaw.Component.Proxy.digest(%{name: "t", url: "https://a.example/{z}", types: ["image/png"]})
...> |> elem(1) |> String.length()
16

path(component, route, values)

@spec path(String.t(), route(), %{required(String.t()) => String.t()}) ::
  {:ok, String.t()}
  | {:error,
     {:placeholders, [String.t(), ...]} | {:bad_route, [Macaw.Base.Fault.t()]}}

The same-origin path a render uses for a route of a component: /macaw/proxy/<component>/<route>/<digest>?<values>, the route's digest/1, the values query-encoded in name order. Every placeholder needs a value: {:error, {:placeholders, missing}} otherwise; a route whose types aren't data is {:error, {:bad_route, faults}}.

placeholders(map)

@spec placeholders(route()) :: [String.t()]

The placeholders a route's URL template names, in order of first appearance.