Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to enable/disable unique token #212

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/webhook-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@
* It deletes all records after 1 week. Set to null if no models should be deleted.
*/
'delete_after_days' => 30,

/*
* Should a unique token be added to the route name
*/
'add_unique_token_to_route_name' => false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@it-can Just personal preference but I would probably simplify this to 'tokenize_route_name'.

Copy link
Contributor Author

@it-can it-can Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think @freekmurze should decide on this one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although tokenize_route_name would be good I thinkadd_unique_token_to_route_name is more clear / human.

];
18 changes: 13 additions & 5 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public function packageBooted()
{
Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') {

if(! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
if (! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw InvalidMethod::make($method);
}

if (config('webhook-client.add_unique_token_to_route_name', false)) {
$name .= Str::random(8);
it-can marked this conversation as resolved.
Show resolved Hide resolved
}

return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')
->name("webhook-client-{$name}." . Str::random(8));
->name("webhook-client-{$name}");
});

$this->app->scoped(WebhookConfigRepository::class, function () {
$configRepository = new WebhookConfigRepository();
$configRepository = new WebhookConfigRepository;

collect(config('webhook-client.configs'))
->map(fn (array $config) => new WebhookConfig($config))
Expand All @@ -44,9 +48,13 @@ public function packageBooted()
$this->app->bind(WebhookConfig::class, function () {
$routeName = Route::currentRouteName() ?? '';

$routeNameSuffix = Str::after($routeName, 'webhook-client-');
$configName = Str::after($routeName, 'webhook-client-');

$configName = Str::before($routeNameSuffix, '.');
if (config('webhook-client.add_unique_token_to_route_name', false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@it-can I think this should probably default to true since the shared route names already break route caching and there is a documented feature that causes that scenario. I can definitely see the benefit of calling the routes by name but route naming is more of an underlying structure of the package and not a documented feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@freekmurze can decide on this one too...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be set to false for now, so we don't break stuff for existing users.

For a next major version, I'll consider setting it to true by default.

$routeNameSuffix = Str::after($routeName, 'webhook-client-');

$configName = Str::before($routeNameSuffix, '.');
}

$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);

Expand Down
2 changes: 2 additions & 0 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public function it_can_store_none_of_the_headers()
/** @test */
public function multiple_routes_can_share_configuration()
{
config()->set('webhook-client.add_unique_token_to_route_name', true);

Route::webhooks('incoming-webhooks-additional');

$this->refreshWebhookConfigRepository();
Expand Down