avertys/json-api-response

Send an elegant Json response from your api.

v1.0.1 2022-08-19 12:44 UTC

This package is not auto-updated.

Last update: 2024-10-26 21:37:42 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

This package allows you to send elegant json response from your Laravel api.

Installation

You can install the package via composer:

composer require avertys/json-api-response

Usage

Successful response

  return JsonApiResponse::make($data)
        ->withSuccess()
        ->send(200);

Add additional data

  return JsonApiResponse::make($data)
        ->withSuccess()
        ->withAdditionalData([
            'deprecated' => true
        ])
        ->send(200);

Unsuccessful response

  return JsonApiResponse::make()
        ->withErrors($validator->errors())
        ->withAdditionalData([
            'deprecated' => true
        ])
        ->send(200);

Working with pagination

  return JsonApiResponse::make(User::paginate(10))
        ->withSuccess()
        ->send(200);

Response format

{
    "success": true,
    "data": [
        "id": 1,
        "name": "John" 
    ],
    "errors": null,
    "additional_data": [
        "pagination": {
            "current_page": 2,
            "to": 5,
            "total": 100
        },
        "deprecated": false
    ]
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email hoostr.co@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.