Remove extraneous data from API response; force API to en (#1241)
* Remove extraneous data from API response; force API to en * Style fixes * Remove * Fix the meta block * Style fixes
This commit is contained in:
parent
dead1cfd0f
commit
7481dab012
@ -2,7 +2,10 @@
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
use App\Support\Resources\CustomAnonymousResourceCollection;
|
||||
use App\Support\Resources\CustomPaginatedResourceResponse;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
|
||||
/**
|
||||
* Base class for a resource/response
|
||||
@ -26,4 +29,28 @@ class Resource extends JsonResource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the response to exclude all the extra data that isn't used. Based on:
|
||||
* https://gist.github.com/derekphilipau/4be52164a69ce487dcd0673656d280da
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function toResponse($request)
|
||||
{
|
||||
return $this->resource instanceof AbstractPaginator
|
||||
? (new CustomPaginatedResourceResponse($this))->toResponse($request)
|
||||
: parent::toResponse($request);
|
||||
}
|
||||
|
||||
public static function collection($resource)
|
||||
{
|
||||
return tap(new CustomAnonymousResourceCollection($resource, static::class), function ($collection) {
|
||||
if (property_exists(static::class, 'preserveKeys')) {
|
||||
$collection->preserveKeys = (new static([]))->preserveKeys === true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,9 @@ class ApiAuth implements Middleware
|
||||
return $user;
|
||||
});
|
||||
|
||||
// Force english locale for API
|
||||
app()->setLocale('en');
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@ class JsonResponse implements Middleware
|
||||
{
|
||||
$response = $next($request);
|
||||
$response->headers->set('Content-Type', 'application/json');
|
||||
$response->headers->set('charset', 'utf-8');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
16
app/Support/Resources/CustomAnonymousResourceCollection.php
Normal file
16
app/Support/Resources/CustomAnonymousResourceCollection.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
|
||||
class CustomAnonymousResourceCollection extends AnonymousResourceCollection
|
||||
{
|
||||
public function toResponse($request)
|
||||
{
|
||||
return $this->resource instanceof AbstractPaginator
|
||||
? (new CustomPaginatedResourceResponse($this))->toResponse($request)
|
||||
: parent::toResponse($request);
|
||||
}
|
||||
}
|
26
app/Support/Resources/CustomPaginatedResourceResponse.php
Normal file
26
app/Support/Resources/CustomPaginatedResourceResponse.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\PaginatedResourceResponse;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class CustomPaginatedResourceResponse extends PaginatedResourceResponse
|
||||
{
|
||||
protected function paginationLinks($paginated)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function meta($paginated)
|
||||
{
|
||||
return Arr::except($paginated, [
|
||||
'data',
|
||||
'first_page_url',
|
||||
'last_page_url',
|
||||
'prev_page_url',
|
||||
'next_page_url',
|
||||
'links',
|
||||
]);
|
||||
}
|
||||
}
|
@ -104,6 +104,36 @@ class ApiTest extends TestCase
|
||||
->assertJson(['data' => ['name' => $airline->name]]);
|
||||
}
|
||||
|
||||
public function testGetAirlinesChineseChars()
|
||||
{
|
||||
$this->user = factory(User::class)->create([
|
||||
'airline_id' => 0,
|
||||
]);
|
||||
|
||||
factory(Airline::class)->create([
|
||||
'icao' => 'DKH',
|
||||
'name' => '吉祥航空',
|
||||
]);
|
||||
|
||||
factory(Airline::class)->create([
|
||||
'icao' => 'CSZ',
|
||||
'name' => '深圳航空',
|
||||
]);
|
||||
|
||||
factory(Airline::class)->create([
|
||||
'icao' => 'CCA',
|
||||
'name' => '中国国际航空',
|
||||
]);
|
||||
|
||||
factory(Airline::class)->create([
|
||||
'icao' => 'CXA',
|
||||
'name' => '厦门航空',
|
||||
]);
|
||||
|
||||
$res = $this->get('/api/airlines');
|
||||
$this->assertTrue($res->isOk());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user