# `AsyncApiSpex.Plug.Viewer`
[🔗](https://github.com/jamescarr/ankusa/blob/async_api_spex-v0.2.0/packages/async_api_spex/lib/async_api_spex/plug/viewer.ex#L2)

A Plug that serves an HTML page rendering an AsyncAPI document in the
browser with the [AsyncAPI React component](https://github.com/asyncapi/asyncapi-react):
servers, channels, operations, messages, and schemas.

The page fetches the document from the `:path` it is given, so mount it
next to `AsyncApiSpex.Plug.RenderSpec`:

    # Plug.Router
    forward "/asyncapi.json", to: AsyncApiSpex.Plug.RenderSpec, init_opts: [spec: Shop.AsyncApi]
    forward "/asyncapi",      to: AsyncApiSpex.Plug.Viewer,     init_opts: [path: "/asyncapi.json"]

    # Phoenix router
    forward "/asyncapi.json", AsyncApiSpex.Plug.RenderSpec, spec: Shop.AsyncApi
    forward "/asyncapi",      AsyncApiSpex.Plug.Viewer,     path: "/asyncapi.json"

The browser resolves `:path` relative to the page, so give it the absolute
path the document is mounted at (including any `scope` prefix).

Options:

  * `:path` (required) — the URL the browser fetches the document from.
  * `:title` — the page's `<title>`; defaults to `"AsyncAPI"`.
  * `:bundle_url` — the component's standalone bundle; defaults to
    `@asyncapi/react-component` 3.2.1's `browser/standalone/index.js` on unpkg.
  * `:stylesheet_url` — the component's stylesheet; defaults to the same
    version's `styles/default.min.css` on unpkg.
  * `:config` — a map passed to the component as its `config` prop;
    defaults to `%{"show" => %{"sidebar" => true}}`.

The bundle and stylesheet are loaded from unpkg; point `:bundle_url` and
`:stylesheet_url` at a self-hosted copy to keep the page off a CDN. Under a
Content Security Policy the page also needs its one inline `<script>` (the
`render` call carrying `:path` and `:config`) allowed, with
`'unsafe-inline'` or that script's hash in `script-src`.

# `call`

```elixir
@spec call(Plug.Conn.t(), binary()) :: Plug.Conn.t()
```

Sends the page as a `200` `text/html` response and halts the connection.

# `init`

```elixir
@spec init(keyword()) :: binary()
```

Builds the HTML page.

Raises `ArgumentError` when the `:path` option is missing or an option is
unknown.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
