ApiCommons
This library aims to ease the development of REST API's, by providing functions to parse Parameters and generate outputs.
Ease creation of REST APIs. This library provides functions to
- Parse received parameters
- Generation
Parse received parameters against ecto schemas or manual parameter definitions. Automatically generate error responses from errors occured while parsing.
Index
Roadmap
[ ] Functions to check parameters manually received at endpoint
[ ] Function to check received parameters against ecto.schema
[ ] Auto-Generation of error responses
[ ] Auto generation of endpoints via DSL/macro usage
- [ ] Common operations create, index, show, delete, update
- [ ] Auto generate view functions
[ ] Auto-Generate OpenAPI spec and documentation (from DSL/Function definitions (Compile time inspection of functions without macro?))
[ ] Auto-Generate HTML API Documentation
[ ] Map Plug.Conn specific errors to REST error responses (405, ...)
[ ] Optionally generate Hateoas links
[ ] Mix tasks to generate endpoints?
# 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"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/api_commons.
Dependencies
Parameter check
| 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