Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lakuapik committed Aug 12, 2024
1 parent f2c5152 commit b7d6aed
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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`.
6 changes: 6 additions & 0 deletions config/api-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 8 additions & 1 deletion src/ApiExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b7d6aed

Please sign in to comment.