ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - smoku/phoenix_api_docs: Phoenix API Docs
Phoenix API Docs. Contribute to smoku/phoenix_api_docs development by creating an account on GitHub.
Visit Site

GitHub - smoku/phoenix_api_docs: Phoenix API Docs

GitHub - smoku/phoenix_api_docs: Phoenix API Docs

Phoenix Api Docs

PhoenixApiDocs is a library written in the Elixir for the Phoenix framework. It lets you generate API documentation in the API Blueprint format from annotations in controllers and automated tests.

Installation

Add PhoenixApiDocs to your mix.exs dependencies:

defp deps do
  [{:phoenix_api_docs, "~> 0.1.0"}]
end

Run mix deps.get to fetch the dependencies:

$ mix deps.get

In your test/test_helper.exs start gen server PhoenixApiDocs.start for logging requests and configure ExUnit to use PhoenixApiDocs.Formatter:

PhoenixApiDocs.start
ExUnit.start(formatters: [ExUnit.CLIFormatter, PhoenixApiDocs.Formatter])

Usage

Add api_docs_info to your mix.exs:

def api_docs_info do
  [
    host: "https://api.acme.com",
    title: "ACME API",
    description: "API requires authorization. All requests must have valid `auth_token`"
  ]
end

Options:

  • host: API host.
  • title: Documentation title (can use Blueprint format).
  • description: Documentation description (can use Blueprint format).

Add PhoenixApiDocs.Controller to your phoenix controller and use api\3 macro to generate specification for the controller action:

defmodule App.CommentController do
  use App.Web, :controller
  use PhoenixApiDocs.Controller

  api :GET, "/posts/:post_id/comments" do
    group "Comment" # If not provided, it will be guessed from the controller name (resource name)
    title "List comments for specific docs"
    description "Optiona description that will be displayed in the documentation"
    note "Optional note that will be displayed in the documentation"
    parameter :post_id, :integer, :required, "Post ID or slug"
  end
  def index(conn, %{"post_id" => post_id}) do
    ...
  end

  api :PUT, "/posts/:post_id/comments" do
    title "Update comment"
    parameter :post_id, :integer, :required, "Post ID or slug"
  end
  def update(conn, %{"comment" => comment_params}) do
    ...
  end

end

API specification options:

  • method: HTTP method - GET, POST, PUT, PATCH, DELETE
  • url: URL route from `phoenix router``
  • group: Documentation routes are grouped by a group name (defaults to resource name guessed from the controller name)
  • title: Title (can use Blueprint format)
  • description: Description (optional, can use Blueprint format)
  • note: Note (optional, can use Blueprint format)
  • parameter: name, type, required/optional, description
    • required - parameter :post_id, :integer, :required, "Post ID"
    • optional - parameter :post_id, :integer, "Post ID"

In your tests select what requests and responses you want to include in the documentation by saving conn to PhoenixApiDocs.ConnLogger:

  test "list comments for post", %{conn: conn} do
    post = insert(:post)
    insert_list(5, :comment, post: post)

    conn = get(
      conn,
      comments_path(conn, :index, post)
    )

    assert json_response(conn, 200)

    PhoenixApiDocs.ConnLogger.save(conn)
  end

PhoenixApiDocs.ConnLogger.save can be also piped:

    conn = get(
      conn,
      comments_path(conn, :index, post)
    ) |> PhoenixApiDocs.ConnLogger.save
  end

After you run your tests, documentation in an API Blueprint format will be generate in a file api.apib

$ mix test

To generate the documentation in a HTML format use Aglio renderer

$ npm install aglio -g

$ mix phoenix.api_docs

Configuration

The configuration options can be setup in config.exs:

config :phoenix_api_docs,
  docs_path: "priv/static/docs",
  theme: "triple"

Config options:

  • docs_path: Specify the path where the documentation will be generated. If you want to serve the documentation directly from the phoenix you can specify priv/static/docs.
  • theme: HTML theme is generated using the Aglio renderer.

Common problems

Route is not generated after adding api annotation in the controller

Please make sure that the route you are using in the annotation matches exactly the route from the phoenix router (including params). Run mix phoenix.routes and compare the routes.

Tasks to do

  • raise error when route that is used in the annotation is not available in the phoenix router

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory