-
Notifications
You must be signed in to change notification settings - Fork 765
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
[Feature] Strongly Typed Event Type for Stripe.Event #902
Comments
Hi @SimplyBallistic, thanks for the suggestion! Strongly typing this field is certainly desirable, but to make this happen we need to modify the (closed-source-for-now) tool that generates the type definitions from the openapi spec. We would also need to make sure that the spec contains an authoritative list of all the event types, which I suspect it may not, but I will investigate. |
I came here to suggest the same. It's worth noting, if a blocker for getting a typedef together for |
This seems to also be referenced by #758 |
There's a quick workaround for this. Add this to a declaration or any ts file imported in your project: declare module 'stripe' {
namespace Stripe {
interface TypedEventData<T> extends Stripe.Event.Data {
object: T;
previous_attributes?: Partial<T>;
}
interface TypedEvent<T = any> extends Stripe.Event {
data: TypedEventData<T>;
type: Exclude<Stripe.WebhookEndpointCreateParams.EnabledEvent, '*'>;
}
}
} Then use |
This could go even further IMO: webhook event types (eg: It would indeed require some internal tooling change at Stripe to connect event.type to its correct event.data type, but this would make it impossible to introduce bugs when writing webhooks handlers (eg: type-casting a payload into a Subscription where the actual object passed in is a Customer). I've got an example of this running in my SaaS, it required doing the mapping manually though: |
Strongly related: #1387 |
Duplicate of #758? |
Someone put in the effort of making a disjoint union of it and publishing it to npm: https://npm.im/stripe-event-types |
So the issue as described in the original post is fixed— |
I think #1921 (v14.0.0) solves this issue! |
This is now available starting with v13.11.0 |
Not too important, but it would be nice if this type:
stripe-node/types/2020-03-02/Events.d.ts
Line 52 in 02da7dc
is strongly typed with all possible events. This would help catch bugs within switch statements or other logic statements, while also providing a nice menu of events within your IDE.
If you are open to it, I don't mind making a quick script to take the webhook dropdown and convert it to a type definition for this variable in a PR.
The text was updated successfully, but these errors were encountered: