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

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. 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 Libraries Top

hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: https://github.com/tmbb/phoenix_ws Phoenix channels are a great...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
KallDrexx
For a good number of months I've been working on creating a very basic RTMP live video streaming server. Now that I have a very, very ba...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement