All Projects → malomohq → accent

malomohq / accent

Licence: MIT License
Dynamically convert the case of your JSON API keys

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to accent

Terraform
A simple plug for incrementally transforming an API into Phoenix. Check out the blog post:
Stars: ✭ 379 (+1303.7%)
Mutual labels:  phoenix, plug
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (+170.37%)
Mutual labels:  phoenix, plug
Guardian
Elixir Authentication
Stars: ✭ 3,150 (+11566.67%)
Mutual labels:  phoenix, plug
plug rails cookie session store
Rails compatible Plug session store
Stars: ✭ 93 (+244.44%)
Mutual labels:  phoenix, plug
alternate
Plug and Phoenix helpers to localize your web app via the URL
Stars: ✭ 26 (-3.7%)
Mutual labels:  phoenix, plug
Logster
Easily parsable single line, plain text and JSON logger for Plug and Phoenix applications
Stars: ✭ 171 (+533.33%)
Mutual labels:  phoenix, plug
Liberator
An Elixir library for building RESTful applications.
Stars: ✭ 28 (+3.7%)
Mutual labels:  phoenix, plug
plug rest
REST behaviour and Plug router for hypermedia web applications in Elixir
Stars: ✭ 52 (+92.59%)
Mutual labels:  phoenix, plug
Absinthe plug
Plug support for Absinthe, the GraphQL toolkit for Elixir
Stars: ✭ 209 (+674.07%)
Mutual labels:  phoenix, plug
Appsignal Elixir
🟪 AppSignal for Elixir package
Stars: ✭ 176 (+551.85%)
Mutual labels:  phoenix, plug
phoenix-client-ssl
Set of Plugs / Lib to help with SSL Client Auth.
Stars: ✭ 18 (-33.33%)
Mutual labels:  phoenix, plug
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (-7.41%)
Mutual labels:  phoenix, plug
mishka-cms
MishkaCms an open source and real time API base CMS Powered by Elixir and Phoenix
Stars: ✭ 37 (+37.04%)
Mutual labels:  phoenix
opensubs.io
💸 Track recurring bills and subscriptions
Stars: ✭ 37 (+37.04%)
Mutual labels:  phoenix
sheriff
Build simple and robust authorization systems with just Elixir and Plug
Stars: ✭ 39 (+44.44%)
Mutual labels:  plug
phoenix live controller
Controller-style abstraction for building multi-action live views on top of Phoenix.LiveView
Stars: ✭ 31 (+14.81%)
Mutual labels:  phoenix
BearNecessities
Multiplayer bear game with Phoenix Live View
Stars: ✭ 19 (-29.63%)
Mutual labels:  phoenix
ecto generator
Generate Ecto schemas from existing database in Phoenix - Elixir
Stars: ✭ 20 (-25.93%)
Mutual labels:  phoenix
mindwendel
Create a challenge. Ready? Brainstorm. mindwendel helps you to easily brainstorm and upvote ideas and thoughts within your team.
Stars: ✭ 22 (-18.52%)
Mutual labels:  phoenix
indicado
Technical indicator library for Elixir with no dependencies.
Stars: ✭ 15 (-44.44%)
Mutual labels:  phoenix

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:

  • Accent.Case.Camel (e.g. camelCase)
  • Accent.Case.Pascal (e.g. PascalCase)
  • Accent.Case.Snake (e.g. snake_case)

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

  • :default_case - module used to case the response when the client does not request a case or requests an unsupported case. When not provided then no conversation will happen for the above scenarios. Defaults to nil.
  • :header - the HTTP header used to determine the case to convert the response body to before sending the response (default: Accent)
  • :json_codec - module used to encode and decode JSON. The module is expected to define decode/1 and encode!/1 functions (required).
  • :supported_cases - map that defines what cases a client can request. By default camel, pascal and snake are supported.

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].