Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kgajera/stripe-event-types
TypeScript typings for Stripe webhook events
https://github.com/kgajera/stripe-event-types
stripe stripe-webhook
Last synced: 2 days ago
JSON representation
TypeScript typings for Stripe webhook events
- Host: GitHub
- URL: https://github.com/kgajera/stripe-event-types
- Owner: kgajera
- License: mit
- Created: 2022-08-27T19:44:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-01T18:09:30.000Z (6 months ago)
- Last Synced: 2024-12-13T14:06:31.144Z (9 days ago)
- Topics: stripe, stripe-webhook
- Language: TypeScript
- Homepage: https://kgajera.github.io/stripe-event-types
- Size: 1.01 MB
- Stars: 78
- Watchers: 1
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stripe Event Types
[![npm](https://img.shields.io/npm/v/stripe-event-types)](https://www.npmjs.com/package/stripe-event-types)
[![Build](https://github.com/kgajera/stripe-event-types/actions/workflows/build.yml/badge.svg)](https://github.com/kgajera/stripe-event-types/actions/workflows/build.yml)This provides TypeScript typings for Stripe webhook events to strongly type the `type` and `data.object` fields. These types are automatically generated by scraping Stripe's documentation for [types of events](https://stripe.com/docs/api/events/types).
Why is this needed? The typings included in the [`stripe`](https://github.com/stripe/stripe-node) library are lacking in this respect. The type for `type` is a `string` instead of a string literal and the type for `data.object` is `{}` which requires casting each usage of it. This can lead to mistakes in your implementation that could easily be caught with stronger types.
![Typed Webhook Event](https://user-images.githubusercontent.com/1087679/187047509-d8cfe324-0e19-468e-8cdf-7fd3f503ad1f.gif)
## Installation
Install the package with:
```shell
npm install stripe-event-types
# or
yarn add stripe-event-types
```### Version compatability
> **Note**
> If you are using `stripe` version [13.11](https://github.com/stripe/stripe-node/releases/tag/v13.11.0) or greater, you don't need the `stripe-event-types` library because the typings have been added to the `stripe` library.| `stripe-event-types` version | `stripe` version |
| ---------------------------- | ---------------- |
| 3 | 13.5 - 13.10 |
| 2 | 11 |
| 1 | 10 |## Usage
### Webhook event
When constructing the webhook event, cast it to `Stripe.DiscriminatedEvent` to strongly type the `type` and `data.object` fields:
```diff
+///const event = stripe.webhooks.constructEvent(
request.body,
request.headers['stripe-signature'],
'whsec_test'
-);
+) as Stripe.DiscriminatedEvent;
```### Event type
The `Stripe.DiscriminatedEvent.Type` type is a string literal of all event types:
```ts
///const type: Stripe.DiscriminatedEvent.Type = "charge.succeeded";
```