-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
📝 [v3 Proposal]: Koa Style ctx.Request
and ctx.Response
Objects
#2854
Comments
Sorry to bring this up again, but I figured I'd try to close the loop on this because the previous discussion kind of fizzled out when the Koa idea was brought up. Gist with sample implementation |
Ping @gofiber/maintainers |
i like your proposal and gist, we can do it that way for all i care this way the old api's still work and it's better separated |
@efectn @gaby @sixcolors any ideas for this? otherwise someone can start with this or ? |
I am OK with the change. Btw we should return Req() and Resp() as interface. |
@efectn we could also use this for the ctx interface |
Sounds good idea |
Sounds good, to keep it short and neady I vote for .Req / .Res interface. It's still good to allow users to access the underlying fasthttp pointers. Maybe ctx.Fasthttp() is an option, but should all methods be exposed in this one? If so, some functions might break internals, can place it "use at own risk" kinds thing 😆 or we limit it to fasthttp's request / response pointers only. Feel free to comment |
I also vote for .Req / .Res interface. Your example looks good. I like |
Thanks for the feedback. I'll get started. Should the existing |
I lean toward removing redundant or similarly named functions that would make the framework confusing. Accessing the underlying fasthttp pointer should be very explicit so I favor the ctx.Fasthttp() approach. This would be breaking and I think v3 is a good opportunity to clean up and improve our approach. But, i think we really need to start a v2 => v3 migration guide. I recently did a migration of play billing lib for an Android app and think Google did a good job of their guide https://developer.android.com/google/play/billing/migrate-gpblv6 What does everyone else think? |
Feature Proposal Description
Currently, the Ctx object handles almost all of the work that is performed in a Handler function. This can potentially lead to confusion about what parts of the API interact with the request and what parts interact with the response (eg
Cookie
vsCookies
).Previously, we decided against eliminating the Ctx object entirely (#2764), but at the tail end of the discussion in Discord, it was brought up that we could avoid almost all breaking changes by following what Koa does: put the core implementation in Request and Response objects and have a generic Ctx API which provides convenient aliases for the request+response APIs.
In other words, the existing Ctx object stays exactly as-is, but ctx.Response() and ctx.Request() are reworked to hold more of the core functionality. As alluded to above, this allows users to more easily reason about what parts of the API touch the request vs response.
I am creating this proposal to attempt to take that idea and arrive at a concrete conclusion for whether we would like to implement this or not for v3 and beyond.
More reading: https://koajs.com/
Alignment with Express API
While inspired by Koa, the approach of splitting Ctx into distinct Request and Response objects brings Fiber closer to Express's res and req centered APIs.
HTTP RFC Standards Compliance
The proposal is not relevant to HTTP RFCs.
API Stability
This would be a breaking change, as it would change
Ctx.Request()
andCtx.Response()
to no longer directly return the underlying fasthttp Request and Response objects. Consequently, it will need to be implemented during a major version bump.The only way around this would be to make the new Request and Response objects compose the fasthttp objects, but that might prove to be a clumsy solution.
Having a clear separation between Request and Response APIs allows for a more idiomatic design. With the current Ctx-only design, we already have examples of collisions between request-based and response-based actions. For example,
Ctx.Get
andCtx.GetResponseHeader
.Feature Examples
Checklist:
The text was updated successfully, but these errors were encountered: