creativecrafts/laravel-api-response

A simple package to have a consistent api response.

1.0.0 2024-03-17 05:58 UTC

This package is auto-updated.

Last update: 2024-11-15 03:31:42 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A simple handy package to have a consistent api response.

Installation

You can install the package via composer:

composer require creativecrafts/laravel-api-response

Usage

// respond with success
$message = 'Success message';
$return LaravelApi::successResponse($message);

// respond with success and data
$message = 'Success message';
$data = ['name' => 'test'];
$return LaravelApi::successResponse($message, $data);

// respond with created
$data = [
    'id' => 1,
    'name' => 'Test',
]
$response = LaravelApi::createdResponse($data);

// respond with exception. Exception is optional and will only be used in local or development environment
$exception = new Exception('Test exception');
$message = 'Internal server error';
$errorCodes = 5001;
$statusCode = 500;
return LaravelApi::errorResponse($message, $statusCode, $exception, $errorCodes);

// respond with error
$message = 'Missing required parameters';
$statusCode = 406;
return LaravelApi::errorResponse($message, $statusCode);

// app/Exceptions/Handler.php can be modified to return the response
public function render($request, Throwable $e): Response|JsonResponse|ResponseAlias
    {
        if ($request->expectsJson()) {
            if ($e instanceof PostTooLargeException) {
                return LaravelApi::errorResponse("Size of attached file should be less " . ini_get("upload_max_filesize") . "B", ResponseAlias::HTTP_REQUEST_ENTITY_TOO_LARGE, $e);
            }

            if ($e instanceof ValidationException) {
                return LaravelApi::errorResponse($e->validator->errors()->first(), ResponseAlias::HTTP_UNPROCESSABLE_ENTITY, $e);
            }

            if ($e instanceof ModelNotFoundException) {
                return LaravelApi::errorResponse('Entry for ' . str_replace('App\\', '', $e->getModel()) . ' not found', ResponseAlias::HTTP_NOT_FOUND, $e);
            }

            if ($e instanceof AuthenticationException) {
                return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_UNAUTHORIZED, $e);
            }

            if ($e instanceof AuthorizationException) {
                return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_FORBIDDEN, $e);
            }

            if ($e instanceof ThrottleRequestsException) {
                return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_TOO_MANY_REQUESTS, $e);
            }

            if ($e instanceof Exception) {
                return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_INTERNAL_SERVER_ERROR, $e);
            }

            if ($e instanceof Error) {
                return LaravelApi::errorResponse($e->getMessage(), ResponseAlias::HTTP_INTERNAL_SERVER_ERROR);
            }
        }
        return parent::render($request, $e);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.