Comments (3)
As a workaround, I've added methods on my repository to support JSON API payloads, translating them into Restify ones:
/**
* @psalm-suppress MissingParamType
*/
public function allowToStore(RestifyRequest $request, $payload = null): RestifyRepository
{
$this->adaptJsonApiRequest($request);
return parent::allowToStore($request, $payload);
}
/**
* @psalm-suppress MissingParamType
*/
public function allowToPatch(RestifyRequest $request, $payload = null): RestifyRepository
{
$this->adaptJsonApiRequest($request, true);
return parent::allowToPatch($request, $payload);
}
public function getStoringRules(RestifyRequest $request): array
{
return $this->collectFields($request)->mapWithKeys(static fn (Field $k) => [
($k->label ?? $k->attribute) => $k->getStoringRules(),
])->toArray();
}
public function collectFields(RestifyRequest $request): FieldCollection
{
$fields = parent::collectFields($request);
if ($request->isUpdateRequest()) {
return $fields->map(static function (Field $field) {
if (!($field instanceof BelongsTo)) {
// Fix to allow updating fields with custom labels
$field->label = $field->attribute;
}
return $field;
});
}
return $fields;
}
/**
* Adapt JSON:API request to Restify request.
*/
protected function adaptJsonApiRequest(RestifyRequest $request, bool $snake_attributes = false): void
{
/** @var array<string, mixed> $attributes */
$attributes = $request->input('data.attributes') ?? [];
// Convert all keys to snake_case using Collections
if ($snake_attributes) {
$attributes = collect($attributes)
->mapWithKeys(static fn ($value, $key) => [Str::snake($key) => $value])
->toArray();
}
$relationships = $request->input('data.relationships') ?? [];
// Get relationships in form of "relationshipName" → relationship_id
$relationships = array_map(static fn (array $relationship): int => Arr::get($relationship, 'data.id'), $relationships);
$request->replace([
...$attributes,
...$relationships,
]);
}
Actually, it supports:
- Post (Store)
- Patch/update
- Labels
- Different attribute casing
- One to one/HasOne relationships (TBC)
from laravel-restify.
That's a good idea to include support. Thanks @maicol07 , will make sure we prioritize this.
from laravel-restify.
That's a good idea to include support. Thanks @maicol07 , will make sure we prioritize this.
By the way, we'll also how to figure out where to put pivot fields (I've read something on discuss.jsonapi.org where they indicate meta inside the the relationship)
from laravel-restify.
Related Issues (20)
- How to get the validate errors in repository and response errors. HOT 1
- Store on a repository ignores the $fillable on the Model HOT 1
- Unauthorized to view repository products. Check \"allowRestify\" policy. HOT 1
- GET /users is always public - it ignores the auth:sanctum middleware HOT 3
- "Class \"Binaryk\\LaravelRestify\\Tests\\Fixtures\\User\\User\" not found HOT 3
- Relationships values should always be an array HOT 2
- Docs for media is missing a function call to store the media HOT 1
- Issue action with not an actual database column HOT 1
- Issue in relationship HOT 4
- nullable belongsto relationship HOT 1
- Laravel 10 Support? HOT 2
- Docs say you should use "Post" on "Getters", but you need to use a "Get" HOT 2
- API quietly fails if policy does not exist HOT 2
- spatie/data-transfer-object is abandoned HOT 1
- Run command: route:cache (Error)
- Does not re-give relationships in query type a.b.c HOT 7
- I installed the package and followed the instructions and got an error 404 HOT 1
- unwarranted error in json HOT 1
- RestifyJs can only be accessed if authenticated. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-restify.