Skip to content

Commit ef3fdb6

Browse files
committed
wip
1 parent 7515766 commit ef3fdb6

File tree

3 files changed

+163
-30
lines changed

3 files changed

+163
-30
lines changed

README.md

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,166 @@
1-
# Laravel Response Builder
1+
# Laravel API Response Builder
22

3-
A Laravel package designed to simplify the creation of structured and formatted API responses (JSON, XML) with custom status codes, messages, and data.
3+
**Laravel API Response Builder** is a Laravel package designed to simplify the creation of structured and formatted API responses (JSON, XML) with customizable status codes, messages, and data. This package provides a flexible way to handle API responses, including error handling, logging, and various configuration options.
4+
5+
## Features
6+
7+
- **Structured Responses**: Define custom response structures for consistency.
8+
- **Format Options**: Choose between JSON and XML response formats.
9+
- **Logging**: Log requests and responses for debugging and auditing.
10+
- **Compression**: Optionally compress responses to reduce payload size.
11+
- **Error Handling**: Include detailed error messages for debugging.
412

513
## Installation
614

7-
Install via Composer:
15+
To install the package, run the following command:
16+
17+
```bash
18+
composer require doliveira/laravel-api-response-builder
19+
```
20+
21+
## Configuration
22+
23+
After installing the package, publish the configuration file using the following command:
824

925
```bash
10-
composer require d-oliveira/laravel-response-builder
26+
php artisan vendor:publish --provider="Doliveira\LaravelResponseBuilder\Providers\ResponseBuilderServiceProvider"
27+
```
28+
29+
This will create a `config/laravel-api-response-builder.php` file where you can customize the package settings.
30+
31+
## Configuration Options
32+
33+
Here's a brief overview of the available configuration options:
34+
35+
- **`default_format`**: The default format for responses (`'json'` or `'xml'`).
36+
- **`json_options`**: Options for JSON encoding (e.g., `JSON_UNESCAPED_SLASHES`).
37+
- **`xml_root_element`**: The default root element for XML responses.
38+
- **`default_status_code`**: The default HTTP status code for responses.
39+
- **`detailed_errors`**: Whether to include detailed error messages for debugging.
40+
- **`wrap_data`**: Whether to wrap response data in an additional `'data'` key.
41+
- **`default_locale`**: The default locale for custom messages.
42+
- **`default_response_language`**: The default language for responses (`'en'` or `'pt'`).
43+
- **`compress_responses`**: Whether to compress responses.
44+
- **`log_responses`**: Whether to log responses.
45+
- **`logging_level`**: The level of detail for logging responses.
46+
- **`api_key_header`**: The name of the header used for API keys.
47+
- **`custom_response_structure`**: Define a custom structure for responses.
48+
- **`debug_mode`**: Enable or disable debug mode.
49+
- **`log_requests`**: Whether to log requests.
50+
- **`log_response_time`**: Whether to log response times.
51+
52+
## Usage
53+
54+
### JSON Responses
55+
56+
#### Success Response
57+
58+
```php
59+
use Doliveira\LaravelResponseBuilder\JsonResponse;
60+
61+
return JsonResponse::success($data, 'Operation successful.');
62+
```
63+
64+
#### Error Response
65+
66+
```php
67+
use Doliveira\LaravelResponseBuilder\JsonResponse;
68+
69+
return JsonResponse::error(400, 'Bad request.', $data);
1170
```
71+
72+
### XML Responses
73+
74+
To create XML responses, use the `XmlResponse` class:
75+
76+
```php
77+
use Doliveira\LaravelResponseBuilder\XmlResponse;
78+
79+
$response = new XmlResponse($statusCode, $message, $data);
80+
return $response->build();
81+
```
82+
83+
## Error Handling
84+
85+
The package allows you to include detailed error messages for debugging. By default, this is disabled in production environments:
86+
87+
```php
88+
'logging_level' => 'info',
89+
'detailed_errors' => false,
90+
```
91+
92+
You can enable detailed error messages for debugging purposes by setting `'detailed_errors'` to `true` in the configuration.
93+
94+
## Examples
95+
96+
### Success JSON Response
97+
98+
```json
99+
{
100+
"status": 200,
101+
"message": "Operation successful.",
102+
"data": {
103+
"id": 1,
104+
"name": "Example"
105+
}
106+
}
107+
```
108+
109+
### Error JSON Response
110+
111+
```json
112+
{
113+
"status": 400,
114+
"message": "Bad request.",
115+
"data": null
116+
}
117+
```
118+
119+
### Success XML Response
120+
121+
```xml
122+
<response>
123+
<status>200</status>
124+
<message>Operation successful.</message>
125+
<data>
126+
<id>1</id>
127+
<name>Example</name>
128+
</data>
129+
</response>
130+
```
131+
132+
### Error XML Response
133+
134+
```xml
135+
<response>
136+
<status>400</status>
137+
<message>Bad request.</message>
138+
<data/>
139+
</response>
140+
```
141+
142+
## Logging
143+
144+
The package can log requests, responses, and response times. Configure logging settings in `config/laravel-api-response-builder.php`:
145+
146+
```php
147+
'log_responses' => true,
148+
'log_requests' => true,
149+
'log_response_time' => true,
150+
'logging_level' => 'info',
151+
```
152+
153+
## Contributing
154+
155+
If you want to contribute to the development of this package, please fork the repository and submit a pull request.
156+
157+
## License
158+
159+
This package is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
160+
161+
## Contact
162+
163+
For any questions or feedback, please contact:
164+
165+
- **Danilo Oliveira**: [daniloworkdev@gmail.com](mailto:daniloworkdev@gmail.com)
166+
- **Website**: [daniloo.dev](http://www.daniloo.dev)

src/BaseResponse.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ protected function getResponseStructure(): array
2929
protected function wrapData($data): array
3030
{
3131
if (Config::get('responsebuilder.wrap_data', true)) {
32-
return ['data' => $data];
32+
return ['data' => is_array($data) ? $data : json_decode(json_encode($data), true)];
3333
}
3434

35-
return $data;
35+
return is_array($data) ? $data : json_decode(json_encode($data), true);
3636
}
37-
37+
3838
protected function getJsonOptions(): int
3939
{
4040
return Config::get('responsebuilder.json_options', JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
@@ -55,11 +55,6 @@ protected function getLoggingLevel(): string
5555
return Config::get('responsebuilder.logging_level', 'info');
5656
}
5757

58-
protected function getCorsSettings(): array
59-
{
60-
return Config::get('responsebuilder.cors_settings', []);
61-
}
62-
6358
protected function getDetailedErrors(): bool
6459
{
6560
return Config::get('responsebuilder.detailed_errors', false);

src/config/config.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
| key. This is useful if you want a consistent structure for all responses.
6363
|
6464
*/
65-
'wrap_data' => true,
65+
'wrap_data' => false,
6666

6767
/*
6868
|--------------------------------------------------------------------------
@@ -171,21 +171,4 @@
171171
|
172172
*/
173173
'log_response_time' => false,
174-
175-
/*
176-
|--------------------------------------------------------------------------
177-
| CORS Settings
178-
|--------------------------------------------------------------------------
179-
|
180-
| Define CORS settings to control cross-origin resource sharing.
181-
|
182-
*/
183-
'cors_settings' => [
184-
'enabled' => true,
185-
'allowed_origins' => ['*'],
186-
'allowed_methods' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
187-
'allowed_headers' => ['Content-Type', 'Authorization'],
188-
'exposed_headers' => ['Authorization'],
189-
'max_age' => 86400, // Time in seconds
190-
],
191174
];

0 commit comments

Comments
 (0)