Skip to content

Commit f3669bd

Browse files
committed
Add some additional docs
1 parent 3e76543 commit f3669bd

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

docs/attributes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,13 @@ Attribute::make('name')->type(
124124
#### `enum`
125125

126126
You can restrict the string to a set of possible values using the `enum` method.
127+
You can provide an array of strings or an array of
128+
[Backed Enum](https://www.php.net/manual/en/language.enumerations.backed.php)
129+
cases:
127130

128131
```php
129132
Attribute::make('status')->type(Str::make()->enum(['to do', 'doing', 'done']));
133+
Attribute::make('status')->type(Str::make()->enum(MyEnum::cases()));
130134
```
131135

132136
#### `pattern`

docs/context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Context
6363
public function sparseFields(Resource $resource): array;
6464

6565
// Determine whether a field has been requested in a sparse fieldset
66-
public function fieldRequested(string $type, string $field, bool $default = true): bool;
66+
public function fieldRequested(string $type, string $field): bool;
6767

6868
// Determine whether a sort field has been requested
6969
public function sortRequested(string $field): bool;

docs/fields.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ Attribute::make('firstName')->serialize(
140140
);
141141
```
142142

143+
### Sparse Fieldsets
144+
145+
[Sparse fieldsets](https://jsonapi.org/format/#fetching-sparse-fieldsets) are
146+
supported on endpoints as per the specification. You can exclude a field from
147+
the fieldset by default, so that it must be explicitly requested, by using the
148+
`sparse` method:
149+
150+
```php
151+
Attribute::make('firstName')->sparse();
152+
```
153+
143154
## Writing
144155

145156
By default, fields are read-only. You can allow a field to be written to in the

docs/resources.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,40 @@ For Laravel applications with Eloquent-backed resources, you can extend the
164164
these interfaces for you. Learn more on the
165165
[Laravel Integration](laravel.md#eloquent-resources) page.
166166
:::
167+
168+
### Collection & Resource Actions
169+
170+
In addition to the standard RESTful endpoints, you can create custom "action"
171+
endpoints for collections (`CollectionAction`) and resources (`ResourceAction`).
172+
173+
When instantiating these classes, pass the name of the action to be used in the
174+
route path, and a handler which will receive the current [context](context.md)
175+
and optionally return a response.
176+
177+
Collection actions will return a `204 No Content` response if nothing is
178+
returned by the handler. Resource actions will return a `200 OK` response
179+
including the resource data.
180+
181+
```php
182+
use Tobyz\JsonApiServer\Context;
183+
use Psr\Http\Message\ResponseInterface;
184+
185+
Endpoint\CollectionAction::make('test', function (
186+
Context $context,
187+
): ?ResponseInterface {
188+
// ...
189+
});
190+
191+
Endpoint\ResourceAction::make('test', function (
192+
Context $context,
193+
): ?ResponseInterface {
194+
// ...
195+
});
196+
```
197+
198+
The `POST` method is used by default, but you can customize this using the
199+
`method` method:
200+
201+
```php
202+
Endpoint\CollectionAction::make(...)->method('PUT');
203+
```

0 commit comments

Comments
 (0)