ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - malomohq/accent: Dynamically convert the case of your JSON API keys
Dynamically convert the case of your JSON API keys - malomohq/accent
Visit Site

GitHub - malomohq/accent: Dynamically convert the case of your JSON API keys

GitHub - malomohq/accent: Dynamically convert the case of your JSON API keys

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!

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory