Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add req.assigns and resp.assigns #448

Open
wojtekmach opened this issue Jan 15, 2025 · 2 comments
Open

Proposal: Add req.assigns and resp.assigns #448

wojtekmach opened this issue Jan 15, 2025 · 2 comments

Comments

@wojtekmach
Copy link
Owner

I've heard from multiple people that using req.private and resp.private feels wrong because name private implies it is internal. The idea behind private was to follow Plug, https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields. I wonder if, similarly to Plug, we should add assigns and it clearly will be meant to be used by users and will be familiar to Plug/LV users. Before:

iex> Req.get!("https://reqbin.org/ndjson", into: fn {:data, _data}, {req, resp} ->
...>   {:cont, {req, Req.Response.update_private(resp, :count, 1, & &1 + 1)}}
...> end).private.count
3

after:

iex> Req.get!("https://reqbin.org/ndjson", into: fn {:data, _data}, {req, resp} ->
...>   {:cont, {req, update_in(resp.assigns[:count], &((&1 || 0) + 1))}}
...> end).private.count
3

which is of course not apples to apples comparison as the former uses a function call. That's the thing though, how should the function be called? If we'd have Req.Response.assign(resp, assigns) then naturally it could be called Req.Response.update_assign(resp, name, initial, fun). Another idea though is calling it just update, akin to Phoenix.Component.update/3:

iex> Req.get!("https://reqbin.org/ndjson", into: fn {:data, _data}, {req, resp} ->
...>   {:cont, {req, Req.update(resp, :count, 1, & &1 + 1)}}
...> end).private.count
3

The signature would be: Req.update(req_or_resp, key, initial, fun), that is, it works with both req.assigns and resp.assigns. I believe the biggest benefit of this is users of Req could stay within Req module and not care about underlying Req.Request and Req.Response modules. Req.Request and Req.Response private could still be used by step library authors. Or we deprecate it altogether in favor of assigns.

@yordis
Copy link

yordis commented Jan 15, 2025

  • assigns for users
  • private for Req package maintainer

There is an outgoing conversation in Tesla about that somewhere.

@wojtekmach
Copy link
Owner Author

Yeah, exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants