From 59a471584bf352f6b29751a0cb89e3acc08b6b08 Mon Sep 17 00:00:00 2001 From: Suraj Datheputhe <3165404-surajdatheputhe@users.noreply.gitlab.com> Date: Tue, 26 Nov 2024 16:37:22 +0545 Subject: [PATCH] [1.x.x] remove unprocessable method --- README.md | 20 -------------------- src/Facades/Api.php | 3 +-- src/Http/ApiResponse.php | 39 +++++++++++---------------------------- tests/ApiResponseTest.php | 18 +----------------- 4 files changed, 13 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 9e3b041..7d31cc5 100644 --- a/README.md +++ b/README.md @@ -303,26 +303,6 @@ public function login() } ``` -#### `unprocessable` -Generates a unprocessable response with HTTP status code 422. - -`unprocessable(string $message, mixed $errors = [])` - -```php -$errors = ['details' => 'Server was unable to process the request']; - -return api()->unprocessable($errors); - -// Result -{ - "success": false, - "message": "Server was unable to process the request", - "errors": { - "details": "Invalid request data" - } -} -``` - #### `serverError` Returns an error response with HTTP status code 4xx. diff --git a/src/Facades/Api.php b/src/Facades/Api.php index c18bc6a..067c6e3 100644 --- a/src/Facades/Api.php +++ b/src/Facades/Api.php @@ -9,13 +9,12 @@ * @method static \Illuminate\Http\JsonResponse success(string $message, mixed $data = []) * @method static \Illuminate\Http\JsonResponse paginate(string $message, mixed $data = []) * @method static \Illuminate\Http\JsonResponse created(string $message, mixed $data = []) - * @method static \Illuminate\Http\JsonResponse error(string $message, int $status = 400) + * @method static \Illuminate\Http\JsonResponse error(string $message, int $status = 400, mixed $errors = []) * @method static \Illuminate\Http\JsonResponse unauthorized(string $message) * @method static \Illuminate\Http\JsonResponse forbidden(string $message) * @method static \Illuminate\Http\JsonResponse notFound(string $message) * @method static \Illuminate\Http\JsonResponse notAllowed(string $message) * @method static \Illuminate\Http\JsonResponse validation(string $message, mixed $errors = []) - * @method static \Illuminate\Http\JsonResponse unprocessable(string $message, mixed $errors = []) * @method static \Illuminate\Http\JsonResponse serverError(string $message, int $status = 500) * * @see \SurazDott\ApiResponse\Http\ApiResponse diff --git a/src/Http/ApiResponse.php b/src/Http/ApiResponse.php index 4a8c79e..52091c1 100644 --- a/src/Http/ApiResponse.php +++ b/src/Http/ApiResponse.php @@ -23,12 +23,9 @@ public function response(string $message, mixed $data = [], int $status = 200): $response = [ 'success' => $status >= 200 && $status < 300, 'message' => $message, + 'data' => $data ]; - if (! empty($data)) { - $response['data'] = $data; - } - return $this->toJson($response, $status); } @@ -40,12 +37,9 @@ public function success(string $message, mixed $data = []): JsonResponse $response = [ 'success' => true, 'message' => $message, + 'data' => $data, ]; - if (! empty($data)) { - $response['data'] = $data; - } - return $this->toJson($response, 200); } @@ -88,12 +82,18 @@ public function created(string $message, mixed $data = []): JsonResponse /** * Method to return a error response. */ - public function error(string $message, int $status = 400): JsonResponse + public function error(string $message, int $status = 400, mixed $errors = []): JsonResponse { - return $this->toJson([ + $response = [ 'success' => false, 'message' => $message, - ], $status); + ]; + + if (! empty($errors)) { + $response['errors'] = $errors; + } + + return $this->toJson($response, $status); } /** @@ -152,23 +152,6 @@ public function validation(string $message, mixed $errors = []): JsonResponse ], 422); } - /** - * Method to return a unprocessable response. - */ - public function unprocessable(string $message, mixed $errors = []): JsonResponse - { - $response = [ - 'success' => false, - 'message' => $message, - ]; - - if (! empty($errors)) { - $response['errors'] = $errors; - } - - return $this->toJson($response, 422); - } - /** * Method to return a server error response. */ diff --git a/tests/ApiResponseTest.php b/tests/ApiResponseTest.php index 89d4d97..2a8c204 100644 --- a/tests/ApiResponseTest.php +++ b/tests/ApiResponseTest.php @@ -185,23 +185,6 @@ public function test_it_returns_a_validation_error_response(): void ], $response->getData(true)); } - #[Test] - public function test_it_returns_an_unprocessable_response(): void - { - $message = 'Unprocessable'; - $errors = ['error' => 'details']; - - $response = Api::unprocessable($message, $errors); - - $this->assertInstanceOf(JsonResponse::class, $response); - $this->assertEquals(422, $response->status()); - $this->assertEquals([ - 'success' => false, - 'message' => $message, - 'errors' => $errors, - ], $response->getData(true)); - } - #[Test] public function test_it_returns_a_server_error_response(): void { @@ -231,6 +214,7 @@ public function test_it_returns_a_server_error_response_when_using_response(): v $this->assertEquals([ 'success' => false, 'message' => $message, + 'data' => [], ], $response->getData(true)); } }