This repository was archived by the owner on Nov 7, 2023. It is now read-only.
forked from open-api-spex/open_api_spex
-
Notifications
You must be signed in to change notification settings - Fork 1
Adds RedocUI Plug #15
Open
tapickell
wants to merge
1
commit into
master
Choose a base branch
from
Add-Redoc-Plug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| defmodule OpenApiSpex.Plug.RedocUI do | ||
| @moduledoc """ | ||
| Module plug that serves RedocUI. | ||
| """ | ||
|
|
||
| @behaviour Plug | ||
|
|
||
| @html """ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>ReDoc</title> | ||
| <!-- needed for adaptive design --> | ||
| <meta charset="utf-8"/> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet"> | ||
|
|
||
| <!-- | ||
| ReDoc doesn't change outer page styles | ||
| --> | ||
| <style> | ||
| body { | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| window.onload = function() { | ||
| const apiSpecUrl = new URL(window.location); | ||
| pathname = "<%= path %>"; | ||
| function specUrl() { return apiSpecUrl + pathname; }; | ||
| </script> | ||
| <h1>SpecUrl: <script>specUrl();</script></h1> | ||
| <redoc spec-url='http://petstore.swagger.io/v2/swagger.json'></redoc> | ||
| <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script> | ||
| </body> | ||
| </html> | ||
| """ | ||
|
|
||
| @impl Plug | ||
| def init(path: path), do: %{path: path} | ||
|
|
||
| @impl Plug | ||
| def call(conn, %{path: path}) do | ||
| csrf_token = Plug.CSRFProtection.get_csrf_token() | ||
| html = render(path, csrf_token) | ||
|
|
||
| conn | ||
| |> Plug.Conn.put_resp_content_type("text/html") | ||
| |> Plug.Conn.send_resp(200, html) | ||
| end | ||
|
|
||
| require EEx | ||
| EEx.function_from_string(:defp, :render, @html, [:path, :csrf_token]) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| defmodule OpenApiSpec.Plug.RedocUITest do | ||
| use ExUnit.Case | ||
|
|
||
| alias OpenApiSpex.Plug.RedocUI | ||
|
|
||
| @opts RedocUI.init(path: "/ui") | ||
|
|
||
| test "renders csrf token" do | ||
| # token = Plug.CSRFProtection.get_csrf_token() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the plan for this test? |
||
|
|
||
| # conn = Plug.Test.conn(:get, "/ui") | ||
| # conn = RedocUI.call(conn, @opts) | ||
| # assert conn.resp_body =~ ~r[pathname.+?/ui] | ||
| # assert String.contains?(conn.resp_body, token) | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add code examples on how to use this? See
OpenApiSpex.Plug.SwaggerUIfor an example.