|
1 |
| -# Laravel API Response (Next version) |
| 1 | +# Laravel API Response v2 |
2 | 2 |
|
3 |
| -> This version is still WORK IN PROGRESS. |
| 3 | +A helper package to return JSON Api Response in structured way. |
4 | 4 |
|
5 |
| -This package aims to help you standardize all your API responses in |
6 |
| -a simple and structured way. |
7 |
| - |
8 |
| -By default, the structure of the API response will look like this: |
| 5 | +By default, the structure of the response will look like this: |
9 | 6 |
|
10 | 7 | ```jsonc
|
11 | 8 | {
|
12 | 9 | "success": true, // it was successfull or not
|
13 | 10 | "title": "Users", // the title/headline/section
|
14 |
| - "message": "Active users only", // the message/description/hightlight |
15 |
| - "data": [ // if it was successfull |
| 11 | + "message": "Active users only", // the message/description/highlight |
| 12 | + "data": { // if it was successfull |
16 | 13 | // profile..
|
17 | 14 | // users..
|
18 | 15 | // products..
|
19 | 16 | // etc..
|
20 |
| - ], |
21 |
| - "errors": [ // if it was not successfull |
| 17 | + }, |
| 18 | + "errors": { // if it was not successfull |
22 | 19 | // validation errors..
|
23 | 20 | // any other errors..
|
24 |
| - ] |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Example: |
| 26 | + |
| 27 | +```jsonc |
| 28 | +{ |
| 29 | + "success": true, |
| 30 | + "title": "Users", |
| 31 | + "message": "Succesfully create a user", |
| 32 | + "data": { |
| 33 | + "id": 1, |
| 34 | + "name": "John Doe", |
| 35 | + "address": "4th Semarang Raya", |
| 36 | + }, |
25 | 37 | }
|
26 | 38 | ```
|
27 | 39 |
|
28 | 40 | ## Install
|
29 | 41 |
|
30 | 42 | ```sh
|
31 |
| -$ composer require kodepandai/laravel-api-response:^2 |
| 43 | +$ composer require kodepandai/laravel-api-response:^2.0 |
| 44 | +``` |
| 45 | + |
| 46 | +**Requirements:** |
| 47 | +* PHP ^8.1 |
| 48 | +* Laravel ^10.0 |
| 49 | + |
| 50 | +After installation, register api response handler in `app/Exceptions/Handler.php` |
| 51 | + |
| 52 | +```php |
| 53 | +use KodePandai\ApiResponse\ApiExceptionHandler; |
| 54 | + |
| 55 | +class Handler extends ExceptionHandler |
| 56 | +{ |
| 57 | + protected $dontReport = [ |
| 58 | + \KodePandai\ApiResponse\Exceptions\ApiException::class, |
| 59 | + \KodePandai\ApiResponse\Exceptions\ApiValidationException::class, |
| 60 | + ]; |
| 61 | + |
| 62 | + public function register() |
| 63 | + { |
| 64 | + $this->renderable(function (Throwable $e, Request $request) { |
| 65 | + if ($request->wantsJson() || $request->is('*api*')) { |
| 66 | + return ApiExceptionHandler::render($e, $request); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +The above handler will automatically transform any exception and render as ApiResponse. |
| 74 | + |
| 75 | +## Config |
| 76 | + |
| 77 | +Publish config file using vendor:publish command |
| 78 | + |
| 79 | +```sh |
| 80 | +$ php artisan vendor:publish --tag=api-response-config |
32 | 81 | ```
|
33 | 82 |
|
34 | 83 | ## Usage
|
35 | 84 |
|
36 |
| -TODO: complete this documentation, simplify the wordings. |
| 85 | +### Return Response |
| 86 | + |
| 87 | +### Throw Exception |
37 | 88 |
|
38 | 89 | ## Develop
|
39 | 90 |
|
|
0 commit comments