diff --git a/CHANGELOG.md b/CHANGELOG.md index a5ccf76..6de4d0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 5fc1d8c..7f8c9f5 100644 --- a/README.md +++ b/README.md @@ -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); } }); @@ -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); } @@ -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 @@ -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 }); } @@ -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 } diff --git a/src/ApiResponse.php b/src/ApiResponse.php index baa17e4..c567689 100644 --- a/src/ApiResponse.php +++ b/src/ApiResponse.php @@ -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); } @@ -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 { @@ -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 {