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

Minor template updates and fixes #1341

Merged
merged 6 commits into from
Sep 19, 2023
Merged

Minor template updates and fixes #1341

merged 6 commits into from
Sep 19, 2023

Conversation

frandiox
Copy link
Contributor

While working on #1334 I noticed our context.session object didn't have proper types.
This PR adds types and fixes issues related to this.

@frandiox frandiox requested a review from a team September 14, 2023 02:02
Comment on lines -13 to +18
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;
Copy link
Contributor Author

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.#...).

Comment on lines -37 to 39
has(key: string) {
return this.session.has(key);
get has() {
return this.#session.has;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 key: string (i.e. string is more generic that what the session expects).

Open to ideas for a simpler syntax that keeps the types right.

const isAuthenticated = Boolean(customerAccessToken);
const isAuthenticated = !!customerAccessToken;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Boolean constructor here does not inform TS that customerAccessToken is defined or not after asserting isAuthenticated. !! does the trick (TIL).

const waitUntil = (p: Promise<any>) => executionContext.waitUntil(p);
const waitUntil = executionContext.waitUntil.bind(executionContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 waitUntil anymore

@@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

"moduleResolution": "node",
"moduleResolution": "Bundler",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new resolution mode is supposed to be appropriate for ESBuild/etc.

Copy link
Contributor

@juanpprieto juanpprieto left a comment

Choose a reason for hiding this comment

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

❤️

@frandiox frandiox merged commit eb5dd4d into main Sep 19, 2023
@frandiox frandiox deleted the fd-minor-skeleton-updates branch September 19, 2023 00:13
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.

2 participants