From e2e5e0d9567183ca0fb99e0ca22fe386467e360b Mon Sep 17 00:00:00 2001 From: AL EMRAN Date: Wed, 9 Aug 2023 13:09:15 +0600 Subject: [PATCH] Enhance Webhook Client to Support Multiple HTTP Methods for Receiving Requests (#192) * Add support for Route method dynamic * add method validation & Invalid Exception class * Remove validatedMethod and add method validation in packageBooted * Added default webhook route method to options * Update README.md --------- Co-authored-by: Freek Van der Herten --- README.md | 11 ++++++++++- src/Exceptions/InvalidMethod.php | 13 +++++++++++++ src/WebhookClientServiceProvider.php | 10 ++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/InvalidMethod.php diff --git a/README.md b/README.md index cdc4b72..ea2fa82 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ Finally, let's take care of the routing. At the app that sends webhooks, you pro Route::webhooks('webhook-receiving-url'); ``` -Behind the scenes, this will register a `POST` route to a controller provided by this package. Because the app that sends webhooks to you has no way of getting a csrf-token, you must add that route to the `except` array of the `VerifyCsrfToken` middleware: +Behind the scenes, by default this will register a `POST` route to a controller provided by this package. Because the app that sends webhooks to you has no way of getting a csrf-token, you must add that route to the `except` array of the `VerifyCsrfToken` middleware: ```php protected $except = [ @@ -292,6 +292,15 @@ Route::webhooks('receiving-url-for-app-1', 'webhook-sending-app-1'); Route::webhooks('receiving-url-for-app-2', 'webhook-sending-app-2'); ``` +### Change route method +Being an incoming webhook client, there are instances where you might want to establish a route method other than the default `post`. You have the flexibility to modify the standard post method to options such as `get`, `put`, `patch`, or `delete`. +```php +Route::webhooks('receiving-url-for-app-1', 'webhook-sending-app-1', 'get'); +Route::webhooks('receiving-url-for-app-1', 'webhook-sending-app-1', 'put'); +Route::webhooks('receiving-url-for-app-1', 'webhook-sending-app-1', 'patch'); +Route::webhooks('receiving-url-for-app-1', 'webhook-sending-app-1', 'delete'); +``` + ### Using the package without a controller If you don't want to use the routes and controller provided by your macro, you can programmatically add support for webhooks to your own controller. diff --git a/src/Exceptions/InvalidMethod.php b/src/Exceptions/InvalidMethod.php new file mode 100644 index 0000000..092f996 --- /dev/null +++ b/src/Exceptions/InvalidMethod.php @@ -0,0 +1,13 @@ +name("webhook-client-{$name}"); + Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') { + + if(! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { + throw InvalidMethod::make($method); + } + + return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')->name("webhook-client-{$name}"); }); $this->app->scoped(WebhookConfigRepository::class, function () {