Skip to content

Commit b7d6aed

Browse files
committed
wip
1 parent f2c5152 commit b7d6aed

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,39 @@ Example:
4040
## Install
4141

4242
```sh
43-
$ composer require kodepandai/laravel-api-response:^2.0
43+
$ composer require kodepandai/laravel-api-response:dev-beta
4444
```
4545

4646
**Requirements:**
4747
* PHP ^8.1
4848
* Laravel ^10.0
4949

50+
**Laravel ^11**
51+
52+
After installation, register api response handler in `bootstrap/app.php`
53+
54+
```php
55+
use KodePandai\ApiResponse\ApiExceptionHandler;
56+
57+
return Application::configure(basePath: dirname(__DIR__))
58+
//...
59+
->withExceptions(function (Exceptions $exceptions) {
60+
// dont report any api response exception
61+
$exceptions->dontReport([
62+
\KodePandai\ApiResponse\Exceptions\ApiException::class,
63+
\KodePandai\ApiResponse\Exceptions\ApiValidationException::class,
64+
]);
65+
// api response exception handler for /api
66+
$exceptions->renderable(function (Throwable $e, Request $request) {
67+
if ($request->wantsJson() || $request->is('*api*')) {
68+
return ApiExceptionHandler::render($e, $request);
69+
}
70+
});
71+
});
72+
```
73+
74+
**Laravel ^10**
75+
5076
After installation, register api response handler in `app/Exceptions/Handler.php`
5177

5278
```php
@@ -70,7 +96,7 @@ class Handler extends ExceptionHandler
7096
}
7197
```
7298

73-
The above handler will automatically transform any exception and render as ApiResponse.
99+
The above handler will automatically transform any exception and render as ApiResponse json response.
74100

75101
## Config
76102

@@ -82,10 +108,20 @@ $ php artisan vendor:publish --tag=api-response-config
82108

83109
## Usage
84110

111+
TODO
112+
85113
### Return Response
86114

115+
TODO
116+
87117
### Throw Exception
88118

119+
TODO
120+
121+
### Overriding response structure
122+
123+
TODO
124+
89125
## Develop
90126

91127
- To test run `composer test`.

config/api-response.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
\Illuminate\Validation\ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
2828
],
2929

30+
/**
31+
* Transform \Illuminate\Validation\ValidationException response
32+
* into ApiValidationException response to standarize the error.
33+
*/
34+
'transform_validation_exception' => true,
35+
3036
/**
3137
* Debugging options
3238
*/

src/ApiExceptionHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Validation\ValidationException;
99
use KodePandai\ApiResponse\Exceptions\ApiException;
1010
use KodePandai\ApiResponse\Exceptions\ApiValidationException;
11+
use KodePandai\ApiResponse\Facades\ApiResponse;
1112
use ReflectionMethod;
1213
use Symfony\Component\HttpFoundation\JsonResponse;
1314
use Symfony\Component\HttpFoundation\Response;
@@ -23,10 +24,16 @@ public static function render(Throwable $e, ?Request $request = null)
2324
{
2425
$request = $request ?: app(Request::class);
2526

26-
if ($e instanceof ApiException || $e instanceof ApiValidationException) {
27+
if ($e instanceof ApiException
28+
|| $e instanceof ApiValidationException) {
2729
return $e->toResponse($request);
2830
}
2931

32+
if ($e instanceof ValidationException
33+
&& config('api-response.transform_validation_exception')) {
34+
return ApiResponse::unprocessable()->errors($e->errors());
35+
}
36+
3037
if ($e instanceof Responsable) {
3138
if ($e->toResponse($request) instanceof ApiResponseContract) {
3239
return $e->toResponse($request);

src/Facades/ApiResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @method static \KodePandai\ApiResponse\ApiResponse forbidden(mixed $errors = [])
1515
* @method static \KodePandai\ApiResponse\ApiResponse badRequest(mixed $errors = [])
1616
* @method static \KodePandai\ApiResponse\ApiResponse invalid(string $key, string|array $messages)
17-
* @method static \KodePandai\ApiResponse\ApiResponse validateOrFail(array $rules, array $messages = [], array $customAttributes = [], ?\Illuminate\Http\Request $request = null): array
17+
* @method array \KodePandai\ApiResponse\ApiResponse validateOrFail(array $rules, array $messages = [], array $customAttributes = [], ?\Illuminate\Http\Request $request = null)
1818
* @method \KodePandai\ApiResponse\ApiResponse statusCode(int $code)
1919
* @method \KodePandai\ApiResponse\ApiResponse successful()
2020
* @method \KodePandai\ApiResponse\ApiResponse notSuccessful()

0 commit comments

Comments
 (0)