creativecrafts / laravel-api-response
A simple package to have a consistent api response.
1.0.0
2024-03-17 05:58 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^11.0|^10.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.13
- nunomaduro/collision: ^7.10|^8.0
- orchestra/testbench: ^8.20|^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
A simple handy package to have a consistent api response.
Installation
You can install the package via composer:
composer require creativecrafts/laravel-api-response
Usage
// respond with success $message = 'Success message'; $return LaravelApi::successResponse($message); // respond with success and data $message = 'Success message'; $data = ['name' => 'test']; $return LaravelApi::successResponse($message, $data); // respond with created $data = [ 'id' => 1, 'name' => 'Test', ] $response = LaravelApi::createdResponse($data); // respond with exception. Exception is optional and will only be used in local or development environment $exception = new Exception('Test exception'); $message = 'Internal server error'; $errorCodes = 5001; $statusCode = 500; return LaravelApi::errorResponse($message, $statusCode, $exception, $errorCodes); // respond with error $message = 'Missing required parameters'; $statusCode = 406; return LaravelApi::errorResponse($message, $statusCode); // app/Exceptions/Handler.php can be modified to return the response public function render($request, Throwable $e): Response|JsonResponse|ResponseAlias { if ($request->expectsJson()) { if ($e instanceof PostTooLargeException) { return LaravelApi::errorResponse("Size of attached file should be less " . ini_get("upload_max_filesize") . "B", ResponseAlias::HTTP_REQUEST_ENTITY_TOO_LARGE, $e); } if ($e instanceof ValidationException) { return LaravelApi::errorResponse($e->validator->errors()->first(), ResponseAlias::HTTP_UNPROCESSABLE_ENTITY, $e); } if ($e instanceof ModelNotFoundException) { return LaravelApi::errorResponse('Entry for ' . str_replace('App\\', '', $e->getModel()) . ' not found', ResponseAlias::HTTP_NOT_FOUND, $e); } if ($e instanceof AuthenticationException) { return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_UNAUTHORIZED, $e); } if ($e instanceof AuthorizationException) { return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_FORBIDDEN, $e); } if ($e instanceof ThrottleRequestsException) { return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_TOO_MANY_REQUESTS, $e); } if ($e instanceof Exception) { return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_INTERNAL_SERVER_ERROR, $e); } if ($e instanceof Error) { return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_INTERNAL_SERVER_ERROR); } } return parent::render($request, $e);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.