Skip to content

Commit a63fa71

Browse files
committed
docs: update documentation
1 parent 06764a5 commit a63fa71

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
All notable changes to `laravel-api-response` will be documented in this file.
44

5-
## [v1.0.0](https://github.com/kodepandai/laravel-indonesia/compare/b5f439...v1.0.0) - 13 Jun 2022
5+
## [v1.1.0](https://github.com/kodepandai/laravel-api-response/compare/v1.0.0...v1.1.0) - 28 Jun 2022
6+
7+
### Fixed
8+
9+
- Missing return type for `ApiException` ([#4](https://github.com/kodepandai/laravel-api-response/pull/4))
10+
11+
### Added
12+
13+
- Return type for `validateOrFail`
14+
- `ExceptionHandler::renderAsApiResponse` helper
15+
16+
## [v1.0.0](https://github.com/kodepandai/laravel-api-response/compare/b5f439...v1.0.0) - 13 Jun 2022
617

718
### Added
819

README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ By default, the stucture of the API response looks like this:
1717
}
1818
```
1919

20-
>**Note**: For now, if you want to customize the response structure, you need to manually extend `ApiResponse`
21-
> class and override `toResponse` or `getResponse` method.
20+
> **Note**: For now, if you want to customize the response structure,
21+
> you need to manually extend `ApiResponse` class and override `toResponse` method.
2222
2323
## Install
2424

@@ -70,8 +70,9 @@ ApiResponse::error()
7070

7171
### Validate or Fail
7272

73-
Use this helper to validate user submitted request then return
73+
Use this helper to validate user submitted request, then return
7474
an `ApiResponse::error` response if the validation fails.
75+
7576
With this helper, now you can use something like `$request->validate()`
7677
without worrying on how to handle the redirect response or validation errors
7778
separately.
@@ -93,8 +94,9 @@ if ($validator->fails()) {
9394

9495
// with this helper
9596

96-
// it will automatically return a json response if validation fails
97-
ApiResponse::validateOrFail([
97+
// it will automatically return a json response if validation fails
98+
// if the validation passes, it will return validated data
99+
$validatedData = ApiResponse::validateOrFail([
98100
'email' => 'required|email',
99101
'username' => 'required|unique:users,username',
100102
]);
@@ -103,7 +105,7 @@ ApiResponse::validateOrFail([
103105
### Throwing an Exception
104106

105107
Instead of using `ApiResponse` manually, you can also throw an exception
106-
and got the same response according to the exception type.
108+
and will get the same response according to the exception type.
107109

108110
This package provides two exception: `ApiException` and `ApiValidationException`.
109111

@@ -120,6 +122,37 @@ if ($user->balance <= 100_000) {
120122
}
121123
```
122124

125+
### Handling all Exception
126+
127+
If you would like to convert all laravel exception to return an ApiResponse,
128+
you can use the `ExceptionHandler::renderAsApiResponse` helper.
129+
130+
```php
131+
// file: Exception/Handler.php
132+
133+
use KodePandai\ApiResponse\ExceptionHandler;
134+
135+
// new laravel (>= 8)
136+
public function register()
137+
{
138+
$this->renderable(function (Throwable $e, $request) {
139+
if ($request->wantsJson()) {
140+
return ExceptionHandler::renderAsApiResponse($e);
141+
}
142+
});
143+
}
144+
145+
// old laravel (<= 7)
146+
public function render($request, Throwable $exception)
147+
{
148+
if ($request->wantsJson()) {
149+
return ExceptionHandler::renderAsApiResponse($exception);
150+
}
151+
152+
return parent::render($request, $exception);
153+
}
154+
```
155+
123156
## Develop
124157

125-
* To test, run `composer test`.
158+
* To test run `composer test`.

0 commit comments

Comments
 (0)