This package help me to some repetitive things in my backend projects.
- Laravel fractal (Required)
- Laravel Debugbar (Optional)
- Sentry SDK Laravel (Optional)
composer require jncalderon/laravel-api
{
"count": 2,
"data": [
{
"id": 1,
"name": "Jonh",
"lastName": "Smith"
},
{
"id": 2,
"name": "Jonh",
"lastName": "Smith"
}
]
}
Facade | Description |
---|---|
Api::jsonResponse() | Transform any data to a json Response |
Transformer::serialize() | Serialize data to a camelcase array |
Transformer::camecaseArray() | Camel case all keys in an array |
Transformer::snakecaseArray() | Snake case all keys in an array |
Can access to the facades through below helpers
api()->jsonResponse();
transformer()->camelcaseArray();
Handler no exceptions to Json, change the extends of the file app\Exceptions\Handler.php
to:
<?php
namespace App\Exceptions;
use Jncalderon\LaravelApi\Exceptions\Handler as JnHandler;
class Handler extends JnHandler
{
// no require additional methods and properties
}
In app\Http\Kernel.php
can add below middlewares.
protected $middlewareGroups = [
'api' => [
...
\Jncalderon\LaravelApi\Http\Middleware\JsonDebugbar::class,
\Jncalderon\LaravelApi\Http\Middleware\ConvertRequestFieldsToCamelCase::class,
\Jncalderon\LaravelApi\Http\Middleware\Localization::class,
\Jncalderon\LaravelApi\Http\Middleware\SentryUser::class,
],
];
Middleware | Description |
---|---|
JsonDebugbar | Attanch to the JsonResponse the debugbar response |
ConvertRequestFieldsToCamelCase | All keys of paramaters send force to snake case |
Localization | Change language adding the header X-Localization |
SentryUser | Tracking of the user in Sentry |
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.