Skip to content

Commit 20a655a

Browse files
committed
wip
1 parent 171a986 commit 20a655a

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

README.md

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,90 @@
1-
# Laravel API Response (Next version)
1+
# Laravel API Response v2
22

3-
> This version is still WORK IN PROGRESS.
3+
A helper package to return JSON Api Response in structured way.
44

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:
96

107
```jsonc
118
{
129
"success": true, // it was successfull or not
1310
"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
1613
// profile..
1714
// users..
1815
// products..
1916
// etc..
20-
],
21-
"errors": [ // if it was not successfull
17+
},
18+
"errors": { // if it was not successfull
2219
// validation errors..
2320
// 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+
},
2537
}
2638
```
2739

2840
## Install
2941

3042
```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
3281
```
3382

3483
## Usage
3584

36-
TODO: complete this documentation, simplify the wordings.
85+
### Return Response
86+
87+
### Throw Exception
3788

3889
## Develop
3990

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"laravel/framework": "^9.0|^10.0|^11.0"
1515
},
1616
"require-dev": {
17-
"orchestra/testbench": "^7.0|^8.0",
17+
"orchestra/testbench": "^7.0|^8.0|^9.0",
1818
"pestphp/pest": "1.0|^2.0",
1919
"pestphp/pest-plugin-laravel": "^1.0|^2.0"
2020
},

config/api-response.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?php
22

3-
use Illuminate\Auth\AuthenticationException;
4-
use Illuminate\Database\Eloquent\ModelNotFoundException;
53
use Illuminate\Http\Response;
6-
use Illuminate\Validation\ValidationException;
74

85
return [
96

@@ -18,16 +15,16 @@
1815
* - return ApiResponse::error()..
1916
* - throw new ApiException('error')..
2017
*/
21-
'error_status_code' => \Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR,
18+
'error_status_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
2219

2320
/**
2421
* List of exception status codes.
2522
* Override the default status code with custom one.
2623
*/
2724
'exception_status_codes' => [
28-
AuthenticationException::class => Response::HTTP_UNAUTHORIZED,
29-
ModelNotFoundException::class => Response::HTTP_NOT_FOUND,
30-
ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
25+
\Illuminate\Auth\AuthenticationException::class => Response::HTTP_UNAUTHORIZED,
26+
\Illuminate\Database\Eloquent\ModelNotFoundException::class => Response::HTTP_NOT_FOUND,
27+
\Illuminate\Validation\ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
3128
],
3229

3330
/**
@@ -46,9 +43,9 @@
4643
* *Set to false to not show in all status codes.
4744
*/
4845
'show_traces_in_codes' => [
49-
\Illuminate\Http\Response::HTTP_BAD_GATEWAY,
50-
\Illuminate\Http\Response::HTTP_BAD_REQUEST,
51-
\Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR,
46+
Response::HTTP_BAD_GATEWAY,
47+
Response::HTTP_BAD_REQUEST,
48+
Response::HTTP_INTERNAL_SERVER_ERROR,
5249
],
5350

5451
/**

src/Facades/ApiResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method static \KodePandai\ApiResponse\ApiResponse forbidden(mixed $errors = [])
1515
* @method static \KodePandai\ApiResponse\ApiResponse badRequest(mixed $errors = [])
1616
* @method static \KodePandai\ApiResponse\ApiResponse invalid(string $key, string|array $messages)
17+
* @method static \KodePandai\ApiResponse\ApiResponse validateOrFail(array $rules, array $messages, array $customAttributes, ?\Illuminate\Http\Request $request): array
1718
* @method \KodePandai\ApiResponse\ApiResponse statusCode(int $code)
1819
* @method \KodePandai\ApiResponse\ApiResponse successful()
1920
* @method \KodePandai\ApiResponse\ApiResponse notSuccessful()

0 commit comments

Comments
 (0)