-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
getSession should only accept one type of request/context #2487
Comments
I haven't done the original implementation, but I am guessing it is for convenience only. If I did the implementation, I probably would have opted to support either passing req or the whole context directly. getSession(req)
getSession(ctx) This getSession({ctx}) looks confusing to me. I don't think it is actionable right now though, and since it's not a serious problem either, I'm inclined to leave it as is. Do you have a suggestion for improvement? |
Looking at the source code I figured it was for convenience. My first thought was pretty much the same route as your first suggestion, however I noticed there are 2 other options available in this object. See here As for suggestions on how to go about this, I think one of these two would be best: getSession({
req // Required
...optional options
}) Or getSession(req, { ...optional options }) Personally I prefer the latter for the same reason as you mentioned, and I think most people will never use the other options, so it's just unnecessary overhead. However I do think we should stick to only being allowed to pass the request object, as nothing else from the context is used. I am not sure if this has any actual real world advantages, but I personally find it to be a cleaner approach to only pass what is needed. These would be breaking changes but i'd be happy to hear what others think. |
Ultimately, I think the server-side |
I wasn't aware of this thread. Thanks for linking. I completely overlooked the client side getSession in my initial proposal. I will look into this and see if I can create a PR out of it. |
I will close this in favour of #1535 |
Question 💬
Currently, you are able to pass in the request object to
getSession
by passing in the full context, or simply an object containing thecontext.req
object. I don't understand why this is, especially as this makes the TS definitions less strict.It is required to pass in the request object, but the current type defs do not reflect that.
Is there a reason why it is allowed to do it both ways rather than just forcing the consumer to pass in the req object as a required param?
How to reproduce ☕️
Use getSession in a TS environment and you'll see that it has no required params, even though it should at least require the
req
object.Repro sandbox: https://codesandbox.io/s/sharp-sun-3lxoc?file=/pages/api/examples/session.ts
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
The text was updated successfully, but these errors were encountered: