Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ All toggles will be available as the `flags` prop in your frontend:
// Vue/React
const { flags } = usePage().props

if (flags['new-checkout']) {
if (flags.newCheckout) {
// Show new checkout
}
```
Expand Down
7 changes: 6 additions & 1 deletion src/Middleware/ShareTogglesWithInertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OffloadProject\Toggle\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Middleware;
use OffloadProject\Toggle\Facades\Toggle;

Expand All @@ -15,9 +16,13 @@ class ShareTogglesWithInertia extends Middleware
*/
public function share(Request $request): array
{
$flags = collect(Toggle::all())
->mapWithKeys(fn (bool $value, string $key) => [Str::camel($key) => $value])
->all();

return [
...parent::share($request),
'flags' => Toggle::all(),
'flags' => $flags,
];
}
}
51 changes: 51 additions & 0 deletions tests/Feature/InertiaMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use Illuminate\Http\Request;
use OffloadProject\Toggle\Middleware\ShareTogglesWithInertia;

it('shares toggles as flags prop', function () {
$middleware = new ShareTogglesWithInertia;
$request = Request::create('/');

$shared = $middleware->share($request);

expect($shared)->toHaveKey('flags')
->and($shared['flags'])->toBeArray();
});

it('converts kebab-case toggle names to camelCase', function () {
config()->set('toggle.flags', [
'test-flag' => true,
'another-feature-flag' => false,
'simple' => true,
]);

$middleware = new ShareTogglesWithInertia;
$request = Request::create('/');

$shared = $middleware->share($request);

expect($shared['flags'])
->toHaveKey('testFlag')
->toHaveKey('anotherFeatureFlag')
->toHaveKey('simple')
->not->toHaveKey('test-flag')
->not->toHaveKey('another-feature-flag');
});
Comment on lines +18 to +36

Copilot AI Jan 14, 2026

Copy link

Choose a reason for hiding this comment

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

Consider adding test coverage for edge cases with different naming conventions. While the current tests cover kebab-case conversion well, it would be beneficial to test how snake_case (e.g., 'feature_flag'), PascalCase (e.g., 'FeatureFlag'), and mixed formats are handled by Str::camel(). This ensures the behavior is consistent and predictable for various input formats that users might employ.

Copilot uses AI. Check for mistakes.

it('preserves toggle values after key conversion', function () {
config()->set('toggle.flags', [
'enabled-flag' => true,
'disabled-flag' => false,
]);

$middleware = new ShareTogglesWithInertia;
$request = Request::create('/');

$shared = $middleware->share($request);

expect($shared['flags']['enabledFlag'])->toBeTrue()
->and($shared['flags']['disabledFlag'])->toBeFalse();
});