Skip to content

Commit

Permalink
Merge pull request #7 from surazdott/fix/methods
Browse files Browse the repository at this point in the history
[1.x.x] remove unprocessable method
  • Loading branch information
surazdott authored Nov 26, 2024
2 parents af85de8 + 59a4715 commit f45e4f4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 67 deletions.
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 1 addition & 2 deletions src/Facades/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 11 additions & 28 deletions src/Http/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
18 changes: 1 addition & 17 deletions tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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));
}
}

0 comments on commit f45e4f4

Please sign in to comment.