Skip to content

Commit 5675663

Browse files
authored
exclude properties from partial responses (#622)
1 parent 27fb7e8 commit 5675663

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/Response.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function resolveProperties(Request $request, array $props): array
113113
{
114114
$isPartial = $request->header(Header::PARTIAL_COMPONENT) === $this->component;
115115

116-
if(!$isPartial) {
116+
if(! $isPartial) {
117117
$props = array_filter($this->props, static function ($prop) {
118118
return ! ($prop instanceof LazyProp);
119119
});
@@ -125,6 +125,10 @@ public function resolveProperties(Request $request, array $props): array
125125
$props = $this->resolveOnly($request, $props);
126126
}
127127

128+
if($isPartial && $request->hasHeader(Header::PARTIAL_EXCEPT)) {
129+
$props = $this->resolveExcept($request, $props);
130+
}
131+
128132
$props = $this->resolvePropertyInstances($props, $request);
129133

130134
return $props;
@@ -174,6 +178,18 @@ public function resolveOnly(Request $request, array $props): array
174178
return $value;
175179
}
176180

181+
/**
182+
* Resolve the `except` partial request props.
183+
*/
184+
public function resolveExcept(Request $request, array $props): array
185+
{
186+
$except = array_filter(explode(',', $request->header(Header::PARTIAL_EXCEPT, '')));
187+
188+
Arr::forget($props, $except);
189+
190+
return $props;
191+
}
192+
177193
/**
178194
* Resolve all necessary class instances in the given props.
179195
*/

src/Support/Header.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class Header
1010
public const VERSION = 'X-Inertia-Version';
1111
public const PARTIAL_COMPONENT = 'X-Inertia-Partial-Component';
1212
public const PARTIAL_ONLY = 'X-Inertia-Partial-Data';
13+
public const PARTIAL_EXCEPT = 'X-Inertia-Partial-Except';
1314
}

tests/ResponseTest.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ public function test_xhr_partial_response(): void
256256
$this->assertSame('123', $page->version);
257257
}
258258

259+
public function test_exclude_props_from_partial_response(): void
260+
{
261+
$request = Request::create('/user/123', 'GET');
262+
$request->headers->add(['X-Inertia' => 'true']);
263+
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
264+
$request->headers->add(['X-Inertia-Partial-Except' => 'user']);
265+
266+
$user = (object) ['name' => 'Jonathan'];
267+
$response = new Response('User/Edit', ['user' => $user, 'partial' => 'partial-data'], 'app', '123');
268+
$response = $response->toResponse($request);
269+
$page = $response->getData();
270+
271+
$props = get_object_vars($page->props);
272+
273+
$this->assertInstanceOf(JsonResponse::class, $response);
274+
$this->assertSame('User/Edit', $page->component);
275+
$this->assertFalse(isset($props['user']));
276+
$this->assertCount(1, $props);
277+
$this->assertSame('partial-data', $page->props->partial);
278+
$this->assertSame('/user/123', $page->url);
279+
$this->assertSame('123', $page->version);
280+
}
281+
259282
public function test_nested_partial_props(): void
260283
{
261284
$request = Request::create('/user/123', 'GET');
@@ -275,8 +298,8 @@ public function test_nested_partial_props(): void
275298
'token' => 'value',
276299
],
277300
'shared' => [
278-
'flash' => 'Value',
279-
]
301+
'flash' => 'value',
302+
],
280303
];
281304

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

316+
public function test_exclude_nested_props_from_partial_response(): void
317+
{
318+
$request = Request::create('/user/123', 'GET');
319+
$request->headers->add(['X-Inertia' => 'true']);
320+
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
321+
$request->headers->add(['X-Inertia-Partial-Data' => 'auth']);
322+
$request->headers->add(['X-Inertia-Partial-Except' => 'auth.user']);
323+
324+
$props = [
325+
'auth' => [
326+
'user' => new LazyProp(function () {
327+
return [
328+
'name' => 'Jonathan Reinink',
329+
'email' => 'jonathan@example.com',
330+
];
331+
}),
332+
'refresh_token' => 'value',
333+
],
334+
'shared' => [
335+
'flash' => 'value',
336+
],
337+
];
338+
339+
$response = new Response('User/Edit', $props);
340+
$response = $response->toResponse($request);
341+
$page = $response->getData();
342+
343+
$this->assertFalse(isset($page->props->auth->user));
344+
$this->assertFalse(isset($page->props->shared));
345+
$this->assertSame('value', $page->props->auth->refresh_token);
346+
}
347+
293348
public function test_lazy_props_are_not_included_by_default(): void
294349
{
295350
$request = Request::create('/users', 'GET');

0 commit comments

Comments
 (0)