stripe-node icon indicating copy to clipboard operation
stripe-node copied to clipboard

[Feature] Strongly Typed Event Type for Stripe.Event

Open Ry-DS opened this issue 4 years ago • 7 comments

Not too important, but it would be nice if this type:

https://github.com/stripe/stripe-node/blob/02da7dc2c5edae280aaf5e3566f3b7b383024371/types/2020-03-02/Events.d.ts#L52

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.

Ry-DS avatar May 25 '20 08:05 Ry-DS

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.

richardm-stripe avatar Jun 01 '20 16:06 richardm-stripe

I came here to suggest the same. It's worth noting, if a blocker for getting a typedef together for Event and Event.type is around making it exhaustive, it would likely be nearly as helpful for folks to have a non-exhaustive typedef with an index to allow for options not included.

JustinTRoss avatar Jun 20 '20 01:06 JustinTRoss

This seems to also be referenced by #758

JustinTRoss avatar Jun 20 '20 01:06 JustinTRoss

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 Stripe.TypedEvent<Stripe.Subscription> for example. This could easily be added to Stripe's official typing too.

svallory avatar Aug 11 '20 18:08 svallory

This could go even further IMO: webhook event types (eg: customer.created) have a known payload type (eg: Customer).

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: https://gist.github.com/franky47/5f06c1e8271de40e28864ecd8d95ef3c

franky47 avatar Dec 21 '21 09:12 franky47

Strongly related: #1387

saiichihashimoto avatar Apr 20 '22 20:04 saiichihashimoto

Duplicate of https://github.com/stripe/stripe-node/issues/758?

jameshfisher avatar Apr 30 '22 11:04 jameshfisher

Someone put in the effort of making a disjoint union of it and publishing it to npm: https://npm.im/stripe-event-types

StevenLangbroek avatar Aug 17 '23 14:08 StevenLangbroek

So the issue as described in the original post is fixed—event.type is a union of string literals in v13.4.0. However, I think either this issue should be modified or we should create a new issue for making Stripe.Event a discriminated union over event.type (i.e. what stripe-event-types does).

huw avatar Sep 10 '23 08:09 huw

I think #1921 (v14.0.0) solves this issue!

huw avatar Oct 16 '23 23:10 huw

This is now available starting with v13.11.0

pakrym-stripe avatar Oct 26 '23 16:10 pakrym-stripe