#troop4christ_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1326573321218953236
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi there ๐ I'm not quite sure I grasp the concern, so I'm looking for a bit more context.
My rough understanding is that you aren't seeing Events cast as the type you're expecting. Can you help me understand where in your flow exactly that misalignment is occurring? Is it after the constructEvent call?
Yeah... I verify the stripe-signature header using constructEvent and now I'm attempting to write handler methods for each of the different webhook event types that will be handled.
But whenever I attempt to use those types for the event parameter being passed into those handler methods, I get TS errors.
I even tried explicitly defining a type w/ the Stripe event types and still get errors:
I'm a little fuzzy on how this typing works, so sorry if this sounds like a silly question, but in your Event handler (after the event validation with constructEvent) are you able to log the type that is assigned to event? Are they just always plain events?
The only way I've figured out how to get this to work is to make each handler method accept an event: Stripe.Event parameter, and then in the body of the method cast the resource type on event.data.object
i.e. const session = event.data.object as Stripe.Checkout.Session (in the case of the event being a Stripe.CheckoutSessionCompletedEvent)
But I don't wanna do that.. I just wanna use the types directly, not cast them
Huh? No.. this is TypeScript typing I'm talking about.. not the event.type on the object sent from the webhook.
Can you share a snippet of code showing one of those handlers, so I can share that with my teammates who are helping look into this. If you can also share the TS error that you're seeing there, that would be insightful as well.
@viral yoke it's just a simple function..
I showed screenshots of the TS errors above...
My teammate is looking into this as well. They're currently under the impression the types for Events are not automatically cast here, but we're working on confirming that.
Well... we figured it out on our end..
It had something to do w/ how I was choosing which handler method to call.
Glad to hear you got it working! What specific change did you make?
Well.. I was using an enum to limit the string types that we have handlers for, and an object literal for looking up those handlers based on the event.type that comes across in the webhook request:
enum WebhookEventType {
CheckoutSessionCompleted = 'checkout.session.completed',
CheckoutSessionExpired = 'checkout.session.expired',
CustomerSubscriptionDeleted = 'customer.subscription.deleted',
CustomerSubscriptionUpdated = 'customer.subscription.updated',
CustomerSubscriptionResumed = 'customer.subscription.resumed',
}
const WEBHOOK_HANDLERS = {
[WebhookEventType.CheckoutSessionCompleted]: checkoutSessionCompletedHandler,
[WebhookEventType.CheckoutSessionExpired]: checkoutSessionExpiredHandler,
[WebhookEventType.CustomerSubscriptionDeleted]:
customerSubscriptionDeletedHandler,
[WebhookEventType.CustomerSubscriptionUpdated]:
customerSubscriptionUpdatedHandler,
[WebhookEventType.CustomerSubscriptionResumed]:
customerSubscriptionResumedHandler,
}
then I call them accordingly w/ the Stripe.Event object that was constructed:
this.logger.log(`processing event.type => ${event.type}`)
try {
await WEBHOOK_HANDLERS[event.type](event)
} catch (e) {
this.logger.error('Failed to process Stripe event', e)
throw e instanceof HttpException
? e
: new BadRequestException('Failed to process Stripe event')
}
Problem was, I was casting the key to the WEBHOOK_HANDLERS as a WebhookEventType and that was confusing TS for some reason w/r/t interpreting the type for event.type:
await WEBHOOK_HANDLERS[event.type as WebhookEventType](event)
Removing that type casting made the TS errors subside.