luckyframework/lucky

Create handler to mutate param key names

jwoertink opened this issue · 0 comments

One common problem I've run in to working in the front-end world is that everything in javascript land is camelcased, but everything on the Crystal side is snakecased. What if we had a handler that you could enable and it would just auto-convert all param keys from firstName to first_name without you needing to change anything in either language?

$fetch("POST", {user: {firstName: "", lastName: ""}})

post "/users" do
  params.nested(:user).get("first_name")
end

One major thing to consider is if your POST body is heavy (i.e. uploading a file), you don't want to parse the request body a bunch of times.

Or maybe the get method can just say "find a key by this or the snake cased version of this". That would probably throw things off if you wanted to pass back first_name and firstName because of some weird legacy DB thing, but then I guess you just wouldn't enable this option.

This wouldn't be something turned on by default. Just an extra handler that if you wanted, you would add to your middleware stack Lucky::ParamTransformerHandler.new