ho3inpgmer/laravel-api-wrapper

A Laravel package for managing API responses with a fluent interface

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/ho3inpgmer/laravel-api-wrapper

v1.0.2 2025-10-24 13:10 UTC

This package is not auto-updated.

Last update: 2026-01-03 12:10:01 UTC


README

A Laravel package for managing API responses with a fluent, elegant interface.

Latest Version on Packagist Total Downloads

Installation

Install via Composer:

composer require ho3inpgmer/laravel-api-wrapper

Configuration

Publish the configuration file (optional):

php artisan vendor:publish --tag=api-wrapper-config

Usage

Success Response

use Ho3inpgmer\ApiWrapper\ApiResponse;

// Basic success response
return ApiResponse::success(['user' => $user]);

// With custom message
return ApiResponse::success($data, 'User created successfully', 201);

// Without data
return ApiResponse::success()->withoutData();

// Add extra fields
return ApiResponse::success($data)
    ->add('meta', ['page' => 1])
    ->add('links', ['next' => 'url']);

Error Response

// Basic error response
return ApiResponse::error(['email' => 'Invalid email']);

// With custom status and message
return ApiResponse::error($errors, 404, 'Resource not found');

// Without errors field
return ApiResponse::error()->withoutErrors();

Method Chaining

return ApiResponse::success($data)
    ->withStatus(201)
    ->add('meta', $meta)
    ->withoutMessage();

Response Format

Success response:

{
    "ok": true,
    "message": "عملیات با موفقیت انجام شد",
    "data": {}
}

Error response:

{
    "ok": false,
    "message": "خطا در انجام عملیات",
    "errors": []
}

Available Methods

  • success($data, $message, $status) - Create a success response
  • error($errors, $status, $message) - Create an error response
  • withoutMessage() - Remove message from response
  • withoutData() - Remove data from response
  • withoutErrors() - Remove errors from response
  • withStatus($status) - Set custom HTTP status code
  • add($key, $value) - Add extra fields to response

Testing

composer test

License

MIT License. Please see License File for more information.

Credits

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.