Skip to content

Exclude except props from partial reloads #622

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

Merged
merged 1 commit into from
May 17, 2024
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
18 changes: 17 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function resolveProperties(Request $request, array $props): array
{
$isPartial = $request->header(Header::PARTIAL_COMPONENT) === $this->component;

if(!$isPartial) {
if(! $isPartial) {
$props = array_filter($this->props, static function ($prop) {
return ! ($prop instanceof LazyProp);
});
Expand All @@ -123,6 +123,10 @@ public function resolveProperties(Request $request, array $props): array
$props = $this->resolveOnly($request, $props);
}

if($isPartial && $request->hasHeader(Header::PARTIAL_EXCEPT)) {
$props = $this->resolveExcept($request, $props);
}

$props = $this->resolvePropertyInstances($props, $request);

return $props;
Expand Down Expand Up @@ -169,6 +173,18 @@ public function resolveOnly(Request $request, array $props): array
return $value;
}

/**
* Resolve the `except` partial request props.
*/
public function resolveExcept(Request $request, array $props): array
{
$except = array_filter(explode(',', $request->header(Header::PARTIAL_EXCEPT, '')));

Arr::forget($props, $except);

return $props;
}

/**
* Resolve all necessary class instances in the given props.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Support/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class Header
public const VERSION = 'X-Inertia-Version';
public const PARTIAL_COMPONENT = 'X-Inertia-Partial-Component';
public const PARTIAL_ONLY = 'X-Inertia-Partial-Data';
public const PARTIAL_EXCEPT = 'X-Inertia-Partial-Except';
}
59 changes: 57 additions & 2 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,29 @@ public function test_xhr_partial_response(): void
$this->assertSame('123', $page->version);
}

public function test_exclude_props_from_partial_response(): void
{
$request = Request::create('/user/123', 'GET');
$request->headers->add(['X-Inertia' => 'true']);
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
$request->headers->add(['X-Inertia-Partial-Except' => 'user']);

$user = (object) ['name' => 'Jonathan'];
$response = new Response('User/Edit', ['user' => $user, 'partial' => 'partial-data'], 'app', '123');
$response = $response->toResponse($request);
$page = $response->getData();

$props = get_object_vars($page->props);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('User/Edit', $page->component);
$this->assertFalse(isset($props['user']));
$this->assertCount(1, $props);
$this->assertSame('partial-data', $page->props->partial);
$this->assertSame('/user/123', $page->url);
$this->assertSame('123', $page->version);
}

public function test_nested_partial_props(): void
{
$request = Request::create('/user/123', 'GET');
Expand All @@ -275,8 +298,8 @@ public function test_nested_partial_props(): void
'token' => 'value',
],
'shared' => [
'flash' => 'Value',
]
'flash' => 'value',
],
];

$response = new Response('User/Edit', $props);
Expand All @@ -290,6 +313,38 @@ public function test_nested_partial_props(): void
$this->assertSame('value', $page->props->auth->refresh_token);
}

public function test_exclude_nested_props_from_partial_response(): void
{
$request = Request::create('/user/123', 'GET');
$request->headers->add(['X-Inertia' => 'true']);
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
$request->headers->add(['X-Inertia-Partial-Data' => 'auth']);
$request->headers->add(['X-Inertia-Partial-Except' => 'auth.user']);

$props = [
'auth' => [
'user' => new LazyProp(function () {
return [
'name' => 'Jonathan Reinink',
'email' => '[email protected]',
];
}),
'refresh_token' => 'value',
],
'shared' => [
'flash' => 'value',
],
];

$response = new Response('User/Edit', $props);
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertFalse(isset($page->props->auth->user));
$this->assertFalse(isset($page->props->shared));
$this->assertSame('value', $page->props->auth->refresh_token);
}

public function test_lazy_props_are_not_included_by_default(): void
{
$request = Request::create('/users', 'GET');
Expand Down
Loading