itzmidinesh

itzmidinesh

Morpheus - Lightweight library to auto convert between snake_case and camelCase in Phoenix APIs

Hello everyone! I’m excited to share my first Hex package: Morpheus.

What is Morpheus?

Morpheus is a lightweight library designed to seamlessly convert between snake_case and camelCase in Phoenix projects. It’s particularly useful when your Phoenix API needs to communicate with frontend JavaScript code, where camelCase is the common convention.

This library was inspired by proper_case, which hasn’t been updated since May 2020. I decided to create a new library that:

  • Works with the latest Elixir (1.17)
  • Has minimal dependencies (only Jason)
  • Focuses on the most common use case: converting between snake_case and camelCase
  • Maintains a lightweight and simple API

Features

  • Convert map keys from snake_case to camelCase and vice versa
  • Handle nested maps and lists
  • Preserve atom and string key types
  • Seamless integration with Phoenix for automatic conversion of JSON responses

Quick Start

Add morpheus to your dependencies in mix.exs:

def deps do
  [
    {:morpheus, "~> 0.1.0"}
  ]
end

In your config/config.exs, add the following after Jason config:

config :phoenix, :format_encoders, json: Morpheus.Encoder

Now your Phoenix controllers will automatically convert snake_case keys to camelCase in JSON responses:

def show(conn, %{"id" => id}) do
 user = %{
   user_id: id,
   first_name: "John",
   last_name: "Doe",
   email_address: "john@example.com"
 }
 
 json(conn, user)  # Will be converted to camelCase automatically
end

For incoming requests, add the plug to your router:

pipeline :api do
 plug :accepts, ["json"]
 plug Morpheus.Plugs.SnakeCaseParams
end

Links

https://github.com/itzmidinesh/morpheus

This is my first contribution to the Elixir ecosystem. Feedback, suggestions, and contributions are welcome! Let me know if you find it useful or have any ideas for improvement.

Most Liked

kevinschweikert

kevinschweikert

There is also GitHub - wemake-services/recase: ♻ Convert strings to any case. · GitHub if you need more than snake and camel case

itzmidinesh

itzmidinesh

Thanks for sharing Recase! It’s a great library for general string case conversion. Morpheus, on the other hand, is specifically designed for Phoenix APIs, providing a plug for automatic conversion of incoming and outgoing parameters.

Where Next?

Popular in Announcing Top

alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10483 141
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement