Skip to content

Commit af1259e

Browse files
add missing fetchEvent type for UnknownEventNotification (#2453)
add missing fetchEvent type for UnknownThinEvent
1 parent 9ab58ed commit af1259e

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

src/stripe.core.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,21 @@ export function createStripe(
525525
);
526526
};
527527

528-
if (eventNotification.related_object) {
529-
eventNotification.fetchRelatedObject = (): Promise<unknown> => {
530-
return this._requestSender._rawRequest(
531-
'GET',
532-
eventNotification.related_object.url,
533-
undefined,
534-
{
535-
stripeContext: eventNotification.context,
536-
},
537-
['fetch_related_object']
538-
);
539-
};
540-
}
528+
eventNotification.fetchRelatedObject = (): Promise<unknown> => {
529+
if (!eventNotification.related_object) {
530+
return Promise.resolve(null);
531+
}
532+
533+
return this._requestSender._rawRequest(
534+
'GET',
535+
eventNotification.related_object.url,
536+
undefined,
537+
{
538+
stripeContext: eventNotification.context,
539+
},
540+
['fetch_related_object']
541+
);
542+
};
541543

542544
return eventNotification;
543545
},

test/stripe.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,10 @@ describe('Stripe Module', function() {
802802
);
803803

804804
expect(event.fetchEvent).to.be.a('function');
805-
expect(event.fetchRelatedObject).not.to.be.a('function');
805+
// this is always present, but hidden in the types if there's no object
806+
// not easy to add conditionally because we don't know at runtime if we have a type for the event or not
807+
expect(event.fetchRelatedObject).to.be.a('function');
808+
expect(await event.fetchRelatedObject()).to.be.null;
806809
const pulled = await event.fetchEvent();
807810
expect(pulled.data).to.equal(jsonWithData.data);
808811
// Have to call another requests for metrics to be sent.

types/V2/EventMisc.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ declare module 'stripe' {
3636
* Object containing the reference to API resource relevant to the event.
3737
*/
3838
related_object: V2.Core.Events.RelatedObject | null;
39+
/**
40+
* Fetches the full object corresponding to the `related_object` field.
41+
* Returns `null` if there is no `related_object`.
42+
*/
3943
fetchRelatedObject: () => Promise<unknown>;
44+
/**
45+
* Fetches the full Event object corresponding to this notification.
46+
*/
47+
fetchEvent: () => Promise<V2.Core.EventBase>;
4048
}
4149
}
4250

types/test/typescriptTest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ async (): Promise<void> => {
333333
// @ts-expect-error - shouldn't be available
334334
eventNotification.related_object;
335335
const e: Stripe.Events.V1BillingMeterNoMeterFoundEvent = await eventNotification.fetchEvent();
336+
// @ts-expect-error - isn't a known type
337+
} else if (eventNotification.type === 'some.unknown.event') {
338+
const e = eventNotification as Stripe.Events.UnknownEventNotification;
339+
e.fetchEvent();
340+
// these exist, but may be null
341+
e.related_object;
342+
e.fetchRelatedObject();
336343
}
337344
};
338345

0 commit comments

Comments
 (0)