Skip to content

Commit

Permalink
chore: update changelog and docs
Browse files Browse the repository at this point in the history
Signed-off-by: David Adi Nugroho <[email protected]>
  • Loading branch information
lakuapik committed Dec 14, 2022
1 parent d112054 commit 4575b76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

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

## [Unreleased](https://github.com/kodepandai/laravel-api-response/compare/v1.2.0...main) - TBD
## [Unreleased](https://github.com/kodepandai/laravel-api-response/compare/v1.3.0...main) - TBD

## [v1.3.0](https://github.com/kodepandai/laravel-api-response/compare/v1.2.0...v1.3.0) - 14 Dec 2022

### Fixed

- Return 404 `NOT_FOUND` for laravel `ModelNotFoundException`
- Laravel exception handler does not recognize Responsable trait ([#11](https://github.com/kodepandai/laravel-api-response/pull/11))
- Return 404 `NOT_FOUND` for laravel `ModelNotFoundException` ([#10](https://github.com/kodepandai/laravel-api-response/pull/10))

### Added

- Support `Arrayable` interface for response data ([#13](https://github.com/kodepandai/laravel-api-response/pull/13))

## [v1.2.0](https://github.com/kodepandai/laravel-api-response/compare/v1.1.0...v1.2.0) - 24 Jul 2022

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ use KodePandai\ApiResponse\ExceptionHandler as ApiExceptionHandler;
public function register()
{
$this->renderable(function (Throwable $e, $request) {
if ($request->wantsJson() || str_contains($request->path(), 'api')) {
if ($request->wantsJson() || $request->is('*api*')) {
return ApiExceptionHandler::renderAsApiResponse($e);
}
});
Expand All @@ -145,7 +145,7 @@ public function register()
// old laravel (<= 7)
public function render($request, Throwable $exception)
{
if ($request->wantsJson() || str_contains($request->path(), 'api')) {
if ($request->wantsJson() || $request->is('*api*')) {
return ApiExceptionHandler::renderAsApiResponse($exception);
}

Expand All @@ -155,8 +155,9 @@ public function render($request, Throwable $exception)

### Handling Exception Manually

If you want to handle all exceptions manually, please not that you must convert `ApiResponse` to `JsonResponse` by call
`toResponse` method explicitly. See this example:
If you want to handle exception manually,
please note that you must convert `ApiResponse` to `JsonResponse`
by calling `toResponse` method explicitly. See this example:

```php
// file: Exception/Handler.php
Expand All @@ -167,7 +168,7 @@ public function register()
$this->renderable(function (AuthenticationException $e, Request $request) {
return ApiResponse::error()
->message('Unauthorized')
->statusCode(401)
->statusCode(Response::HTTP_UNAUTHORIZED)
->toResponse($request); // this part is required
});
}
Expand All @@ -178,7 +179,7 @@ public function render($request, Throwable $exception)
if($exception instanceof AuthenticationException){
return ApiResponse::error()
->message('Unauthorized')
->statusCode(401)
->statusCode(Response::HTTP_UNAUTHORIZED)
->toResponse($request); // this part is required
}

Expand Down
16 changes: 8 additions & 8 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public function addHeaders(array $headers): self
public function toResponse($request): JsonResponse
{
return (new JsonResponse([
'success' => $this->isSuccess,
'title' => $this->title,
'message' => $this->message,
'data' => $this->data,
'errors' => $this->errors,
]))
'success' => $this->isSuccess,
'title' => $this->title,
'message' => $this->message,
'data' => $this->data,
'errors' => $this->errors,
]))
->setStatusCode($this->statusCode)
->withHeaders($this->headers);
}
Expand All @@ -83,7 +83,7 @@ public static function create(): self
/**
* Return a success api response.
*
* @param array|Arrayable|JsonResource|ResourceCollection $data
* @param array|Arrayable|Collection|JsonResource|ResourceCollection $data
*/
public static function success($data = []): self
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public static function validateOrFail(
/**
* Add data to response and transform according to its type.
*
* @param array|Arrayable|JsonResource|ResourceCollection $data
* @param array|Arrayable|Collection|JsonResource|ResourceCollection $data
*/
public function data($data): self
{
Expand Down

0 comments on commit 4575b76

Please sign in to comment.