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

👩‍💻 improved HydrogenSession typing. #1590

Merged
merged 3 commits into from
Jan 3, 2024
Merged

Conversation

michenly
Copy link
Contributor

@michenly michenly commented Dec 20, 2023

WHY are these changes introduced?

The current typing for HydrogenSession is de-coupled from the implementation of HydrogenSession in project. This means the shape of the class could be very different from what @shopify/hydrogen expect session to look like.

WHAT is this pull request doing?

Export the typing for HydrogenSession from @shopify/hydrogen and use it to implement HydrogenSession in all the example projects.

The type is implemented using base session type from @remix-run/server-runtime which should ensure all session type from remix are supported. (Addressing concern from #1430 (comment))

Having HydrogenSession implemented from a base type will ensure the interface of this class is what we expect it to be when we use property from this class in hydrogen utility.

HOW to test your changes?

Any CookieSession class that implements HydrogenSession type, changed the interface so it no longer conform to the interface and see type error in editor.

example 1: changed return type of commit method
Screenshot 2023-12-20 at 12 44 47 PM

example 2: remove get method
Screenshot 2023-12-20 at 12 45 03 PM

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

@michenly
Copy link
Contributor Author

Does anyone know if there are other documentation I need to update with this change?

Copy link
Contributor

@frandiox frandiox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest renaming the project session to CookieSession and keep HydrogenSession type as is:

import {
 // ...
 type HydrogenSession,
} from '@shopify/hydrogen';
// ...

        CookieSession.init(request, [env.SESSION_SECRET]),

// ...
export class CookieSession implements HydrogenSession {
// ...

It might require more changes but it feels like a better update 🤔

packages/hydrogen/src/hydrogen.d.ts Outdated Show resolved Hide resolved
michenly and others added 2 commits January 2, 2024 15:32
Co-authored-by: Fran Dios <frankdiox@gmail.com>
@@ -102,7 +103,7 @@ export default {
* Feel free to customize it to your needs, add helper methods, or
* swap out the cookie-based implementation with something else!
*/
export class HydrogenSession {
export class HydrogenSession implements DefaultHydrogenSession {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat nit-picky, but I think that DefaultHydrogenSession doesn't really make sense as an identifier name. It is the interface that the developer must conform to. So it isn't really a "default". It is what they need to implement to ensure compatibility. IMO export class Session implements HydrogenSession makes more sense. But that means a bit more to modify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see Fran above had a similar comment. Whether it is Session or CookieSession, I just don't like calling the type DefaultHydrogenSession in our examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will update the name of the Session in our template. Was avoiding making a bigger change but if everyone are on board I will make it happen :)

Copy link
Contributor

@blittle blittle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great change, thank you for pushing it!

…ession to be session typing coming from Hydrogen package
@michenly michenly merged commit cce6579 into main Jan 3, 2024
10 checks passed
@michenly michenly deleted the mc-improved-session-type branch January 3, 2024 21:21
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

Successfully merging this pull request may close these issues.

3 participants