-
Notifications
You must be signed in to change notification settings - Fork 284
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
Minor template updates and fixes #1341
Changes from all commits
0b24e43
d6e04a0
03dda3e
16e1f4c
8ceffb2
8d412c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'demo-store': patch | ||
'@shopify/create-hydrogen': patch | ||
--- | ||
|
||
Improved types of `HydrogenSession` when accessing `session.get('customerAccessToken')`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,12 @@ import { | |
* swap out the cookie-based implementation with something else! | ||
*/ | ||
export class HydrogenSession { | ||
constructor( | ||
private sessionStorage: SessionStorage, | ||
private session: Session, | ||
) { | ||
this.session = session; | ||
this.sessionStorage = sessionStorage; | ||
#sessionStorage; | ||
#session; | ||
|
||
constructor(sessionStorage: SessionStorage, session: Session) { | ||
this.#sessionStorage = sessionStorage; | ||
this.#session = session; | ||
} | ||
|
||
static async init(request: Request, secrets: string[]) { | ||
|
@@ -34,31 +34,31 @@ export class HydrogenSession { | |
return new this(storage, session); | ||
} | ||
|
||
has(key: string) { | ||
return this.session.has(key); | ||
get has() { | ||
return this.#session.has; | ||
} | ||
Comment on lines
-37
to
39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By turning method wrappers into getters like this, we delegate the type to the actual session without losing information due to Open to ideas for a simpler syntax that keeps the types right. |
||
|
||
get(key: string) { | ||
return this.session.get(key); | ||
get get() { | ||
return this.#session.get; | ||
} | ||
|
||
destroy() { | ||
return this.sessionStorage.destroySession(this.session); | ||
get flash() { | ||
return this.#session.flash; | ||
} | ||
|
||
flash(key: string, value: any) { | ||
this.session.flash(key, value); | ||
get unset() { | ||
return this.#session.unset; | ||
} | ||
|
||
unset(key: string) { | ||
this.session.unset(key); | ||
get set() { | ||
return this.#session.set; | ||
} | ||
|
||
set(key: string, value: any) { | ||
this.session.set(key, value); | ||
destroy() { | ||
return this.#sessionStorage.destroySession(this.#session); | ||
} | ||
|
||
commit() { | ||
return this.sessionStorage.commitSession(this.session); | ||
return this.#sessionStorage.commitSession(this.#session); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ export async function loader({request, context, params}: LoaderArgs) { | |
const {pathname} = new URL(request.url); | ||
const locale = params.locale; | ||
const customerAccessToken = await context.session.get('customerAccessToken'); | ||
const isAuthenticated = Boolean(customerAccessToken); | ||
const isAuthenticated = !!customerAccessToken; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Boolean constructor here does not inform TS that |
||
const loginPath = locale ? `/${locale}/account/login` : '/account/login'; | ||
const isAccountPage = /^\/account\/?$/.test(pathname); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ export default { | |
throw new Error('SESSION_SECRET environment variable is not set'); | ||
} | ||
|
||
const waitUntil = (p: Promise<any>) => executionContext.waitUntil(p); | ||
const waitUntil = executionContext.waitUntil.bind(executionContext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes it easier to infer the type when using JSDocs because we are not wrapping |
||
const [cache, session] = await Promise.all([ | ||
caches.open('hydrogen'), | ||
HydrogenSession.init(request, [env.SESSION_SECRET]), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"moduleResolution": "node", | ||
"moduleResolution": "Bundler", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new resolution mode is supposed to be appropriate for ESBuild/etc. |
||
"resolveJsonModule": true, | ||
"module": "ES2022", | ||
"target": "ES2022", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ export async function action({request, context}: ActionArgs) { | |
case CartForm.ACTIONS.BuyerIdentityUpdate: { | ||
result = await cart.updateBuyerIdentity({ | ||
...inputs.buyerIdentity, | ||
customerAccessToken, | ||
customerAccessToken: customerAccessToken?.accessToken, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was an actual bug now caught by the enhanced types. |
||
}); | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was working fine but TS parameter properties is a bit obscure. I think using assignments is easier to understand.
Plus, this can now rely on the private members of native JS (
this.#...
).