Home

Awesome

Accent

Installation

This package can be installed by adding accent to your list of dependencies in mix.exs:

def deps do
  [{:accent, "~> 1.1"}]
end

Please note that you will need to provide accent with a JSON codec. Both poison and jason are supported.

Usage

This project provides two plugs for handling the conversion of JSON keys to different cases: Accent.Plug.Request and Accent.Plug.Response

Accent.Plug.Request

Transforms the keys of an HTTP request's params to the same or a different case.

You can specify what case to convert keys to by passing in a :transformer option.

Accent supports the following transformers out of the box:

If no transformer is provided then Accent.Case.Snake will be used.

Please note that this plug will need to be executed after the request has been parsed.

Example

Given this request:

curl -X POST https://yourapi.com/endpoints \
  -H "Content-Type: application/json" \
  -d '{"hello": "Accent"}'

a router with this configuration:

plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json],
                   pass: ["*/*"],
                   json_decoder: Jason

plug Accent.Plug.Request

could expect to receive a conn.params value of:

%{"hello" => "Accent"}

Accent.Plug.Response

Transforms the keys of an HTTP response to the case requested by the client.

A client can request what case the keys are formatted in by passing the case as a header in the request. By default the header key is Accent. If the client does not request a case or requests an unsupported case then a default case defined by :default_case will be used. If no default case is provided then no conversion will happen. By default the supported cases are camel, pascal and snake.

Options

Example

plug Accent.Plug.Response, default_case: Accent.Case.Snake,
                           header: "x-accent",
                           supported_cases: %{"pascal" => Accent.Case.Pascal},
                           json_codec: Jason

Given this request:

curl -X POST https://yourapi.com/endpoints \
  -H "Content-Type: application/json" \
  -H "Accent: camel" \
  -d '{"hello": "Accent"}'

with this router:

defmodule MyAPI.Router do
  use Plug.Router

  plug Accent.Plug.Response, json_codec: Jason

  post "/" do
    send_resp(conn, 200, Jason.encode!(%{hello_back: "Anthony"}))
  end
end

a client could expect a JSON response of:

{
  "helloBack": "Anthony"
}

Contributors

A special thanks to all of our contributors!