Support multiple handlers for the same webhook topic #827
Description
Overview
I would like to propose some improvements to the webhook registration logic of the library. These changes aim to address the following issues:
-
Deletion of unrelated topics during individual webhook registration: Currently, when registering a new topic individually, the library removes existing topics with different names. For example, if a
COLLECTIONS_UPDATE
topic exists and you register anAPP_SUBSCRIPTIONS_UPDATE
topic in a separate registration call, theCOLLECTIONS_UPDATE
webhook will be deleted. This behavior is not desired, as developers may want to subscribe to multiple topics individually. It is worth noting that subscribing to multiple topics at once does not cause this issue. -
Limitation on registering the same topic with different URLs: The library currently does not allow registering the same topic with different URLs. For instance, if you register an
APP_SUBSCRIPTIONS_UPDATE
topic withdomain.com/webhook
, trying to register the same topic withdomain.com/webhook-new
will result in the deletion of the previous one. This limitation has already been discussed in issue #427, but no changes have been made. According to a response from SBD_ on this Shopify community post, Shopify's API allows multiple subscriptions to the same webhook topic with different URLs.
To address these issues, I have implemented the necessary logic and introduced a new parameter allowMultipleHandlers
, which is false
by default to maintain the library's current behavior. To enable multiple handlers, you can use the following code:
api.webhooks.register({
session,
allowMultipleHandlers: true,
});
I am happy to add tests and submit a PR if these changes align with the goals of the core contributors. You can review my code changes in my repository here. Your feedback is much appreciated. Thank you.