Skip to content

Commit 42ae0f5

Browse files
Merge pull request #161 from MarcinOrlowski/dev
Release 9.0.0
2 parents 277a702 + e1a7940 commit 42ae0f5

File tree

63 files changed

+2407
-1550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2407
-1550
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.idea/
22
vendor/
33
composer.lock
4-
.phpunit.result.cache
5-
.php_cs.cache
4+
.*.cache

CHANGES.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
# REST API Response Builder for Laravel #
44

5-
See [compatibility docs](docs/compatibility.md) for details about backward compatibility!
5+
This library follows [Semantic versioning](https://semver.org).
6+
See [compatibility docs](docs/compatibility.md) for details about backward compatibility
7+
before doing major upgrade!
68

79
## CHANGE LOG ##
810

11+
* v9.0.0 (2020-10-17)
12+
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
13+
* [RB-156] Added logic to deal with directly returned objects or arrays.
14+
* [RB-158] Passing primitives as direct payload (i.e. `success(12.50);` is now supported for `array`, `boolean`,
15+
`double`, `integer` and `string` types, configurable via new `converter/primitives`.
16+
* Removed hadrcoded `val` key used by `JsonSerializable` converter.
17+
* Introduced own exceptions for better error reporting. See [src/Exceptions](src/Exceptions) for more info.
18+
919
* v8.1.1 (2020-10-15)
1020
* [RB-155] Fixed `ResponseBuilder` internals preventing exdending class code from
1121
being invoked, thus making response object structure manipulation ineffective (reported by krek95)
@@ -62,7 +72,7 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
6272
* Data converter now handles objects implementing `JsonSerializable` and `Arrayable` contracts as well.
6373

6474
* v6.3.2 (2019-11-07)
65-
* Added `ResponseBuilder::successWithMessage()` method.
75+
* Added `RB::successWithMessage()` method.
6676
* Entries in `classes` config array can now have `pri` (default 0) to enforce order while
6777
merging config with a built-in configuration.
6878
* Persian translation (Thanks to @FaridAghili).
@@ -75,8 +85,8 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
7585

7686
* v6.3.0 (2019-11-02)
7787
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
78-
* Signature of `ResponseBuilder::buildResponse()` changed to allow customization of final `message` entry (@hawezo).
79-
* Moved all code that produces messages for API codes to `ResponseBuilder::getMessageForApiCode()`.
88+
* Signature of `RB::buildResponse()` changed to allow customization of final `message` entry (@hawezo).
89+
* Moved all code that produces messages for API codes to `RB::getMessageForApiCode()`.
8090
* Added `Validator::assertType()` helper method that validates var against set of allowed types.
8191
* Added `Validator::assertString()` helper.
8292

@@ -125,20 +135,20 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
125135
* Reserved codes reduced to 19 (from former 63).
126136
* Added type hints to all method arguments and return values
127137
* `ExceptionHandler` responses use exception specific HTTP code.
128-
* Fixed `ResponseBuilder::errorWithMessageAndData()` not passing data properly.
138+
* Fixed `RB::errorWithMessageAndData()` not passing data properly.
129139
* Fixed exception message thrown by `ApiCodesHelpers::getMaxCode()`.
130140
* Corrected test cases list in `testSuccess_DataAndHttpCode()`.
131141
* Fixed error code fallback in `testRender_HttpException()` test.
132142
* Fixed `testError_DebugTrace()` not containing any asserts.
133143
* Reformatted code to not exceed 132 columns, for better on-line readability.
134-
* `ResponseBuilder::errorWithDataAndHttpCode()` accepts now `null` as http code.
135-
* `ResponseBuilder::errorWithHttpCode()` accepts now `null` as http code.
144+
* `RB::errorWithDataAndHttpCode()` accepts now `null` as http code.
145+
* `RB::errorWithHttpCode()` accepts now `null` as http code.
136146
* Fixed `ExceptionHandlerHelper` replacing HTTP codes above 499 with 400.
137147
* Changed default message for `HTTP_NOT_FOUND` error.
138148
* `ExceptionHandler` now falls back to `EX_UNCAUGHT_EXCEPTION` for all the cases.
139149
* Simplified `ExceptionHandlerHelperTest::testRender_HttpException()` test.
140150
* Removed `exception_handler.use_exception_message_first` feature.
141-
* Removed `ResponseBuilder::DEFAULT_API_CODE_OK` constant.
151+
* Removed `RB::DEFAULT_API_CODE_OK` constant.
142152
* Removed `getReservedMinCode()`, `getReservedMinCode()`, `getReservedMessageKey()` methods.
143153
* Removed internal API code constants. Use corresponding methods to get proper code value.
144154
* Reimplemented Laravel config merger to support multi-dimensional configuration arrays too.
@@ -150,7 +160,7 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
150160

151161
* v4.1.9 (2019-08-08)
152162
* Fixed `ApiCodesHelpers::getMaxCode()` exception message
153-
* Fixed `ResponseBuilder::errorWithMessageAndData()` not passing args properly
163+
* Fixed `RB::errorWithMessageAndData()` not passing args properly
154164

155165
* v4.1.8 (2019-08-07)
156166
* Added Laravel 6 to testing setup
@@ -199,7 +209,7 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
199209
* `[RB-59]` Added option to remap response JSON keys to user provided values
200210
* `[RB-54]` Debug data no longer pollutes `data` leaf. Instead, it adds `debug` dictionary to root data structure.
201211
* `[RB-37]` Added support for Laravel 5.3+ `unauthenticated()` in Exception Handler. See new config keys defaults
202-
* `[RB-47]` Exception Handler now supports `FormRequests` and returns all messages in `ResponseBuilder::KEY_MESSAGES`
212+
* `[RB-47]` Exception Handler now supports `FormRequests` and returns all messages in `RB::KEY_MESSAGES`
203213
* Uncaught `HttpResponse::HTTP_UNAUTHORIZED` exception is now handled same way `authentication_exception` is
204214
* `[RB-56]` Added configurable key for debug trace added to returned JSON response (if enabled)
205215
* Added traits to help testing your config and ApiCodes with ease. See `Unit Testing your ApiCodes` docs for details

CONTRIBUTING.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* [Documentation](docs/docs.md)
2727
* [Requirements](docs/docs.md#requirements)
2828
* [Installation and Configuration](docs/docs.md#installation-and-configuration)
29-
* [Bugs reports and pull requests](CONTRIBUTING.md)
3029
* [License](#license)
3130
* [Changelog](CHANGES.md)
3231

@@ -36,34 +35,33 @@
3635

3736
## Introduction ##
3837

39-
`ResponseBuilder` is a [Laravel](https://laravel.com/) helper designed to build nice, normalized and easy to consume REST API
38+
`ResponseBuilder` is a [Laravel](https://laravel.com/) helper designed to build nice, normalized and easy to consume REST API
4039
JSON responses.
4140

4241
## Benefits ##
4342

44-
`ResponseBuilder` is written for REST API developers by REST API developer and is based on my long lasting experience on both
45-
"sides" (API dev and API consumer) of variety of REST APIs. Lightweight, with simple to use public methods, covering multiple
46-
potential use-cases, on-the-fly data conversion, localization support, automatic error message building, support
47-
for chained APIs and (hopefully) exhaustive documentation. But that's not all! The JSON structure produced by `ResponseBuilder`
48-
is designed with **users of your API** in mind, which helps them easily deal with your API with ease. They get simple, well
49-
defined and predictable JSON structure responses with all the fields needed to consume it without any unnecessary a hassle nor
50-
other trickery.
51-
52-
Android developers can use [ApiResponse](https://github.com/MarcinOrlowski/ApiResponse) library to handle `ResponseBuilder`
53-
responses produced in their mobile applications.
54-
55-
You are even covered in a case of emergency as provided Exception Handler ensures your API keeps talking JSON (and
56-
not HTML) to its clients if case of any unexpected and unhandled exception.
57-
58-
Did I mention, you also get testing traits that would automatically cover your whole `ResponseBuilder` related code with
59-
unit tests with just a few lines of code?
43+
`ResponseBuilder` is written for REST API developers by REST API developer and is based on my long-lasting experience on both
44+
"sides" as API dev and API consumer, of variety of REST APIs. It's lightweight, extensively tested, simple to use yet
45+
flexible and powerful, withon-the-fly data conversion, localization support, automatic error message building, support
46+
for chained APIs and (hopefully) exhaustive documentation. But that's not all! The JSON structure produced by `ResponseBuilder`
47+
is designed with **users of your API** in mind, to make dealing with your API a breeze. Simple JSON response, with well-defined
48+
and predictable structure, easy to consume without a hassle nor trickery.
49+
50+
As a bonus, Android developers can use [ApiResponse](https://github.com/MarcinOrlowski/ApiResponse) library in their apps
51+
to handle `ResponseBuilder` responses.
52+
53+
You are even covered in a case of emergency, as provided Exception Handler helper, ensures your API keeps talking JSON (and
54+
not HTML) to its clients even in case of unexpected and unhandled exception.
55+
56+
Did I mention, you would also get free testing traits that automatically unit test your whole `ResponseBuilder` related code
57+
and configuration with just a few lines of code?
6058

6159
## Usage examples ##
62-
60+
6361
Operation successful? Conclude your controller method with:
6462

6563
```php
66-
return ResponseBuilder::success();
64+
return RB::success();
6765
```
6866

6967
and your client will get nice JSON like
@@ -78,10 +76,43 @@ return ResponseBuilder::success();
7876
}
7977
```
8078

81-
Something went wrong? Just type:
79+
Need to additionally return extra payload with the response? Pass it as
80+
argument to `success()`:
81+
82+
```php
83+
$flight = App\Flight::where(...)->get();
84+
return RB::success($flight);
85+
```
86+
87+
and your client will get that data in `data` node of your response:
88+
89+
```json
90+
{
91+
"success": true,
92+
"code": 0,
93+
"locale": "en",
94+
"message": "OK",
95+
"data": {
96+
"items": [
97+
{
98+
"airline": "lot",
99+
"flight_number": "lo123",
100+
...
101+
},
102+
{
103+
"airline": "american",
104+
"flight_number": "am456",
105+
...
106+
}
107+
]
108+
}
109+
}
110+
```
111+
112+
Something went wrong and you want to tell the clinet about that? Just do:
82113

83114
```php
84-
return ResponseBuilder::error(250);
115+
return RB::error(250);
85116
```
86117

87118
The following JSON response will then be returned:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume Laravel REST API.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "8.1.1",
5+
"version": "9.0.0",
66
"keywords": [
77
"laravel",
88
"json",

config/response_builder.php

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,63 @@
3737
|
3838
*/
3939
'converter' => [
40-
\Illuminate\Database\Eloquent\Model::class => [
41-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
42-
// 'key' => 'item',
43-
'pri' => 0,
44-
],
45-
\Illuminate\Support\Collection::class => [
46-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
47-
// 'key' => 'item',
48-
'pri' => 0,
49-
],
50-
\Illuminate\Database\Eloquent\Collection::class => [
51-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
52-
// 'key' => 'item',
53-
'pri' => 0,
54-
],
55-
\Illuminate\Http\Resources\Json\JsonResource::class => [
56-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
57-
// 'key' => 'item',
58-
'pri' => 0,
59-
],
40+
'primitives' => [
41+
/* Configuration for primitives used when such data is passed directly as payload (i.e. `success(15)`;) */
42+
'array' => [
43+
'key' => 'values',
44+
],
45+
'boolean' => [
46+
'key' => 'value',
47+
],
48+
'double' => [
49+
'key' => 'value',
50+
],
51+
'integer' => [
52+
'key' => 'value',
53+
],
54+
'string' => [
55+
'key' => 'value',
56+
],
57+
],
6058

61-
/*
62-
|-----------------------------------------------------------------------------------------------------------
63-
| Generic converters should have lower pri to allow dedicated ones to kick in first when class matches
64-
|-----------------------------------------------------------------------------------------------------------
65-
*/
66-
\JsonSerializable::class => [
67-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
68-
// 'key' => 'item',
69-
'pri' => -10,
70-
],
71-
\Illuminate\Contracts\Support\Arrayable::class => [
72-
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
73-
// 'key' => 'item',
74-
'pri' => -10,
59+
/* Object converters configuration for supported classes */
60+
'classes' => [
61+
\Illuminate\Database\Eloquent\Model::class => [
62+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
63+
'key' => 'item',
64+
'pri' => 0,
65+
],
66+
\Illuminate\Support\Collection::class => [
67+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
68+
'key' => 'items',
69+
'pri' => 0,
70+
],
71+
\Illuminate\Database\Eloquent\Collection::class => [
72+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
73+
'key' => 'items',
74+
'pri' => 0,
75+
],
76+
\Illuminate\Http\Resources\Json\JsonResource::class => [
77+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
78+
'key' => 'item',
79+
'pri' => 0,
80+
],
81+
82+
/*
83+
|-----------------------------------------------------------------------------------------------------------
84+
| Generic converters should have lower pri to allow dedicated ones to kick in first when class matches
85+
|-----------------------------------------------------------------------------------------------------------
86+
*/
87+
\JsonSerializable::class => [
88+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
89+
'key' => 'item',
90+
'pri' => -10,
91+
],
92+
\Illuminate\Contracts\Support\Arrayable::class => [
93+
'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
94+
'key' => 'items',
95+
'pri' => -10,
96+
],
7597
],
7698

7799
],

0 commit comments

Comments
 (0)