diff --git a/README.md b/README.md index d3057f2..bb03fd1 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,39 @@ Example: ## Install ```sh -$ composer require kodepandai/laravel-api-response:^2.0 +$ composer require kodepandai/laravel-api-response:dev-beta ``` **Requirements:** * PHP ^8.1 * Laravel ^10.0 +**Laravel ^11** + +After installation, register api response handler in `bootstrap/app.php` + +```php +use KodePandai\ApiResponse\ApiExceptionHandler; + +return Application::configure(basePath: dirname(__DIR__)) + //... + ->withExceptions(function (Exceptions $exceptions) { + // dont report any api response exception + $exceptions->dontReport([ + \KodePandai\ApiResponse\Exceptions\ApiException::class, + \KodePandai\ApiResponse\Exceptions\ApiValidationException::class, + ]); + // api response exception handler for /api + $exceptions->renderable(function (Throwable $e, Request $request) { + if ($request->wantsJson() || $request->is('*api*')) { + return ApiExceptionHandler::render($e, $request); + } + }); + }); +``` + +**Laravel ^10** + After installation, register api response handler in `app/Exceptions/Handler.php` ```php @@ -70,7 +96,7 @@ class Handler extends ExceptionHandler } ``` -The above handler will automatically transform any exception and render as ApiResponse. +The above handler will automatically transform any exception and render as ApiResponse json response. ## Config @@ -82,10 +108,20 @@ $ php artisan vendor:publish --tag=api-response-config ## Usage +TODO + ### Return Response +TODO + ### Throw Exception +TODO + +### Overriding response structure + +TODO + ## Develop - To test run `composer test`. diff --git a/config/api-response.php b/config/api-response.php index 747add2..a394204 100644 --- a/config/api-response.php +++ b/config/api-response.php @@ -27,6 +27,12 @@ \Illuminate\Validation\ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY, ], + /** + * Transform \Illuminate\Validation\ValidationException response + * into ApiValidationException response to standarize the error. + */ + 'transform_validation_exception' => true, + /** * Debugging options */ diff --git a/src/ApiExceptionHandler.php b/src/ApiExceptionHandler.php index 816aec1..8041139 100644 --- a/src/ApiExceptionHandler.php +++ b/src/ApiExceptionHandler.php @@ -8,6 +8,7 @@ use Illuminate\Validation\ValidationException; use KodePandai\ApiResponse\Exceptions\ApiException; use KodePandai\ApiResponse\Exceptions\ApiValidationException; +use KodePandai\ApiResponse\Facades\ApiResponse; use ReflectionMethod; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -23,10 +24,16 @@ public static function render(Throwable $e, ?Request $request = null) { $request = $request ?: app(Request::class); - if ($e instanceof ApiException || $e instanceof ApiValidationException) { + if ($e instanceof ApiException + || $e instanceof ApiValidationException) { return $e->toResponse($request); } + if ($e instanceof ValidationException + && config('api-response.transform_validation_exception')) { + return ApiResponse::unprocessable()->errors($e->errors()); + } + if ($e instanceof Responsable) { if ($e->toResponse($request) instanceof ApiResponseContract) { return $e->toResponse($request); diff --git a/src/Facades/ApiResponse.php b/src/Facades/ApiResponse.php index a92d11d..a5783f0 100644 --- a/src/Facades/ApiResponse.php +++ b/src/Facades/ApiResponse.php @@ -14,7 +14,7 @@ * @method static \KodePandai\ApiResponse\ApiResponse forbidden(mixed $errors = []) * @method static \KodePandai\ApiResponse\ApiResponse badRequest(mixed $errors = []) * @method static \KodePandai\ApiResponse\ApiResponse invalid(string $key, string|array $messages) - * @method static \KodePandai\ApiResponse\ApiResponse validateOrFail(array $rules, array $messages = [], array $customAttributes = [], ?\Illuminate\Http\Request $request = null): array + * @method array \KodePandai\ApiResponse\ApiResponse validateOrFail(array $rules, array $messages = [], array $customAttributes = [], ?\Illuminate\Http\Request $request = null) * @method \KodePandai\ApiResponse\ApiResponse statusCode(int $code) * @method \KodePandai\ApiResponse\ApiResponse successful() * @method \KodePandai\ApiResponse\ApiResponse notSuccessful()