2017 © Pedro Peláez
 

library json-api-response

A package for handling JSON API responses

image

giadc/json-api-response

A package for handling JSON API responses

  • Tuesday, September 26, 2017
  • by giadc-dev
  • Repository
  • 3 Watchers
  • 0 Stars
  • 333 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 17 Versions
  • 3 % Grown

The README.md

GIADC JSON Response Package

currently: v0.1, (*1)

This package makes it easy to return HTTP API responses that fit the JSON API standard., (*2)

Installation

Via Composer, (*3)

$ composer require giadc/giadc-json-response

Laravel

Add the following to the providers array of your config/app.php, (*4)

Giadc\JsonApiResponse\ServiceProviders\LaravelDoctrineServiceProvider::class,

Usage

Basic Example:, (*5)

use Giadc\JsonResponse\Responses\Response;

class YourClass {
    _construct(Response $response) {
        $this->response = $response;
    }

    public function jsonApiFunctions() {

        return $this->response->success();
    }
}

Available functions:, (*6)

$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., (*7)

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., (*8)

JsonApiResource

Forces entities to have the following methods:, (*9)

  • 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), (*10)

Excludes

not supported by JsonAPI, (*11)

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., (*12)

Example:, (*13)

/api/export/campaigns?excludes[campaigns]=config,ui, (*14)

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., (*15)

Example: /api/permissionGroups?fields[permissionGroups]=name&include=permissions&fields[permissions]=, (*16)

{
  "data": [
    {
      "type": "permissionGroups",
      "id": "adops",
      "attributes": { "name": "adops" },
      "relationships": {
        "permissions": {
          "data": [{ "type": "permissions", "id": "campaigns.tracking" }]
        }
      }
    },
  ],
  "included": [
    { "type": "permissions", "id": "campaigns.tracking", "attributes": {} }
  ]
}

The Versions

26/09 2017

dev-master

9999999-dev

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

26/09 2017

1.3.0

1.3.0.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

02/05 2017

1.2.1

1.2.1.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

02/05 2017

1.2.0

1.2.0.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

17/01 2017

1.1.0

1.1.0.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

07/12 2016

1.0.2

1.0.2.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

18/11 2016

1.0.1

1.0.1.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

12/08 2016

dev-f-refactor

dev-f-refactor

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

12/08 2016

1.0.0

1.0.0.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

12/08 2016

1.0.0-alpha

1.0.0.0-alpha

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Hill
by Ryan Marganti

11/08 2016

0.1.6

0.1.6.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

12/04 2016

0.1.5

0.1.5.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

07/03 2016

0.1.4

0.1.4.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

07/03 2016

0.1.3

0.1.3.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

07/03 2016

0.1.2

0.1.2.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

29/02 2016

0.1.1

0.1.1.0

A package for handling JSON API responses

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti

29/02 2016

0.1

0.1.0.0

A package for handling JSON API responses with Fractal

  Sources   Download

MIT

The Requires

 

by David Hill
by Ryan Marganti