Skip to content

Commit

Permalink
Enhance Webhook Client to Support Multiple HTTP Methods for Receiving…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
emrancu and freekmurze authored Aug 9, 2023
1 parent 744dc34 commit e2e5e0d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\WebhookClient\Exceptions;

use Exception;

class InvalidMethod extends Exception
{
public static function make($method): self
{
return new static("The method $method is not allowed.");
}
}
10 changes: 8 additions & 2 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\WebhookClient\Exceptions\InvalidConfig;
use Spatie\WebhookClient\Exceptions\InvalidMethod;

class WebhookClientServiceProvider extends PackageServiceProvider
{
Expand All @@ -20,8 +21,13 @@ public function configurePackage(Package $package): void

public function packageBooted()
{
Route::macro('webhooks', function (string $url, string $name = 'default') {
return Route::post($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')->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 () {
Expand Down

0 comments on commit e2e5e0d

Please sign in to comment.