faresnassar09/api-vault

A fluent Laravel package for standardized API responses, smart pagination, and page-aware caching.

Maintainers

Package info

github.com/faresnassar09/faresnassar09-api-vault

pkg:composer/faresnassar09/api-vault

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-02-13 08:09 UTC

This package is auto-updated.

Last update: 2026-02-13 08:12:02 UTC


README

Run Tests

🔐 API Vault

API Vault is a lightweight Laravel utility for building clean, consistent, and chainable API responses with optional caching, callbacks, headers, and response customization.

It helps you avoid repetitive response logic and keeps your controllers clean and readable.

✨ Features

  • Fluent method chaining for API responses
  • Optional caching support
  • Lazy data execution using callbacks (with or without caching)
  • Unified response structure
  • Custom headers & JSON options support
  • Clean and expressive syntax

📦 Installation

composer require faresnassar09/api-vault

🚀 Usage Examples

1️⃣ Basic data response

use FaresNassar\ApiVault\Formatter;

$formatter = new Formatter();

return $formatter
    ->success(true)
    ->message('Users Retrieved Successfully')
    ->data(User::all())
    ->code(200)
    ->send();

2️⃣ Using callback() with caching

return $formatter
    ->success(true)
    ->message('Users Retrieved And Cached Successfully')
    ->cache('users_cache_key', 600)
    ->callback(fn () => User::where('id', '<', 10000)->get())
    ->code(200)
    ->send();

3️⃣ Using callback() without caching

return $formatter
    ->success(true)
    ->message('Users Retrieved Successfully')
    ->callback(fn () => User::all()) // lazy evaluation, no caching
    ->code(200)
    ->send();

4️⃣ Sending additional meta data

return $formatter
    ->success(true)
    ->message('Users Retrieved Successfully')
    ->data(User::all())
    ->additional([
        'cached' => false,
        'execution_time' => '12ms',
        'debug' => true
    ])
    ->send();

5️⃣ Custom headers and JSON options

return $formatter
    ->success(true)
    ->message('Custom Response')
    ->data($data)
    ->headers([
        'X-App-Version' => '1.0.0'
    ])
    ->jsonOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
    ->send();

🛠️ Available Methods

  • success() → Set response status (true || false)
  • message() → Set response message
  • data() → Set response data directly
  • cache() → Enable caching (key, seconds)
  • callback() → Lazy data execution (can be used with or without caching)
  • code() → HTTP status code (default: 200)
  • additional() → Set extra meta data
  • headers() → Custom response headers
  • jsonOptions() → Set JSON encoding options (int)
  • send() → Return the final response

📄 Example JSON Response

{
  "success": true,
  "message": "Users Retrieved Successfully",
  "data": [
    {"id": 1, "name": "Fares Ahmed"},
    {"id": 2, "name": "Ali Mohamed"}
  ],

  "code": 200,
  "additional": {
    "cached": false,
    "execution_time": "12ms",
    "debug": true
  }
}

📄 License

MIT License

👨‍💻 Author

Fares Nassar
GitHub: https://github.com/faresnassar09
Email: fares.ahmed.nassar0@gmail.com

Built for clean APIs, not messy controllers 🚀