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

Commit 9839290

Browse files
committed
Update README
1 parent 8ef2db7 commit 9839290

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

README.md

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
Integrate JSON:API resources on Laravel.
88

9+
## Features
10+
11+
- Compatible and tested with all the Laravel LTS supported versions ([see them here](https://laravel.com/docs/master/releases#support-policy))
12+
- Full formatting using pure built-in model methods and properties.
13+
- Relationships and nested working with [eager loading](https://laravel.com/docs/master/eloquent-relationships#eager-loading).
14+
- Permissions "out-of-the-box" authorising each resource view or list of resources.
15+
- Auto-hide not allowed attributes from responses like `user_id` or `post_id`.
16+
- Own testing utilities built-in Laravel's ones to make integration tests easier.
17+
918
## Installation
1019

1120
You can install the package via composer:
@@ -14,7 +23,7 @@ You can install the package via composer:
1423
composer require skore-labs/laravel-json-api
1524
```
1625

17-
### Usage
26+
## Usage
1827

1928
As simple as importing the class `SkoreLabs\JsonApi\Http\Resources\JsonApiCollection` for collections or `SkoreLabs\JsonApi\Http\Resources\JsonApiResource` for resources.
2029

@@ -43,6 +52,34 @@ class UserController extends Controller
4352
}
4453
```
4554

55+
### Custom resource type
56+
57+
To customise the resource type of a model you should do like this:
58+
59+
```php
60+
<?php
61+
62+
namespace SkoreLabs\JsonApi\Tests\Fixtures;
63+
64+
use Illuminate\Database\Eloquent\Model;
65+
use SkoreLabs\JsonApi\Contracts\JsonApiable;
66+
67+
class Post extends Model implements JsonApiable
68+
{
69+
/**
70+
* Get a custom resource type for JSON:API formatting.
71+
*
72+
* @return string
73+
*/
74+
public function resourceType(): string
75+
{
76+
return 'custom-post';
77+
}
78+
}
79+
```
80+
81+
**Note: Just remember to check the allowed types in [the oficial JSON:API spec](https://jsonapi.org/format/#document-member-names).**
82+
4683
### Authorisation
4784

4885
For authorize a resource or collection you'll need the `view` and `viewAny` on the **model policy**, which you can create passing the model to the make command:
@@ -58,12 +95,44 @@ Alternatively, you can pass an authorisation (boolean) to the constructor of the
5895
return new JsonApiResource($user, true);
5996
```
6097

61-
## Features
98+
## Testing
6299

63-
- Full formatting using pure built-in model methods and properties.
64-
- Relationships and nested working with [eager loading](https://laravel.com/docs/master/eloquent-relationships#eager-loading).
65-
- Permissions "out-of-the-box" authorising each resource view or list of resources.
66-
- Auto-hide not allowed attributes from responses like `user_id` or `post_id`.
100+
```php
101+
public function testGetPostApi()
102+
{
103+
$this->get('/posts/1')->assertJsonApi(function (Assert $json) {
104+
$json->hasId(1)
105+
->hasType('post')
106+
->hasAttribute('title', 'My summer vacation');
107+
});
108+
}
109+
```
110+
111+
### Collections
112+
113+
In case of a test that **receives a JSON:API collection** as a response, **the first item will be the defaulted** on all the methods that tests **attributes and relationships**.
114+
115+
In case you want to access a specific item you have the `at` method:
116+
117+
```php
118+
public function testGetPostsListApi()
119+
{
120+
$this->get('/posts')->assertJsonApi(function (Assert $json) {
121+
// We're testing the first post here!
122+
$json->hasId(1)
123+
->hasType('post')
124+
->hasAttribute('title', 'My summer vacation @ Italy');
125+
126+
// Now we want to test the second post of the collection
127+
$json->at(1)
128+
->hasId(2)
129+
->hasType('post')
130+
->hasAttribute('title', 'What is really great for your diet');
131+
});
132+
}
133+
```
134+
135+
**Note: Remember that this method takes in mind the array keys, so the first item is at 0, not 1!**
67136

68137
## Credits
69138

0 commit comments

Comments
 (0)