mhasnainjafri/apitoolkit

This package is abandoned and no longer maintained. The author suggests using the spatie/laravel-query-builder package instead.

APIToolkit is a comprehensive Laravel package designed to streamline and enhance API development by providing consistent responses, efficient pagination, robust caching mechanisms, and standardized field definitions with appropriate status codes. Simplify your API development process and ensure a un

v0.1.1 2024-06-18 09:10 UTC

This package is auto-updated.

Last update: 2024-12-11 07:29:56 UTC


README

APITOOLKIT is a comprehensive package for Laravel that provides powerful tools for building queries, handling API responses, and managing caching efficiently.

Installation

You can install the package via composer:

composer require mhasnainjafri/apitoolkit

Features

APITOOLKIT provides a variety of features for building and handling API responses efficiently:

  • Query Building: Construct queries with filters, sorts, includes, and more.
  • API Response Helpers: Standardize API responses.
  • Caching: Cache API responses for a specified duration.
  • Custom Responses: Standardized methods for success, error, validation, and other types of responses.

Usage

Query Building

APITOOLKIT helps you build complex queries easily.

use Mhasnainjafri\APIToolkit\QueryBuilder\QueryBuilder;

// Example usage
$users = QueryBuilder::for(User::class)
    ->allowedFilters(['name', 'email'])
    ->allowedSorts(['name', 'created_at'])
    ->paginate();

API Responses

APITOOLKIT offers various methods to handle API responses efficiently.

Success Response

use Mhasnainjafri\APIToolkit\API;

return API::success($data, 'Data retrieved successfully');

Error Response

use Mhasnainjafri\APIToolkit\API;

return API::error('An error occurred', 500);

Validation Error Response

use Mhasnainjafri\APIToolkit\API;

$errors = ['email' => 'The email field is required.'];
return API::validationError($errors);

Not Found Response

use Mhasnainjafri\APIToolkit\API;

return API::notFound('User not found');

Caching

You can cache responses to improve performance and reduce load on your database.

Cached Response

use Mhasnainjafri\APIToolkit\API;
use App\Models\User;

$resource = User::query();
$cacheKey = 'users_list';

return API::cachedResponse($resource, $cacheKey);

Paginated Cached Response

use Mhasnainjafri\APIToolkit\API;
use App\Models\User;

$resource = User::query();
$pageNumber = 1;

return API::paginatedCachedResponse($resource, $pageNumber);

Clearing Cache

use Mhasnainjafri\APIToolkit\API;

$cacheKey = 'users_list';
API::clearCacheKey($cacheKey);

Custom Responses

You can also create custom responses as needed.

use Mhasnainjafri\APIToolkit\API;

$data = ['key' => 'value'];
return API::custom($data, 'Custom response message');

Constants

APITOOLKIT provides various HTTP status codes as constants for convenience:

  • API::SUCCESS: 200
  • API::CREATED: 201
  • API::NO_CONTENT: 204
  • API::BAD_REQUEST: 400
  • API::UNAUTHORIZED: 401
  • API::FORBIDDEN: 403
  • API::NOT_FOUND: 404
  • API::METHOD_NOT_ALLOWED: 405
  • API::UNPROCESSABLE_ENTITY: 422
  • API::INTERNAL_SERVER_ERROR: 500
  • API::NOT_IMPLEMENTED: 501
  • API::BAD_GATEWAY: 502
  • API::SERVICE_UNAVAILABLE: 503

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

License

This package is open-sourced software licensed under the MIT license.