-
-
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
[Proposal v3] 🔥feat!: add Req and Res objects to mirror Express #2764
Conversation
Add a minimal Res() API as an incremental experimental step toward the idiomatic Express req/res API. This first change adds support for the following portions of the res API: * res.app * res.append * res.get * res.set The underlying fasthttp struct can be accessed using res.FastHTTP(). See https://expressjs.com/en/api.html#res
prefer option 2 right now as we have to accommodate functionalities that do not belong in either request or response |
yes, next would need to be added to the function signature to mirror Express.
res and req both hold references to the underlying app https://expressjs.com/en/api.html#req.app
req has a reference to the route object https://expressjs.com/en/api.html#req.route
yes, there are some features that we'd need to make sure are still accessible, for example by moving to req/res or making the context accessible from req/res.
I agree, this is probably the biggest downside in my opinion and is the primary reason I'm not strongly advocating for option 1. In fact, since option 2 is really a subset of option 1, it's probably prudent to limit this PR to option 2 anyway (if the idea is approved at all). |
Hi, If you will take second options it will be looks like koa: Just for info can you take a look how it was implemented in embed bun server: |
I also like option 1, but we need good solutions for the mentioned methods |
I think |
Ok, I can already tell this PR is going to get way too big to review very quickly. |
Discord conversationCtx vs Req & Res API Change Discussionnick#2764
|
Please forgive the after-the-fact comment, but I'd like to add another (minor) argument that I don't see mentioned yet, should this come up again in the future. A common pattern for implementing HTTP endpoints is to create request and response body types within the function body. Typically these types, and variables based on the types, are named Of course, users can name their variables something else -- but "naming things" is extra cognitive overhead. |
Thank you for the very thorough discussion! I do think it's worth thinking about separating or clarifying the request-specific vs response-specific APIs as the ambiguity has real effects on usability. |
Description
DRAFT/Proposal:
Align with Express's idiomatic req/res APIs (v4) by adding the req and res objects. The intent is to make the translation of Express's APIs to Fiber's APIs more natural by separating request and response based methods by design. Not only will this make it easier to move from Express to Fiber, but it will also make it easier to internally evaluate API alignment.
The core change that must be made to satisfy the goal of this PR is to add Req and Res structs/interfaces that are accessible in handles and middlewares.
This is a very early incremental step to get feedback on what direction we should take with this, and more importantly, whether we want to do this at all.
Possible Approaches
There are two main approaches that could be taken:
1. Keep Ctx as a concept, but modify handler and middleware signatures to take a Res and Req parameter instead of a Ctx.
Example:
2. Keep Ctx as the parameter for handlers and middlewares and convert the Response() and Request() methods to return Res and Req objects in which the Express res and req APIs are implemented.
c.Res()
vsres
)Example:
In my own opinion, I think function signatures that match what Express has is a big deal for making Fiber intuitive for that specific audience, so I lean toward option 1.
Important Considerations
TODO
Changes Introduced
Type of Change
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/
directory for Fiber's documentation.