Skip to content

Commit d42eb01

Browse files
Add support for guzzle promises in props (#316)
Co-authored-by: Rhys <[email protected]> Co-authored-by: Claudio Dekker <[email protected]>
1 parent f697368 commit d42eb01

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Response.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inertia;
44

55
use Closure;
6+
use GuzzleHttp\Promise\PromiseInterface;
67
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Contracts\Support\Responsable;
89
use Illuminate\Http\JsonResponse;
@@ -101,6 +102,10 @@ public function toResponse($request)
101102
$prop = App::call($prop);
102103
}
103104

105+
if ($prop instanceof PromiseInterface) {
106+
$prop = $prop->wait();
107+
}
108+
104109
if ($prop instanceof ResourceResponse || $prop instanceof JsonResource) {
105110
$prop = $prop->toResponse($request)->getData(true);
106111
}

tests/ResponseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Inertia\LazyProp;
1515
use Inertia\Response;
1616
use Inertia\Tests\Stubs\FakeResource;
17+
use Mockery;
1718

1819
class ResponseTest extends TestCase
1920
{
@@ -153,6 +154,29 @@ public function test_arrayable_prop_response(): void
153154
$this->assertSame('123', $page->version);
154155
}
155156

157+
public function test_promise_props_are_resolved(): void
158+
{
159+
$request = Request::create('/user/123', 'GET');
160+
$request->headers->add(['X-Inertia' => 'true']);
161+
162+
$user = (object) ['name' => 'Jonathan'];
163+
164+
$promise = Mockery::mock('GuzzleHttp\Promise\PromiseInterface')
165+
->shouldReceive('wait')
166+
->andReturn($user)
167+
->mock();
168+
169+
$response = new Response('User/Edit', ['user' => $promise], 'app', '123');
170+
$response = $response->toResponse($request);
171+
$page = $response->getData();
172+
173+
$this->assertInstanceOf(JsonResponse::class, $response);
174+
$this->assertSame('User/Edit', $page->component);
175+
$this->assertSame('Jonathan', $page->props->user->name);
176+
$this->assertSame('/user/123', $page->url);
177+
$this->assertSame('123', $page->version);
178+
}
179+
156180
public function test_xhr_partial_response(): void
157181
{
158182
$request = Request::create('/user/123', 'GET');

0 commit comments

Comments
 (0)