ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - sheharyarn/better_params: Cleaner request parameters in Elixir web applications 🙌
Cleaner request parameters in Elixir web applications 🙌 - sheharyarn/better_params
Visit Site

GitHub - sheharyarn/better_params: Cleaner request parameters in Elixir web applications 🙌

GitHub - sheharyarn/better_params: Cleaner request parameters in Elixir web applications 🙌

BetterParams

Build Status Coverage Status Version License

Cleaner request parameters in Elixir web applications 🙌

BetterParams is a simple Elixir Plug that allows passed request parameters to be called as atoms instead of Strings. The sole purpose of this Plug is to pattern match on maps with atom keys instead of string keys in routers/controllers to calm my OCD down.

Usage

Once installed, it lets you pattern match request parameters like %{id: id} instead of %{"id" => id} in Phoenix applications:

# web/controllers/some_controller.ex

def show(conn, %{id: id}) do
  # do something
end

def create(conn, %{id: id, post: %{title: title, body: body}}) do
  # do something
end

Notes

  • Implementation uses String.to_existing_atom to prevent against DoS attacks, so it only converts those params to atoms that you use in your application.
  • You can continue to use String keys without breaking your existing matches if you want. All request parameters are available for both String and Atom keys (that have already been defined in the application).
  • This doesn't pollute your Request Logs with duplicate params.
  • For other Plug.Router based applications, you can also access request params similarly by calling them like conn.params[:id] or conn.params.post.title.

Installation

Add better_params to your project dependencies in mix.exs:

def deps do
  [{:better_params, "~> 0.5.0"}]
end

Phoenix Framework

For Phoenix applications, call the plug at the end of the controller method in web/web.ex (inside the quote block):

# web/web.ex

def controller do
  quote do
    use Phoenix.Controller

    # Other stuff...

    plug BetterParams
  end
end

Alternatively, you can also call it your Router Pipelines or in individual controllers directly.

Other Plug.Router Apps

For other applications using Plug.Router, call the Plug anytime after calling the :match and :dispatch plugs:

defmodule MyApp.Router do
  use Plug.Router

  plug :match
  plug :dispatch
  plug BetterParams

  # Rest of the router...
end

Removing String Keys Entirely

If your use case calls for a params object with only Atom keys, you may pass the option drop_string_keys to the plug. Much as it says on the can, this will replace the String-type keys altogether, rather than preserving them alongside the Atom keys.

plug BetterParams, drop_string_keys: true

Roadmap

  • Write Tests
  • Write Documentation
  • Symbolize the collective params map
  • Option to remove string keys entirely
  • Symbolize individual parameter maps (if the need arises)
    • path_params
    • body_params
    • query_params

Contributing

  • Fork, Enhance, Send PR
  • Lock issues with any bugs or feature requests
  • Implement something from Roadmap
  • Spread the word :heart:

License

This package is available as open source under the terms of the MIT License.

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