Skip to content

Commit

Permalink
Defects fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yetakchi committed Jul 26, 2024
1 parent 878413c commit 31957c8
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 97 deletions.
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,82 @@
# Laravel/Api-Responses
# Laravel API Responses

Laravel API Response api uchun moslangan bo‘lib, mijoz javobni JSON shaklida qaytaruvchi va unga shakl berib
jo‘natuvchi kutibxona.

Laravel API Response is a package that helps to provide and render a consistent HTTP JSON responses to API calls as well
as converting and formatting exceptions to JSON responses.

## Requirements
## Talablar (Requirements)

- PHP ^7.4|8.1
- Laravel ^10 | ^11

## Version Compatibility
## Talqinlar mutonosibligi (Version Compatibility)

Laravel | Laravel API Response
:--------|:---------------------
10.x | 1.x
11.x | 1.2.x
| Laravel | Laravel API Response |
|:--------|:---------------------|
| 10.x | 1.x |
| 11.x | 1.2.x |

## Installation
## O‘rnatish (Installation)

Install the package via composer:

```bash
composer require ijodkor/laravel-api-response
```

## Usage
## Ishlatish (Usage)

Add ApiResponse trait to app module Controller file or any controller which is needed
Add RestResponse trait to app module Controller file or any controller which is needed

```php
use Ijodkor\ApiResponse\Responses\RestResponse;

class Controller extends Controller {
use ApiResponse;
use RestResponse;
}

...

class UserController extends Controller {
public function () {
return $this->success([
'user' => new User();
]);
}
}
```

### Bonus
### Mavjuda funksiyalar (Available functions)

| Nomi (name) | Izoh (description) | Status |
|:-------------|:----------------------------------:|-------:|
| success | Muvaffaqiyatli | 200 |
| created | Muvaffaqiyatli | 201 |
| fail | Xatolik yuz berganda | [400] |
| error | Ichki xatolik | 500 |
| unAuthorized | Manzil ruxsat yo‘q | [401] |
| result | Javobda raqam va satrlar moslangan | 200 |
| paginated | Sahiflangan ro‘yxat | 200 |

### Sovg‘a (Bonus)

This package also provided RestRequest to return json response Request validations

```php
use Ijodkor\LaravelApiResponse\Requests\RestRequest;
use Ijodkor\ApiResponse\Requests\RestRequest;

// class UserRequest extends FormRequest - x
class UserRequest extends RestRequest {

}
```

# References
# Foydalanilgan manbalar (References)

- [Testbench](https://packages.tools/testbench) Laravel Testing Helper for Packages Development

# Links
# Foydali havolalar (Links)

- [Create Laravel package](https://laravel-news.com/building-your-own-laravel-packages)
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
{
"role": "Creator",
"name": "anvar",
"email": "anvarindastries@gmail.com"
"email": "mendasturchiman@gmail.com"
}
],
"require": {
"php": "^8.1",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^v10.48.16"
},
"require-dev": {
"orchestra/testbench": "^8.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Requests/RestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Ijodkor\ApiResponse\Requests;

use Ijodkor\ApiResponse\Responses\ApiResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Ijodkor\ApiResponse\Responses\RestResponse;

class RestRequest extends FormRequest {
use ApiResponse;
use RestResponse;

public function failedValidation(Validator $validator) {
throw new HttpResponseException(
Expand Down
75 changes: 0 additions & 75 deletions src/Responses/ApiResponse.php

This file was deleted.

127 changes: 127 additions & 0 deletions src/Responses/RestResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Ijodkor\ApiResponse\Responses;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Ijodkor\ApiResponse\Supporters\ListPaginator;

trait RestResponse {

/**
* Response use any success process
* @param array $data
* @param string $message
* @return JsonResponse
*/
protected function success(array $data = [], string $message = 'Muvaffaqiyatli'): JsonResponse {
return response()->json([
'success' => true,
'data' => [...$data],
'message' => $message
]);
}

/**
* Response use when create a record in system
* @param array $data
* @param string $message
* @return JsonResponse
*/
protected function created(array $data = [], string $message = 'Muvaffaqiyatli'): JsonResponse {
return response()->json([
'success' => true,
'data' => [...$data],
'message' => $message
], 201);
}

/**
* Use when code fall down catch block
* @param mixed $errors
* @param string $message
* @param int $status
* @return JsonResponse
*/
protected function fail($errors = [], string $message = 'Muvaffaqiyatsiz', int $status = 400): JsonResponse {
return response()->json([
'success' => false,
'errors' => $errors,
'message' => $message
], $status);
}

/**
* Use when server error occurs
* @param string $message
* @param array $errors
* @return JsonResponse
*/
protected function error(string $message = 'Ichki xatolik yuz berdi!', array $errors = []): JsonResponse {
return response()->json([
'success' => false,
'errors' => $errors,
'message' => $message
], 500);
}

/**
* Unauthorized response
* @param string $message
* @return JsonResponse
*/
protected function unAuthorized(string $message = "Kirishga ruxsat berilmagan"): JsonResponse {
return response()->json([
'success' => false,
'message' => $message
], 401);
}

// ########## Bonuses ##########

/**
* Returned format number sensible response
* @param mixed $data
* @param string $message
* @return JsonResponse
*/
protected function result($data = [], string $message = 'Muvaffaqiyatli'): JsonResponse {
return response()->json([
'success' => true,
'data' => $data,
'message' => $message
], options: JSON_NUMERIC_CHECK);
}

/**
* Response with no content.
* @return JsonResponse
*/
protected function noContent(): JsonResponse {
return response()->json([], 204);
}

/**
* Returned paginated response
* @param LengthAwarePaginator|ResourceCollection $data
* @param string $key
* @param string|null $message
* @return JsonResponse
*/
protected function paginated($data, string $key, ?string $message = 'Muvaffaqiyatli'): JsonResponse {
if ($data instanceof LengthAwarePaginator) {
$data = new ListPaginator($data, $key);
} elseif ($data instanceof ResourceCollection) {
$data = new ListPaginator($data, $key);
}

return response()->json([
'status' => true,
'data' => [
$key => $data
],
'message' => $message,
]);
}
}
8 changes: 5 additions & 3 deletions src/Supporters/ListPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

class ListPaginator extends LengthAwarePaginator {

/**
* @var string $key
*/
private string $key;

public function __construct(Paginator|ResourceCollection $paginator, string $key = "data") {
$this->key = $key;

$options = [
'path' => $paginator->path()
];
$options = ['path' => $paginator->path()];

parent::__construct($paginator->items(), $paginator->total(), $paginator->perPage(), $paginator->currentPage(), $options);
}

Expand Down

0 comments on commit 31957c8

Please sign in to comment.