defmodule Redoc.Plug.RedocUI do @moduledoc """ A Plug for rendering Redoc UI. ## Usage If you're using Phoenix Framework, add Redoc UI by add this plug to the router: ```elixir scope "/api" do ... get "/redoc", Redoc.Plug.RedocUI, spec: "/spec/openapi" end ``` ## Options * `spec_url` - The openapi path to fetch. Support both `yaml` and `json`. * `redoc_version` - Specify a Redoc version, default to `latest`. """ @behaviour Plug import Plug.Conn @index_html """ ReDoc """ @supported_opts [ :spec_url, :redoc_version ] @impl true def init(opts) do # TODO: validate mandatory fields. Keyword.take(opts, @supported_opts) |> Keyword.validate!([:spec_url, redoc_version: "latest"]) end @impl true def call(conn, opts) do conn |> send_resp(200, EEx.eval_string(@index_html, opts)) end end