Skip to content

Commit

Permalink
Ensure resource primary key is included in response
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed May 10, 2022
1 parent e236086 commit 52bcabd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
6 changes: 6 additions & 0 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Admin\Models\Customers_model;
use Admin\Models\Users_model;
use Dingo\Api\Auth\Auth;
use Igniter\Api\Classes\ScopeFactory;
use Igniter\Flame\Database\Model;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
use Laravel\Sanctum\Sanctum;
use League\Fractal\Manager;
use System\Classes\BaseExtension;

/**
Expand Down Expand Up @@ -193,6 +195,10 @@ public function registerApiResources()
*/
protected function registerResponseFactory()
{
$this->app->bind(Manager::class, function () {
return new Manager(new ScopeFactory);
});

$this->app->alias('api.response', Classes\ResponseFactory::class);

$this->app->singleton('api.response', function ($app) {
Expand Down
26 changes: 26 additions & 0 deletions classes/Scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Igniter\Api\Classes;

use Igniter\Flame\Database\Model;
use League\Fractal\Scope as FractalScope;

/**
* Scope
*
* The scope class acts as a tracker, relating a specific resource in a specific
* context. For example, the same resource could be attached to multiple scopes.
* There are root scopes, parent scopes and child scopes.
*/
class Scope extends FractalScope
{
protected function fireTransformer($transformer, $data)
{
[$transformedData, $includedData] = parent::fireTransformer($transformer, $data);

if (!array_key_exists('id', $transformedData) && $data instanceof Model)
$transformedData['id'] = $data->getKey();

return [$transformedData, $includedData];
}
}
15 changes: 15 additions & 0 deletions classes/ScopeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Igniter\Api\Classes;

use League\Fractal\Manager;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\ScopeFactory as FractalScopeFactory;

class ScopeFactory extends FractalScopeFactory
{
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
{
return new Scope($manager, $resource, $scopeIdentifier);
}
}
4 changes: 4 additions & 0 deletions classes/TransformerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @deprecated
* @package Igniter\Api\Classes
*/
class TransformerAbstract extends JsonResource
{
/**
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"laravel/sanctum": "~2.13.0"
},
"extra": {
"laravel": {
"dont-discover": [
"*"
]
},
"tastyigniter-extension": {
"code": "igniter.api",
"name": "APIs",
Expand Down
16 changes: 0 additions & 16 deletions serializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@

namespace Igniter\Api\Serializer;

use Igniter\Flame\Support\Str;
use League\Fractal\Serializer\JsonApiSerializer as FractalJsonApiSerializer;

class JsonApiSerializer extends FractalJsonApiSerializer
{
public function item($resourceKey, array $data)
{
// Hacky way of ensuring the id key is always present
// in the data array, should probably change later
if (!array_key_exists('id', $data) && $resourceKey) {
$keyName = Str::singular($resourceKey).'_id';
$data = array_merge($data, [
'id' => array_get($data, $keyName),
]);

unset($data[$keyName]);
}

return parent::item($resourceKey, $data);
}
}

0 comments on commit 52bcabd

Please sign in to comment.