giadc / json-api-response
A package for handling JSON API responses
Installs: 5 782
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: >=8.1
- doctrine/collections: ^1.6
- doctrine/orm: ^2.11
- giadc/json-api-request: 3.0.4
- league/fractal: ^0.20
- symfony/http-kernel: ^7.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpstan/phpstan: ^1.5
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^10
README
currently: v0.1
This package makes it easy to return HTTP API responses that fit the JSON API standard.
Installation
Via Composer
$ composer require giadc/giadc-json-response
Laravel
Add the following to the providers
array of your config/app.php
Giadc\JsonApiResponse\ServiceProviders\LaravelDoctrineServiceProvider::class,
Usage
Basic Example:
use Giadc\JsonResponse\Responses\Response; class YourClass { _construct(Response $response) { $this->response = $response; } public function jsonApiFunctions() { return $this->response->success(); } }
Available functions:
$response->getStatusCode(): int; $response->setStatusCode(int $statusCode); $response->withArray(array $array, array $headers = []): JsonResponse; $response->withError(string $message): JsonResponse; $response->createSuccessful( $entity = null, TransformerAbstract $transformer = null, string $resourceKey = '', array $headers = []): SymfonyResponse; $response->withItem( $item, TransformerAbstract $transformer, string $resourceKey, array $headers = []): JsonResponse; $response->withResourceItem( JsonApiResource $item, ResourceTransformer $transformer, array $headers = []): JsonResponse; $response->withCollection( $collection, TransformerAbstract $transformer, string $resourceKey = ''): SymfonyResponse; $response->noContent(array $headers = []): JsonResponse; $response->withPaginatedCollection(PaginatedCollection $paginator, TransformerAbstract $transformer, string $resourceKey = ''): JsonResponse; $response->withHttpException( HttpExceptionInterface $httpException): JsonResponse; $response->errorForbidden(string $message = 'Forbidden'): JsonResponse; $response->errorInternalError(string $message = 'Internal Error'): JsonResponse; $response->errorNotFound(string $message = 'Not Found'): JsonResponse; $response->errorUnauthorized(string $message = 'Unauthorized'): JsonResponse; $response->errorValidation(string $message = 'Validation Error'): JsonResponse; $response->errorsValidation(array $messages): JsonResponse; $response->errorNotSearchable(string $message = 'Not Searchable'): JsonResponse;
Fractal Transformers
The GIADC JSON Response packages uses league/fractal
for the withItem()
, withCollection()
, & withPaginatedCollection()
responses.
See Fractal's documentation for more information regarding Transformers.
ResourceTransformer
ResourceTransformer adds the required transform
that pulls JsonApiResource's data from the required jsonSerialize
method. It also allows for automatic management of excludes
and fields
request parameters options.
JsonApiResource
Forces entities to have the following methods:
- getResourceKey: We can now reference this instead of hard coding this value very time.
- id: JsonAPI allows requires an id.
- jsonSerialize: Used in the new ResourceTransformer.
Use the JsonApiResource Item response to automatically pass $resourceKey
to the transformer.
$response->withResourceItem($item, $transformer, $headers)
Excludes
not supported by JsonAPI
Attributes may be excluded from JsonApiResource's response. A decent use case for this would be removing the config/ui/tracking from Campaigns when it's not needed. This would allow us to remove some redundant routes, providing the FE with more flexibility.
Example:
/api/export/campaigns?excludes[campaigns]=config,ui
Fields (https://jsonapi.org/format/#fetching-sparse-fieldsets)
An empty value indicates that no fields should be returned. ... If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.
Example:
/api/permissionGroups?fields[permissionGroups]=name&include=permissions&fields[permissions]=
{
"data": [
{
"type": "permissionGroups",
"id": "adops",
"attributes": { "name": "adops" },
"relationships": {
"permissions": {
"data": [{ "type": "permissions", "id": "campaigns.tracking" }]
}
}
},
],
"included": [
{ "type": "permissions", "id": "campaigns.tracking", "attributes": {} }
]
}