-
Notifications
You must be signed in to change notification settings - Fork 751
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
TypeScript definitions #736
Conversation
First of all, this is awesome 🙌 I've given it a quick go in our codebase, and with strict null checks enabled we immediately got a lot of type errors due to the The same goes for certain properties on objects, such as Other issues I came across in my brief testing this morning:
EDIT: Found the part about importing using |
Awesome, thanks @bruun , this is super helpful. Appreciate all the specifics very much, will look into all of those shortly. Updated the docs to mention the |
Thanks so much again for the feedback @bruun – all your comments have been addressed. Care to give it another shot? |
No problem @rattrayalex-stripe :) The change to optional fields helped a lot, thanks! One last thing that I can think of at the moment, it would be great if definitions like this:
Could be limited to the actual possible values
|
Terrific, thanks @bruun ! How much work was it to move your project over from DT? Did you need to change a lot of things or just install & it worked? Yes, we have a project underway to find all of those strings and convert them over to enums! Curious for your thoughts on our reliance on the |
Moving over from DT was pretty painless, and mostly consisted of renaming The hard part, and something I haven't managed to solve yet due to time constraints, is making our codebase work with The reliance on the If we do not explicitly The above error disappears if we explicitly import Stripe in that module. There might be something very off in our module resolution which might also explain the |
Hi @bruun, Thanks, that's great to hear! We love catching bugs. We definitely don't want users to have to make runtime changes to adopt our types. I changed the export scheme so Regarding the errors, yes, you do need to import Stripe (or use the instantiated With |
@rattrayalex-stripe Removing the The following does not give a type error, but fails at runtime:
The following works at runtime, but yields a Typescript error (version 3.6.4):
Again, this could be a issue with our configuration or even my local setup, I'll try to look further into it on Monday. |
Thanks again @bruun ! Sorry about that, I should have checked that kind of setup first. Thanks for sharing your config. I've now made a few changes (see updated docs) – you can now import Stripe from 'stripe';
const stripe = new Stripe('sk_test_123', {apiVersion: '2019-12-03'}); Give that a try? Curious what you think. |
@rattrayalex-stripe Great, I can now successfully compile and run our code :-) Personally I like the changes to the apiVersion, and having to explicitly ignore any errors. I think perhaps this part of the docs...
... should include the more specific comment from
As we explicitly use an earlier API version than "2019-12-05", it wasn't immediately clear from the docs what the proper way of specifying this API version was. The second comment makes it very clear. Coming back to the previously discussed
As you said previously, this fails at runtime because When not explicitly importing Stripe in a module, When explicitly importing Stripe, Any ideas? I have made sure to remove Stripe from the tsconfig "types" array. |
Fantastic! Great suggestion; reworded. Let me know what you think. I removed the direct export of the error class types; they're now only accessible from |
The docs are very clear now, thank you! Removing the export on the class types didn't help, unfortunately. I have not worked much with namespaces and modules in Typescript so this is outside my comfort zone, but Typescript is apparently still able to find the |
Ah, darn. I'm not sure if there's anything we can do about that, then. Presumably it'd be rare for users to run into this, and it only happened to you because of the unfortunate import story from before? |
That sounds likely, yeah. Maybe someone will come along with a suggested fix once it's released and people start migrating from DT |
Hey @rattrayalex-stripe, I'm running into issues with the Edit: I forgot to mention that I'm using version I'm getting an error when I invoke this.stripe.invoices.listUpcomingLineItems(customerId, {
limit,
starting_after: startingAfter,
})
WorkaroundI have managed to find a workaround, which is to ignore the type definitions and insert the const method: any = this.stripe.invoices
return method.listUpcomingLineItems({
customer: customerId,
limit,
starting_after: startingAfter,
}) Suggested FixReplace listUpcomingLineItems(
id: string,
params?: InvoiceLineItemListUpcomingParams,
options?: RequestOptions
): ApiListPromise<InvoiceLineItem>;
listUpcomingLineItems(
id: string,
options?: RequestOptions
): ApiListPromise<InvoiceLineItem>; With listUpcomingLineItems(
params: InvoiceLineItemListUpcomingParams,
options?: RequestOptions
): ApiListPromise<InvoiceLineItem>;
listUpcomingLineItems(
params: InvoiceLineItemListUpcomingParams,
options?: RequestOptions
): ApiListPromise<InvoiceLineItem>; and update interface InvoiceLineItemListUpcomingParams {
/**
* The identifier of the customer whose upcoming invoice line items you'd like to retrieve.
*/
customer: string;
.
.
.
} |
Thanks for reporting @JetJet13 ! You're correct; I hope to fix this up today. |
No real issues to report aside from the one mentioned. The transition from Thanks for picking this up! |
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.
Again, amazing job @rattrayalex-stripe and @jlomas-stripe!
I focused on the non-TS files and the manually written TS files (shared.d.ts
, lib.d.ts
, webhooks.d.ts
, and OAuth.d.ts
). Left one comment, but LGTM otherwise. I'd feel better if someone with more Node/TS experience reviewed too, but up to you.
*/ | ||
constructEvent( | ||
payload: string, | ||
header: string, |
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.
payload
and header
can be either string
or Buffer
s, cf.
Lines 71 to 72 in f6e5ad3
payload = Buffer.isBuffer(payload) ? payload.toString('utf8') : payload; | |
header = Buffer.isBuffer(header) ? header.toString('utf8') : header; |
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.
Thanks fixed. Note that this required adding @types/node
as a dependency, meaning it could add a bit of bloat for non-TS users. Given this is a server-only package, I don't think that's the end of the world.
0992b7a
to
2b76719
Compare
2b76719
to
578a768
Compare
```ts | ||
import Stripe from 'stripe'; | ||
const stripe = new Stripe('sk_test_...', { | ||
apiVersion: '2019-12-03', |
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.
Let's make sure our release script updates this automatically.
|
||
To install: | ||
Types can change between API versions (eg; Stripe may have changed a field from a string to a hash), |
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.
e.g., I think is our style
const typescript = props.typescript || false; | ||
if (typescript !== Stripe.USER_AGENT.typescript) { | ||
// The mutation here is uncomfortable, but likely fastest; | ||
// seralizing the user agent involves shelling out to the system, |
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.
serIalizing
Will correct typos in a follow-on, we'll likely have more work to do on README in any case. |
Thanks to everyone in the community for the feedback! You're welcome & encouraged to continue using this branch until v8.0.0 is released, likely in early-mid January, and please do continue to provide feedback, big & small! |
* Drop support for Node 6 * TypeScript definitions [beta] (#736) * First pass * wip * More changes * Split resources by file * Move to api version folder * Remove tsconfig, add prettierignore * Properly generate nested and no-method resources * Document new TypeScript usage in README * Add a basic test for typescript correctness * Better formatting for export of Stripe * Break out subtypes * Separate out Address * Pull empty strings out of enums * Use all named subresources from openapi * Rough draft of using shared subresources * Revert "Rough draft of using shared subresources" This reverts commit eb9a4d8. * Fix some bugs I think * Okay actually go back to normal * Actually use all shared subresources from openapi * Revert "Actually use all shared subresources from openapi" This reverts commit 52136ae. * Add webhooks.d.ts * Add and test ApiListPromise for async iteration of lists * Add better types for ApiList and HeaderOptions * Rename HeaderOptions to RequestOptions * Add docs and tests for expanding objects * Add in reference to webhooks.d.ts * Update snapshots to reflect reordering * Add multiple dispatch for optional params * Add TS tests for multiple dispatch, config, etc * Move types for setAppInfo * Add errors * Fix some formatting * Remove (Customer) Source and ExternalAccount * Move metadata back up, add deleted?: void, add eg DeletedCustomer to the union for expandable deletable objects * Reorder nested methods, rename nested & delete param types, move nested model & param types into their own files * Remove docstrings from param types since they exist on method definitions * Reorder required params to be first * Minor method reorder * Describe why you have to explicitly reference the types * Move from vYYYY-MM-DD to YYYY-MM-DD, upgrade api version to 2019-12-03 * Update future version * Fix param type name and mention esModuleInterop in docs * Add docstring and test for ApiList * Add docstrings for errors * Fix accidentally optional fields * Add missing values to RawErrorType * Fix many non-optional fields * Remove whitespace typo * Add signature for retrieving a specific account * Add types for OAuth * Openapi updates * Rename Webhooks in git * Use export default instead of export = so that users dont need to do esModuleInterop * Add a default export to the runtime * Also export Stripe as a named export, and as a static property on itself * Collapse types for setHost * Require the user to specify the latest API version, use types by default. * Fix a type error * Reorganize test, use strictNullChecks etc * Fix readme * Unexport error classes to avoid instanceof confusion * Clarify how to use old api versions with TS * Fix typo params -> param * Updates from openapi * Mark the args to several setters as required * More accurate handling of deleted resources and required/optional fields * Erroneous handling of deleted * Revert "Erroneous handling of deleted" This reverts commit ad64e33. * Add comment about deprecated total_count. * Add shared types for Addresses and Metadata, and update openapi * Use null intead of empty string * Fix invoice upcoming lines * Update openapi * More empty-string to null translation * Stop polluting the global namespace by scoping everything under module stripe * Run eslint & prettier on TS files * Continue to have poor types for HttpAgent, but with more claity as to why * Clean up explicit any lint warnings * Remove constants intended for internal use only * Extract CustomerSource to a standalone type * Remove unnecessary export * Better types for account debit sources * Use shared RangeQueryParam and PaginationParam * Add expand param to SubscriptionDeleteParams * Bring business_type back * Add docstrings to config options * Add mandatory `typescript: true` config prop * Use await in TS readme * Wrap await in async function * Many readme updates * Remove docs on non-public fields/params (we should revert this if it comes up a lot) * Clarify async/await a bit * TypeScriptify docstrings for Webhooks * Add @types/node as a dependency, use it for typing Buffer and http.Agent * Remove a TODO Co-authored-by: jlomas-stripe <jlomas-stripe@users.noreply.github.com> * Small typo fixes (#743) * Correct "eg;" to "e.g.," * Fix typo * Remove deprecated resources and methods (#744) * Remove stripe.usageRecords and stripe.usageRecordSummaries, add stripe.subscriptionItems.listUsageRecordSummaries * Update openapi * Remove Recipients * Remove BitcoinReceivers * Remove ThreeDSecure * Fix SubscriptionSchedules.Plan.plan (#748) It was incorrectly pointing back to SubscriptionSchedules.Plan instead of Stripe.Plan * Openapi updates * Use Stripe. prefix for all top-level resources (#751) * Deprecate many library api's, unify others (#752) * Deprecate snake_case options like stripe_account with warning messages * Remove deprecated Error base export, extend method, and populate method * Privatize several constants * Add protocol to config object for local proxies etc * Add `appInfo` config option * Mark all setter methods as deprecated, emit warnings. * Mark methods prefixed with an underscore as private * Mark a few getter methods which are not prefixed with underscores as private * Rename the request option stripeVersion to apiVersion to be consistent with the config option and our overall terminology * Throw an error if a deprecated and non-deprecated version of the same request option is passed * Accept an array of strings or buffers as the header argument for webhooks.constructEvent (#753) * Stop importing errors as a single Error object, which shadowed the real Error object * Accept an array of strings (or buffers) as the header in webhooks * Actually just throw an error if an array is actually passed * Improve markdown links in TS comments (#754) Co-authored-by: jlomas-stripe <jlomas-stripe@users.noreply.github.com> Co-authored-by: Petja Touru <hello@petja.me>
hey, we're using this, I know it's merged... but any chance more interfaces could be exported? specifically I wanted to make a type that configures our payment intents for both stripe and stripe terminal
and in one of my payment methods I have
what'd I'd really like to do is have I can make a new ticket if y'all prefer. |
@xenoterracide Yes, please open a new ticket! |
This adds TypeScript definitions for stripe-node, reflecting the latest API version.
To try it out, install with:
Docs are here.
Feedback from the community would be much appreciated on this! Email me at rattrayalex@stripe.com or leave comments here.
Fixes #296