-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters