mhasnainjafri / apitoolkit
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
Requires
- php: ^8.0|^8.2|^8.3
- illuminate/database: ^10.0|^11.0
- illuminate/http: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- ext-json: *
- mockery/mockery: ^1.4
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^2.0
- spatie/invade: ^2.0
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
: 200API::CREATED
: 201API::NO_CONTENT
: 204API::BAD_REQUEST
: 400API::UNAUTHORIZED
: 401API::FORBIDDEN
: 403API::NOT_FOUND
: 404API::METHOD_NOT_ALLOWED
: 405API::UNPROCESSABLE_ENTITY
: 422API::INTERNAL_SERVER_ERROR
: 500API::NOT_IMPLEMENTED
: 501API::BAD_GATEWAY
: 502API::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.