DEV Community

kolirt
kolirt

Posted on

Simplifying API responses in Laravel with kolirt/laravel-api-response package

Simplifying API responses in Laravel with kolirt/laravel-api-response package

API responses are a crucial part of any web application, especially when dealing with client-server communications. In Laravel, while handling API responses is straightforward, maintaining a consistent structure across different projects can be challenging. This is where the kolirt/laravel-api-response package comes in handy.

What is kolirt/laravel-api-response?
The kolirt/laravel-api-response package is designed to unify API responses for Laravel projects, drawing inspiration from the well-structured responses of Telegram's API. This package simplifies the process of generating consistent API responses, whether you're returning success or error messages.

Key Features

  • Unified Response Structure: The package ensures that all your API responses follow a consistent structure, which is essential for maintainability and clarity.
  • Error and Success Responses: Easily generate error and success responses with dedicated methods.
  • Flexible and Extensible: Customize your responses with methods to set HTTP status codes, descriptions, errors, and more.

Getting Started
To get started with kolirt/laravel-api-response, ensure your project meets the following requirements:

  • PHP version: >=8.1
  • Laravel version: >= 10

You can install the package via Composer:

composer require kolirt/laravel-api-response
Enter fullscreen mode Exit fullscreen mode

Usage Example
Here’s a quick example of how you can use the kolirt/laravel-api-response package:

Error response

class ExampleController extends Controller
{
    public function index()
    {
        return api()
            ->error(404)
            ->setDescription('Resource not found')
            ->render();
    }

}
Enter fullscreen mode Exit fullscreen mode

Json response

{
    "ok": false,
    "error_code": 404,
    "description": "Resource not found"
}
Enter fullscreen mode Exit fullscreen mode

Validation error response

class ExampleController extends Controller
{
    public function index()
    {
        return api()
            ->error(422)
            ->setErrors([
                'password' => 'Field is required'
            ])
            ->render();
    }

}
Enter fullscreen mode Exit fullscreen mode

Json response

{
    "ok": false,
    "error_code": 422,
    "errors": {
        "password": "Field is required"
    }
}
Enter fullscreen mode Exit fullscreen mode

Success Response

class ExampleController extends Controller
{
    public function store()
    {
        return api()
            ->success()
            ->setDescription('Resource created successfully')
            ->setData(['id' => 123])
            ->render();
    }

}
Enter fullscreen mode Exit fullscreen mode

Json response

{
    "ok": true,
    "description": "Resource created successfully",
    "data": {
        "id": 123
    }
}
Enter fullscreen mode Exit fullscreen mode

Methods Overview
The package provides a set of methods to customize your API responses:

  • error: Generate an error response.
  • success: Generate a success response.
  • setCode: Set the HTTP status code.
  • setDescription: Add a description to the response.
  • setErrors: Include specific error details.
  • abort: Abort the request with a custom response.
  • cookie: Add cookies to the response.
  • setData: Attach data to the success response.
  • render: Render the final response.

Conclusion
The Laravel Api Response package is a powerful tool for Laravel developers looking to streamline their API response handling. By unifying the structure of responses, it not only improves code readability and consistency but also enhances the overall development experience.

Feel free to explore the package further by visiting its GitHub repository.

Top comments (0)