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

fix: Stripe types and checks #264

Merged
merged 4 commits into from
Jul 2, 2023
Merged

Conversation

nicu-chiciuc
Copy link
Contributor

@nicu-chiciuc nicu-chiciuc commented Jun 17, 2023

Fixes #261

Adding types

I started by adding

// @deno-types="npm:stripe"

just above the Stripe import.
This is based on https://deno.com/manual@v1.34.0/advanced/typescript/types#providing-types-when-importing

This is not the best solution, but currently there's an issue with having types for Stripe in Deno:
stripe-samples/stripe-node-deno-samples#2

Covering all possibilities

After the initial error was fixed, I was receiving a different error, also caused by type assertions, so I had to change

pricePerInterval={toPricePerInterval(
                product.default_price as Stripe.Price,
              )}

and handle all cases

Testing

  1. git clone ...
  2. deno task start
  3. access http://localhost:8000/pricing
  4. you shouldn't see a 500 error
Screenshot 2023-06-18 at 02 28 30

PS

defaultPrice.recurring?.interval already has a defined type, but I wasn't sure what's the correct way to import it in Deno. I left it as string since it's not a critical part of the app, it's used just for displaying text. So it's not a high-concern at the moment.

Screenshot 2023-06-18 at 02 29 37

@nicu-chiciuc
Copy link
Contributor Author

What should cd476cd accomplish?

Copy link
Contributor

@iuioiua iuioiua left a comment

Choose a reason for hiding this comment

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

My main thought is that the checks added in this PR aren't needed if the init:stripe task is run correctly. So having product.default_price as Stripe.Price is acceptable. If product.default_price is not of type Stripe.Price, we may have to investigate the init:stripe task, as it's not working correctly.

Either way, nice work fixing the Stripe types! I've added a suggestion to improve this situation for the whole codebase.

@@ -1,4 +1,7 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.

// Default types for Stripe don't yet work: https://github.com/stripe-samples/stripe-node-deno-samples/issues/2
// @deno-types="npm:stripe"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this to a stripe.ts file at the root of this codebase and point the stripe import in the import_map.json to that file? That way, all files that import stripe also get types.

Also, can you please add a version that ensures the types and runtime versions of stripe match?

@nicu-chiciuc
Copy link
Contributor Author

I see, in that case a better solution would've been for me to not make these checks but throw a custom error/banner instead of relying on type assertions. That means I could remove most of the code checks.

@iuioiua
Copy link
Contributor

iuioiua commented Jun 23, 2023

Yes, I'd rather the code threw 👍🏾

@nicu-chiciuc
Copy link
Contributor Author

I'm made the changes. Remove the type assertions and threw and error (had to create a type guard for this to work).

I still get an error on the pricing page, even after running deno task init:stripe since one of the elements in data doesn't have a default_price. But that's consistent with the previous implementation.

@nicu-chiciuc
Copy link
Contributor Author

I've also moved the stripe definitions into stripe.ts. I had to change import in stripe-webhooks.ts so it uses a default export like all the other files.

@nicu-chiciuc nicu-chiciuc requested a review from iuioiua July 1, 2023 14:24
@iuioiua iuioiua changed the title fix: remove type assertions, fix code fix: Stripe types and checks Jul 2, 2023
Copy link
Contributor

@iuioiua iuioiua left a comment

Choose a reason for hiding this comment

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

I made a few fixes, and now LGTM! Thanks for this!

Note for future: please use deno task ok before git push-ing to ensure all checks pass.

@iuioiua iuioiua merged commit 849df0f into denoland:main Jul 2, 2023
@nicu-chiciuc nicu-chiciuc deleted the nicu/fix-pricing branch July 2, 2023 15:17
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.

TypeError: Cannot read properties of null (reading 'unit_amount')
2 participants