Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 76ae759

Browse files
author
Andrey Helldar
committed
Optimized determination of the need for wrapping to the data key
1 parent 354d8c1 commit 76ae759

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Services/Response.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Helldar\ApiResponse\Wrappers\Resolver;
1111
use Helldar\ApiResponse\Wrappers\Success;
1212
use Helldar\Support\Concerns\Makeable;
13+
use Illuminate\Http\Resources\Json\JsonResource;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415

1516
final class Response implements Responsable
@@ -47,11 +48,19 @@ public static function withoutWith(): void
4748
public static function wrapped(): void
4849
{
4950
self::$wrap = true;
51+
52+
if (class_exists(JsonResource::class)) {
53+
JsonResource::wrap('data');
54+
}
5055
}
5156

5257
public static function withoutWrap(): void
5358
{
5459
self::$wrap = false;
60+
61+
if (class_exists(JsonResource::class)) {
62+
JsonResource::withoutWrapping();
63+
}
5564
}
5665

5766
public function with(array $with = []): Responsable

tests/Laravel/TestCase.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Helldar\ApiResponse\Services\Response;
66
use Illuminate\Contracts\Debug\ExceptionHandler;
7-
use Illuminate\Http\Resources\Json\JsonResource;
87
use Orchestra\Testbench\TestCase as BaseTestCase;
98
use Tests\Fixtures\Concerns\Laravel\Application;
109
use Tests\Fixtures\Concerns\Laravel\Exceptionable;
@@ -49,10 +48,6 @@ protected function resolveApplicationExceptionHandler($app)
4948

5049
protected function setWrapping(): void
5150
{
52-
$this->wrap
53-
? JsonResource::wrap('data')
54-
: JsonResource::withoutWrapping();
55-
5651
$this->wrap
5752
? Response::wrapped()
5853
: Response::withoutWrap();

0 commit comments

Comments
 (0)