Skip to content

Commit

Permalink
exclude properties from partial responses (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
lepikhinb authored May 17, 2024
1 parent 27fb7e8 commit 5675663
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 @@ -125,6 +125,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 @@ -174,6 +178,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

0 comments on commit 5675663

Please sign in to comment.