Skip to content

Commit 980621f

Browse files
Merge pull request #62 from MarcinOrlowski/dev
Release 4.0.0
2 parents 7fa1561 + b68a72f commit 980621f

24 files changed

+1517
-914
lines changed

CHANGES.md

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
44

55
## CHANGE LOG ##
66

7+
* v4.0.0 (2017-04-10)
8+
* **BACKWARD INCOMPATIBILE CHANGES**
9+
* [RB-59] Added option to remap response JSON keys to user provided values
10+
* [RB-54] Debug data no longer pollutes `data` leaf. Instead, it adds `debug` dictionary to root data structure.
11+
* [RB-37] Added support for Laravel 5.3+ `unauthenticated()` in Exception Handler. See new config keys defails
12+
* [RB-47] Exception Handler now supports `FormRequests` and returns all messages in `ResponseBuilder::KEY_MESSAGES`
13+
* Uncaught `HttpResponse::HTTP_UNAUTHORIZED` exception is now handled same way `authentication_exception` is
14+
* [RB=56] Added configurable key for debug trace added to returned JSON response (if enabled)
15+
* Added traits to help testing your config and ApiCodes with ease. See `Unit Testing your ApiCodes` docs for details
16+
* `ApiCodeBase` class is now named `BaseApiCodes`
17+
* [RB-35] ExceptionHandlerHelper is now covered by tests
18+
719
* v3.2.1 (2017-04-06)
820
* [RB-49] Fixed `artisan vendor:publish` not publishing config file correctly
921

README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# API Response Builder for Laravel 5 #
1+
# API Response Builder for Laravel #
22

3-
`ResponseBuilder` is Laravel5's helper designed to simplify building
3+
`ResponseBuilder` is [Laravel](https://laravel.com/)'s helper designed to simplify building
44
nice, normalized and easy to consume REST API responses.
55

66
[![Latest Stable Version](https://poser.pugx.org/marcin-orlowski/laravel-api-response-builder/v/stable)](https://packagist.org/packages/marcin-orlowski/laravel-api-response-builder)
@@ -41,13 +41,14 @@ then then feel free to donate to the project. Send some Bitcoins (BTC) to `1Lbfb
4141

4242
* Easy to use,
4343
* [Stable and production ready](https://travis-ci.org/MarcinOrlowski/laravel-api-response-builder),
44-
* Laravel 5.1, 5.2, 5.3 and 5.4 compatible,
44+
* Laravel 5.x, 5.2, 5.3 and 5.4 compatible,
4545
* Works on PHP 5.5, 5.6, 7.0, 7.1 and [HHVM](http://hhvm.com/),
4646
* Configurable (with ready-to-use defaults),
4747
* Localization support,
4848
* Automatic object conversion with custom mapping,
4949
* API chaining/cascading support,
50-
* Provides [exception handler helper](docs/exceptions.md) to ensure your API stays consumable even in case of unexpected,
50+
* Includes traits to help [unit testing your API code](docs/testing.md),
51+
* Provides own [exception handler helper](docs/exceptions.md) to ensure your API stays consumable even in case of unexpected,
5152
* No extra dependencies.
5253

5354
----
@@ -57,13 +58,16 @@ then then feel free to donate to the project. Send some Bitcoins (BTC) to `1Lbfb
5758
Please report any issue spotted using [GitHub's project tracker](https://github.com/MarcinOrlowski/laravel-api-response-builder/issues).
5859

5960
If you'd like to contribute to the this project, please [open new ticket](https://github.com/MarcinOrlowski/laravel-api-response-builder/issues)
60-
**before doing any work**. This will help us save your
61-
time in case I'd not be able to accept such changes. But if all is good and clear then follow common routine:
62-
63-
* fork the project
64-
* create new branch
65-
* do your changes
66-
* send pull request
61+
**before doing any work**. This will help us save your time in case I'd not be accept PR either completely or in proposed form.
62+
But if all is good and clear then follow common routine:
63+
64+
* fork the project,
65+
* create new branch,
66+
* do your changes,
67+
* add tests covering your changes,
68+
* ensure **ALL** tests pass,
69+
* send pull request,
70+
* glory.
6771

6872
----
6973

composer.json

+7-11
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 REST API responses.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "3.2.1",
5+
"version": "4.0.0",
66
"keywords": [
77
"laravel",
88
"json",
@@ -27,22 +27,18 @@
2727
"laravel/framework": "^5.1"
2828
},
2929
"autoload": {
30-
"psr-4": {
31-
"MarcinOrlowski\\ResponseBuilder\\": "src"
32-
}
33-
},
34-
"autoload-dev": {
35-
"psr-4": {
36-
"MarcinOrlowski\\ResponseBuilder\\": "src",
37-
"MarcinOrlowski\\ResponseBuilder\\Tests\\": "tests"
38-
}
30+
"classmap": [
31+
"src/",
32+
"tests/",
33+
"tests/Traits/"
34+
]
3935
},
4036
"require-dev": {
4137
"marcin-orlowski/coding-standard": "^1.1",
4238
"phpunit/phpunit": "^4.8",
4339
"phpunit/php-code-coverage": "^2.2.4",
4440
"mockery/mockery": "^0.9.8",
45-
"orchestra/testbench": "~3.1",
41+
"orchestra/testbench": "^3.1",
4642
"codacy/coverage": "^1.0"
4743
}
4844
}

config/response_builder.php

+60-16
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
|
4444
| ApiCode::SOMETHING => 'api.something',
4545
|
46-
| See README if you want to provide own messages for built-in codes too.
46+
| See docs/exceptions.md if you want to provide own messages for built-in codes too.
4747
|
4848
*/
4949
'map' => [
@@ -106,6 +106,35 @@
106106
'encoding_options' => JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE,
107107

108108

109+
/*
110+
|--------------------------------------------------------------------------
111+
| Response JSON keys mapping
112+
|--------------------------------------------------------------------------
113+
|
114+
| You can remap response JSON keys if really needed.
115+
|
116+
|
117+
| WARNING: there's NO duplicate check at runtime, so if you remap two keys
118+
| to the same values you will end up with problems. There's testing trait
119+
| to prevent this from happening, so ensure you unit test your app (see docs!)
120+
|
121+
| NOTE: Ensure you have this config file using:
122+
|
123+
| use MarcinOrlowski\ResponseBuilder\ResponseBuilder;
124+
|
125+
| if you want to use custom mapping.
126+
|
127+
| It's safe to completely remove/comment out this config element.
128+
|
129+
*/
130+
// 'response_key_map' => [
131+
// ResponseBuilder::KEY_SUCCESS => 'success',
132+
// ResponseBuilder::KEY_CODE => 'code',
133+
// ResponseBuilder::KEY_LOCALE => 'locale',
134+
// ResponseBuilder::KEY_MESSAGE => 'message',
135+
// ResponseBuilder::KEY_DATA => 'data',
136+
// ],
137+
109138
/*
110139
|--------------------------------------------------------------------------
111140
| Exception handler error codes
@@ -119,36 +148,46 @@
119148
*/
120149
'exception_handler' => [
121150

122-
// By default, exception provided messages have higher priority than mapped error messages.
123-
// This behaviour can be configured with `use_exception_message_first` option. When option
151+
// By default, exception provided messages has higher priority than mapped error messages.
152+
// This behaviour can be configured with `use_exception_message_first` option. When it
124153
// is set to `true` (which is default value) and when exception's `getMessage()` returns non empty
125-
// string, that string will be used as returned as `message` w/o further processing. If
154+
// string, that string will be used and returned as `message` w/o further processing. If
126155
// it is set to `true` but exception provides no message, then mapped message will be used
127156
// and the ":message" placeholder will be substituted with exception class name. When option
128-
// is set to @false, then pre 2.0 behaviour takes place and mapped messages will always be used
129-
// with `:message` placeholder being substituted with exception message (can if it is empty string).
157+
// is set to @false, then mapped messages will always be used with `:message` placeholder
158+
// being substituted with exception message (can if it is empty string).
130159
'use_exception_message_first' => env('EX_USE_EXCEPTION_MESSAGE', true),
131160

132161

133162
// Map exception to your own error codes. That way, when cascading
134163
// you will still know which module thrown this exception
135164
'exception' => [
165+
136166
// 'http_not_found' => [
137-
// 'code' => \App\ApiCode::HTTP_NOT_FOUND,
167+
// 'code' => \App\ApiCodes::HTTP_NOT_FOUND,
138168
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_BAD_REQUEST,
139169
// ],
140170
// 'http_service_unavailable' => [
141-
// 'code' => \App\ApiCode::HTTP_SERVICE_UNAVAILABLE,
171+
// 'code' => \App\ApiCodes::HTTP_SERVICE_UNAVAILABLE,
142172
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_BAD_REQUEST,
143173
// ],
144174
// 'http_exception' => [
145-
// 'code' => \App\ApiCode::HTTP_EXCEPTION,
175+
// 'code' => \App\ApiCodes::HTTP_EXCEPTION,
146176
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_BAD_REQUEST,
147177
// ],
148178
// 'uncaught_exception' => [
149-
// 'code' => \App\ApiCode::UNCAUGHT_EXCEPTION,
179+
// 'code' => \App\ApiCodes::UNCAUGHT_EXCEPTION,
150180
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_INTERNAL_SERVER_ERROR,
151181
// ],
182+
// 'authentication_exception' => [
183+
// 'code' => \App\ApiCodes::AUTHENTICATION_EXCEPTION,
184+
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_UNAUTHORIZED,
185+
// ],
186+
// 'validation_exception' => [
187+
// 'code' => \App\ApiCodes::VALIDATION_EXCEPTION,
188+
// 'http_code' => Symfony\Component\HttpFoundation\Response\::HTTP_UNPROCESSABLE_ENTITY,
189+
// ],
190+
152191
],
153192

154193
],
@@ -162,15 +201,20 @@
162201
*/
163202

164203
'debug' => [
204+
// 'debug_key' => 'debug',
205+
165206
'exception_handler' => [
166207
/**
167-
* When ExceptionHandler kicks in and this is set to @true (default),
168-
* then **if** your `app.debug` is `true` too, returned JSON structure
169-
* will contain `debug` node with additional exception base trace (class
170-
* name, file name, line number). If `app.debug` is anything but @true,
171-
* no debug info is added.
208+
* Name of the JSON key trace data should be put under in `debug` node.
209+
*/
210+
// 'trace_key' => 'trace',
211+
212+
/**
213+
* When ExceptionHandler kicks in and this is set to @true,
214+
* then returned JSON structure will contain additional debug data
215+
* with information about class name, file name and line number.
172216
*/
173-
'trace_enabled' => true,
217+
'trace_enabled' => env('APP_DEBUG', false),
174218
],
175219
],
176220

docs/compatibility.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# API Response Builder for Laravel 5 #
1+
# API Response Builder for Laravel #
22

33
`ResponseBuilder` follows [Semantic Versioning](http://semver.org/).
44

5+
### v4 ###
6+
7+
* `ErrorCodes` class is now `BaseApiCodes`
8+
* ExceptionHandler's debug trace no longer depends on `APP_DEBUG` value and can be enabled independently
9+
510
### v3 ###
611

712
* `success()` now accepts (optional) `api_code` too, therefore signature of this method as well as and argument
@@ -14,10 +19,12 @@
1419
* ApiCodeBase's `getErrorCodeConstants()` is now `getApiCodeConstants()`
1520
* ApiCodeBase's `getMapping()` is now `getCodeMessageKey()`
1621
* ApiCodeBase's `getBaseMapping()` is now `getReservedCodeMessageKey()`
22+
* Internal constants for `ExeceptionHandlerHelper` supported exceptions are now prefixed with `EX_` (i.e. `HTTP_NOT_FOUND`
23+
is now `EX_HTTP_NOT_FOUND`)
1724

1825
### v2 ###
1926

20-
* First public release.
27+
* First public release
2128

2229
### v1 ###
2330

0 commit comments

Comments
 (0)