Skip to content

ExLeonem/ApiCommons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ApiCommons

Coverage Status Build Status

Development of a REST API is tedious. Use your ecto schemes to generate endpoints quicker.

Mongoose-rest-api

Index

  1. Roadmap
  2. Installation
  3. Dependencies
  4. Examples
  5. Contribution

Roadmap

  • Provide functions to check parameters to endpoint

  • Provide functions to construct a return value for the endpoint

  • Auto generation of endpoints via DSL/macro usage

    • Common operations create, index, show, delete, update
    • Auto generate view functions
  • Auto-Generate OpenAPI file from DSL usage

  • Auto-Generate HTML API Documentation

  • Plug to sync OpenAPI definition and DSL definitions?

  • Adding option for configuration to map errors

      # Check wether parameter of a specific struct
      def takesStruct(struct = %StructTest{}) do
    

Installation

If available in Hex, the package can be installed by adding api_commons to your list of dependencies in mix.exs:

def deps do
  [
    {:api_commons, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/api_commons.

Dependencies

Examples

Function Description
&check/3 Check received parameter list for a single parameter
&like_schema/3 Check received parameters against ecto schema
defmodule AppWeb.CommentController do
  use AppWeb, :controller
  alias ApiCommons.Parameter

  def create(conn, params) do
    param_checks = conn
    |> Parameter.check(:user, type: :integer, position: :body)
    |> Parameter.check(:title, position: :body)
    |> Parameter.check(:content, position: :body)
    |> Parameter.check(:response_on, type: :integer, required?: false)

    # Render either error view or the entity
    if param_checks.valid? do
      render("comment.json", params: param_checks.parsed)
    else
      render("error.json", errors: param_checks.errors)
    end
  end
end

defmodule AppWeb.CommentView do
  use AppWeb, :view
  alias ApiCommons.Response

  def render("error.json", params) do
    # Render the error
  end

  def render("comment.json", params) do
    
  end
end
defmodule AppWeb.CommentController do
  use AppWeb, :controller

  alias AppRepo.Comment
  alias ApiCommons.Parameter

  def create(conn, params) do
    param_checks = params
    |> Parameter.like_json(Comment)

    # Render either error view or the entity
    if param_checks.valid? do
      render("comment.json", params: param_checks.parsed)
    else
      render("error.json", errors: param_checks.errors)
    end
  end
end

Contribution

About

More convenient REST API development in phoenix.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages