Suggest renaming "getSession()" for clarity and better API design #1043
                  
                    
                      duongphuhiep
                    
                  
                
                  started this conversation in
                Ideas
              
            Replies: 1 comment
-
| 
         feel free to draft a PR for v2 branch to deprecate   | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently,
getSession()internally callsupdateSession()(sometimes), which is unexpected behavior. By convention, a function namedgetX()is assumed to retrieve data, not modify something else. This pattern can lead to confusion and indicates a potential design issue in the API.Suggested quick fix
getSession()touseSession()orgetOrInitSession()to reflect its side effects.useSession()touseSessionAccessor()orgetOrInitSessionAccessor().SessionAccessorprovides clear intent: it can both read and update session data.This naming confusion highlights that the current functions does too much without a clear separation of concerns.
Suggested long-term improvement:
Consider treating session data like any other data entity with explicit CRUD operations:
createSession(inputSessionData)– creates a new session in the cookies, using theinputSessionDataor the data of the headergetSession()– retrieves the session from header or cookiesupdateSession(inputSessionData)– updates session data in cookies, using theinputSessionDataor the data of the headerdeleteSession()– deletes session cookiesThis approach favors clarity and flexibility. Although it may require more boilerplate and might be slightly less performant than an all-in-one helper like
useSessionAccessor(), it empowers consumers to manage sessions in a more deliberate and extensible way.Beta Was this translation helpful? Give feedback.
All reactions