Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joshnuss/transponder
https://github.com/joshnuss/transponder
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/joshnuss/transponder
- Owner: joshnuss
- Created: 2020-09-30T00:00:42.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-18T04:04:26.000Z (over 2 years ago)
- Last Synced: 2024-09-16T18:57:40.291Z (about 2 months ago)
- Language: Elixir
- Size: 63.5 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Transponder
DSL for declarative Phoenix controllers.
## Features
- Turns controllers into declarative code.
- Works with all context functions that return a tagged tuple like `{:ok, ...}` or `{:error, ...}`
- Supports rendering `Ecto.Changeset` errors.
- Built-in formatters for JSON and HTML with ability to add custom ones.**Disclaimer**: This is intended for simple apps or when starting to build an application. More complex apps may require more complexity in their controllers, and then a standard Phoenix controller with tests would work better. This is just a starting point.
## Installation
Add `transponder` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:transponder, "~> 0.2"}
]
end
```Add `use Transponder` to your controllers, and define actions with `defaction`:
```elixir
defmodule MyAppWeb.Admin.ProductsController do
use MyAppWeb, :controller
use Transponder, format: Transponder.JSONdefaction :index, &Catalog.list_products(&1.params)
defaction :show, &Catalog.get_product(&1.params)
defaction :create, &Catalog.create_product(&1.params["product"])
defaction :update, &Catalog.update_product(&1.params["id"], &1.params["product"])
defaction :delete, &Catalog.delete_product(&1.params["id"])
end
```Then define a `show.json.eex` and `index.json.eex`. Or you can use a `Phoenix.View`:
```elixir
defmodule MyAppWeb.Admin.ProductsView do
use MyAppWeb, :viewdef render("index.json", %{records: products}) do
%{products: Enum.map(&render("show.json", %{record: &1}))}
enddef render("show.json", %{record: product}) do
%{
id: product.id,
title: product.title,
price: product.price,
}
end
end
```To handle errors, configure the error view in `config/config.exs`:
```elixir
config :transponder, :error_view, MyAppWeb.ErrorView
```To render HTML instead of JSON use `Transponder.HTML`:
```elixir
defmodule MayAppWeb.Admin.ProductController do
use MyAppWeb, :controller
use Transponder, format: Transponder.HTML# defaction ...
end
```And then you'll need to create templates for the actions you use: `index.html.eex`, `show.html.eex`, etc..
You can also create a custom formatter:
```elixir
defmodule MyFormat do
@behaviour Transponder.Formatter# pattern match and render how you like
@impl true
def format(_action, conn, {:ok, bla}) do
conn
|> put_status(200)
|> render("yay.html")
enddef format(_action, conn, {:error, bla}) do
conn
|> put_status(401)
|> render("oops.html")
end
end
```## Documentation:
[https://hexdocs.pm/transponder](https://hexdocs.pm/transponder).
## License
MIT