Apiwrapper
Superfast, lightweight, and highly customizable wrapper for Laravel API responses.
Install / Use
/learn @negartarh/ApiwrapperREADME
Laravel Missed API Response Wrapper 
Super Fast | Light Weight | Standard | Octane Compatible | High Customizable
Built for Applications Ranging from Small to Large Scale
</div> <br /> <div align="center" style="text-align: center">
Introduction
The Laravel Missed API Response Wrapper package is a high-quality and standard package that makes the process of creating and managing standard API responses in Laravel easy. This package is both fast and lightweight, and fully compatible with Laravel Octane, highly customizable, and automatically enables the standardization of all API responses. By using this package, you can easily manage errors and develop standard API services that automatically provide responses according to HTTP and REST standards.
This package is usable anywhere, from validators to controllers and other components, and automatically provides features such as request status, message, errors, and execution time. Additionally, adding custom values to responses or disabling these features is easily achievable.
Using this package guarantees the standardization of API responses, meaning you can continuously and reliably provide high-quality responses that are easily understandable and usable for consumers of your API, while also being fully compliant with standards. All done automatically.</div>
Installation
To install Laravel Missed API Response Wrapper, just run the following command:
composer require negartarh/apiwrapper
Configuration
After installing the package, you need to publish its configuration file. To do that, run the following command:
php artisan vendor:publish --provider="Negartarh\APIWrapper\APIResponseServiceProvider"
This will publish the apiwrapper.php configuration file to your config directory and publish localization files to your languages/vendor/apiwrapper directory.
Basic usage
There are two ways of utilizing the package: using the facade, or using the helper functions. Either way you will get the same result, it is totally up to you.
Facade
Example 1.
use Negartarh\APIWrapper\Facades\APIResponse;
...
public function index():\Illuminate\Http\Response
{
$users = User::latest()->take(10)->get();
# Alias of
# return APIResponse::success($users);
return APIResponse::ok($users);
}
Helper functions
Example 1.
use Negartarh\APIWrapper\Facades\APIResponse;
...
public function index():\Illuminate\Http\Response
{
$users = User::latest()->take(10)->get();
return apiwrapper()->ok($users);
# or
return api_response()->ok($users);
}
The result of the above codes is as follows:
{
"status": 200, // HTTP status code
"message": "OK", // HTTP message
"data": [ // Response data
{
"id": 1,
...
},
...
],
"errors": [], // Errors
"execution": "13ms", // Serverside exection time
"version": "3.2.0" // Your application version
}
As you can see, the simple output information has been automatically replaced with the classified information suitable for API requests. Look at the output keys, they are all changeable and editable, but before that, it is better to do more research on the package and get acquainted with its more features.
Advanced Usage
Automatic output based on HTTP standards
Example 1. Storing data
use Illuminate\Support\Facades\Request;
use Negartarh\APIWrapper\Facades\APIResponse;
...
public function create(Request $request):\Illuminate\Http\Response
{
$user = User::where('email', '=', $request->get('email'))
->firstOrCreate();
return APIResponse::created($user);
}
The result of the above code is as follows:
{
"status": 201, // HTTP status code
"message": "Created", // HTTP message
"data": { // Response data
"id": 1,
...
},
"errors": [], // Errors
"execution": "10ms", // Serverside exection time
"version": "3.2.0" // Your application version
}
Example 2. No content
use Illuminate\Support\Facades\Request;
use Negartarh\APIWrapper\Facades\APIResponse;
...
public function index(Request $request):\Illuminate\Http\Response
{
$posts = Post::all();
if(!is_countable($posts) or count($posts) == 0):
return APIResponse::noContent();
else:
...
}
The result of the above code is as follows:
{
"status": 204, // HTTP status code
"message": "No Content", // HTTP message
"data": [],
"errors": [],
"execution": "10ms",
"version": "3.2.0"
}
Example 3. Validating data
use Illuminate\Http\Exceptions\HttpResponseException;
use Negartarh\APIWrapper\Facades\APIResponse;
class ExampleCaptchaRequest extends FormRequest // example rule class
{
...
/**
* Handle a failed validation attempt.
*
* @param Validator $validator
* @return HttpResponseException
*/
public function failedValidation(Validator $validator): HttpResponseException
{
# based on RFC: 4918
return APIResponse::unprocessableEntity($validator->errors());
}
}
The result of the above code is as follows:
{
"status": 422, // HTTP status code
"message": "Unprocessable Entity", // HTTP message
"errors": {
"captcha": [
"The CAPTCHA has expired."
]
},
"data": [],
"execution": "41ms",
"version": "3.2.0"
}
Let’s also take a look at the server response and output headers,
everything looks great, If it is hard for you to remember the HTTP standards, no problem, pay attention to the next example.
Alternative method
Example 1. Status method
use Illuminate\Http\Exceptions\HttpResponseException;
use Negartarh\APIWrapper\Facades\APIResponse;
class ExampleCaptchaRequest extends FormRequest // example rule class
{
...
/**
* Handle a failed validation attempt.
*
* @param Validator $validator
* @return HttpResponseException
*/
public function failedValidation(Validator $validator): HttpResponseException
{
return APIResponse::status(413, $validator->errors());
}
}
and the result is:
{
"status": 413,
"message": "Request Entity Too Large",
"errors": [
...
],
"data": [],
"execution": "17ms",
"version": "3.2.0"
}
Wait a moment, isn't it better to customize the output message? So pay attention to the following example:
Customized messages
Example 1.
use Illuminate\Http\Exceptions\HttpResponseException;
use Negartarh\APIWrapper\Facades\APIResponse;
class ExampleAuthenticationRequest extends FormRequest // example rule class
{
...
/**
* Handle a failed validation attempt.
*
* @param Validator $validator
* @return HttpResponseException
*/
public function failedValidation(Validator $validator): HttpResponseException
{
# Alias of
# return APIResponse::status(403, $validator->errors(), 'Where are you looking here?');
retun APIResponse::forbidden($validator->errors(), 'What are you looking for here?');
}
}
and guess the result:
{
"status": 403,
"message": "What are you looking for here?",
"errors": {
...
},
"data": [],
"execution": "15ms",
"version": "3.2.0"
}
But wait, there is a better solution, why not implement our own team standard? To do this, just add your own standard to the apiwrapper.php file in the config folder of your project and or make changes to it as needed.
Customized methods
Example 1.
# path/to/project/configuration/dir/apiwrapper.php
return [
...
'methods' => [
...
'accessDenied' => [
'code' => 403,
'message' => 'What are you looking for here?',
'headers' => [
'Authorization' => 'Failed',
],
],
and easily use the defined method in your project.
use Illuminate\Support\Facades\Request;
use Negartarh\APIWrapper\Facades\APIResponse;
...
public function login(Request $request):\Illuminate\Http\Response
{
$user = User::where('access_token', '=', $request->get('access_token'))
->first();
if($user == null):
return APIResponse::accessDenied();
els
