This package is in pre-release. The API may change before v1.0.

Installation

Install via Composer

composer require bluebeetlept/api-toolkit

Publish Configuration

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

This creates config/api-toolkit.php with the following defaults:

return [
    'pagination' => [
        'default_size' => 20,
        'max_size'     => 100,
        'valid_sizes'  => [10, 20, 40, 80, 100],
    ],

    'exceptions' => [
        'dont_report' => [],
        'domain'      => [],
    ],

    'openapi' => [
        'title'       => env('APP_NAME', 'API'),
        'version'     => '1.0.0',
        'description' => '',
        'servers'     => [
            ['url' => env('APP_URL', 'http://localhost') . '/api'],
        ],
        'output' => [
            'path'   => public_path('openapi.json'),
            'pretty' => false,
        ],
    ],
];

Exception Handler

To render all exceptions as JSON:API errors, register the handler in bootstrap/app.php:

// bootstrap/app.php
use BlueBeetle\ApiToolkit\Exceptions\ConfigureExceptionHandler;

return Application::configure(basePath: dirname(__DIR__))
    ->withExceptions(new ConfigureExceptionHandler())
    ->create();

Middleware

Add the JSON:API content type middleware to your API routes:

// routes/api.php
use BlueBeetle\ApiToolkit\Http\Middleware\ForceJsonApiResponse;

Route::middleware([ForceJsonApiResponse::class])->group(function () {
    Route::apiResource('products', ProductController::class);
});