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

Package for standardizing the responses from the API of your Symfony based applications.

License

Notifications You must be signed in to change notification settings

TheDragonCode/api-response

Repository files navigation

API Response

Package for standardizing the responses from the API of your Symfony based applications.

api response

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

Installation

To get the latest version of API Response, simply require the project using Composer:

$ composer require andrey-helldar/api-response

This command will automatically install the latest version of the package for your environment.

Or you can manually set the required branch, following the table:

Package version PHP version Symfony version Command
^4.0 5.6.9+ ^3.0, ^4.0 composer require andrey-helldar/api-response:^4.0
^4.4.1 5.6.9+ ^3.0, ^4.0, ^5.0 composer require andrey-helldar/api-response:^4.4.1
^5.0 7.1.3+ ^4.0, ^5.0 composer require andrey-helldar/api-response:^4.0

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "andrey-helldar/api-response": "^5.0"
    }
}

If you use a package outside the Laravel framework, you only need to connect the file src/helpers.php for easy use:

require_once 'src/helpers.php';

Alright! Use api_response() helper.

Using

returned NULL with code:

return api_response(null, 304);

returned with code 304:

{
    "data": null
}

returned integer with default code:

return api_response(304);

returned with code 200:

{
    "data": 304
}

returned string with default code:

return api_response('qwerty');

returned with code 200:

{
    "data": "qwerty"
}

returned string with code:

return api_response('qwerty', 400);

returned with code 400:

{
  "error": {
    "code": 400,
    "data": "qwerty"
  }
}

returned integer with code:

return api_response(304, 400);

returned with code 400:

{
  "error": {
    "code": 400,
    "data": 304
  }
}

returned array:

$content = [
    [
        'title' => 'Title #1',
        'description' => 'Description #1',
    ],
    [
        'title' => 'Title #2',
        'description' => 'Description #2',
    ],
];

as error

return api_response($content, 400);

returned with code 400:

{
  "error": {
    "code": 400,
    "data": [
      {
        "title": "Title #1",
        "description": "Description #1"
      },
      {
        "title": "Title #2",
        "description": "Description #2"
      }
    ]
  }
}

as success

return api_response($content, 200);

returned with code 200:

{
    "data": [
        {
            "title": "Title #1",
            "description": "Description #1"
        },
        {
            "title": "Title #2",
            "description": "Description #2"
        }
    ]
}

If the first parameter is a number, then the decryption of the error by code will be returned. In other cases, the value of the passed variable will be returned.

with additional content

return api_response('title', 200, [], ['foo' => 'bar']);

returned with code 200:

{
  "data": "title",
  "foo": "bar"
}

returned with code 400:

{
  "error": {
    "code": 400,
    "data":"ok"
  },
  "foo": "bar"
}

Using in Laravel 5+ framework

To use you need to add three methods to the file app/Exceptions/Handler.php:

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $this->isJson($request)
            ? api_response(__('errors.401', 401))
            : redirect()->guest(route('login'));
    }
    
    protected function invalidJson($request, ValidationException $exception)
    {
        return api_response($exception->errors(), $exception->status ?: 400);
    }
    
    protected function isJson($request): bool
    {
        return $request->expectsJson() || $request->isJson() || $request->is('api/');
    }
}

Copyright and License

API Response was written by Andrey Helldar, and is licensed under the MIT License.

About

Package for standardizing the responses from the API of your Symfony based applications.

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages