Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
🎨 Update methods & README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pcsaini committed Dec 8, 2022
1 parent dd474d8 commit 1a56ba1
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
.phpunit.result.cache
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

297 changes: 283 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<p align="center">
<img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400">
</p>
<h1 align="center">Laravel API Response Builder</h1>
<img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel">
<h1>Laravel API Response Builder</h1>


<p align="center">
<a href="#"><img src="https://poser.pugx.org/laravel/framework/license.svg" alt="License"></a>
</p>
---
![GitHub](https://img.shields.io/github/license/pcsaini/laravel-api-response-builder)
[![Unit Tests](https://github.com/pcsaini/laravel-api-response-builder/actions/workflows/phpunit.yml/badge.svg)](https://github.com/pcsaini/laravel-api-response-builder/actions/workflows/phpunit.yml)
![Packagist Downloads](https://img.shields.io/packagist/dt/pcsaini/laravel-api-response-builder)

## Table of contents ##

* [Introduction](#introduction)
* [Installation & Configuration](#installation--configuration)
* [Examples](#examples)
* [License](#license)
* [Introduction](#introduction)
* [Installation & Configuration](#installation--configuration)
* [Examples](#examples)
* [License](#license)


## Introduction ##
Expand All @@ -23,12 +21,283 @@ REST API JSON responses.


## Installation & Configuration ##
You can install this package via composer using:
```
composer require pcsaini/laravel-api-response-builder
```

Next run the command below to setup `api-response.config` file, you can set your configuration.
```
php artisan vendor:publish --tag=response-builder
```


## Examples ##

### Example : Success
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
$users = User::query()->take(2)->get();
return ResponseBuilder::success($users);
}
// Output
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
},
{
"id": 2,
"name": "Ms. Tessie Streich III",
"email": "dledner@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
}
]
}
```

### Example : Custom Success
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function login() {
$user = User::query()->first();
$token = Uuid::uuid4();
return ResponseBuilder::asSuccess()
->withMessage('Successfuly Login')
->with('auth_token', $token)
->withData([
'profile' => new UserResource($user)
])
->build();
}
// Output
Status: 200 OK
{
"status": true,
"message": "Successfuly Login",
"auth_token": "65050859-1a05-4fa8-827f-7023ff73d4a3",
"data": {
"profile": {
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net"
}
}
}
```

### Example : Error
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
return ResponseBuilder::error('Sorry We are not able to getting your details', Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Output
Status: 500 Internal Server Error
{
"status": false,
"message": "Sorry We are not able to getting your details"
}
```

### Example : Custom Error
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
return ResponseBuilder::asError(Response::HTTP_BAD_GATEWAY)
->withMessage('Sorry We are not able to getting your details')
->with('custom_key', 'value')
->withData([
'bad_gateway_error' => 'We are not able to process your request'
])
->build();
}
// Output
Status: 502 Bad Gateway
{
"status": false,
"message": "Sorry We are not able to getting your details",
"code": 10001,
"data": {
"bad_gateway_error": "We are not able to process your request"
}
}
```


### Example : Validation Error Message
```
use Pcsaini\ResponseBuilder\Http\ValidationFailed;
class UserRequest extends FormRequest
{
use ValidationFailed;
.
.
}
// Output
Status: 400 Bad Request
{
"status": false,
"errors": {
"name": [
"The name field is required."
],
"email": [
"The email field is required."
]
}
}
```

* You can customize HttpStatus Code in `config/api-response.php` under the variable `validation_http_code`.
* You can customize your error message `config/api-response.php` under the variable `show_validation_failed_message`, `all` for get all error messages and `first` for get the first message.

Example:
```
// config/api-response.php
'show_validation_failed_message' => 'first',
// Output
Status: 400 Bad Request
{
"status": false,
"message": "The name field is required."
}
```

### Example : Custom Filed
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->first();
$token = Uuid::uuid4();
return ResponseBuilder::asSuccess()->with('auth_token', $token)->withData($user)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"auth_token": "21d97007-e2b9-4ee1-86b1-3cfb96787436",
"data": {
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
}
}
```

### Example : Pagination
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->paginate(3);
return ResponseBuilder::asSuccess()->withPagination($user)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
},
.
.
],
"meta": {
"total_page": 14,
"current_page": 1,
"total_item": 40,
"per_page": 3
},
"link": {
"next": true,
"prev": false
}
}
```

### Example : Pagination and you are using JsonResource
```
use Pcsaini\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->paginate(3);
return ResponseBuilder::asSuccess()->withPagination($user, UserResource::class)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net"
},
.
.
],
"meta": {
"total_page": 14,
"current_page": 1,
"total_item": 40,
"per_page": 3
},
"link": {
"next": true,
"prev": false
}
}
```

## License ##

* Written and copyrighted &copy;2022 by Prem Chand Saini ([prem@signaturetech.in](mailto:prem@signaturetech.in))
* ResponseBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
* Written and copyrighted &copy;2022 by Prem Chand Saini ([prem@signaturetech.in](mailto:prem@signaturetech.in))
* ResponseBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
},
"autoload": {
"psr-4": {
"Prem\\ResponseBuilder\\": "src/",
"Prem\\ResponseBuilder\\Tests\\": "tests"
"Pcsaini\\ResponseBuilder\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Prem\\ResponseBuilder\\Tests\\": "tests"
"Pcsaini\\ResponseBuilder\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Prem\\ResponseBuilder\\ResponseBuilderServiceProvider"
],
"aliases": {
"response_builder": "Prem\\ResponseBuilder\\Facades\\ResponseBuilder"
}
"Pcsaini\\ResponseBuilder\\ResponseBuilderServiceProvider"
]
}
}
}
30 changes: 17 additions & 13 deletions config/api-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

return [

/**
* default HTTP code for validation failed.
*/
/*
| --------------------------------------------------------------------------
| Default HTTP Code for Failed Validation.
| --------------------------------------------------------------------------
*/
'validation_http_code' => \Illuminate\Http\Response::HTTP_BAD_REQUEST,

/**
* default Validation fail message
*
* first | all
*
* first => Display the first validation error message
* all => Display all errors messages
*
*/
'show_validation_failed_message' => 'all',
/*
|--------------------------------------------------------------------------
| Show Validation Message
|--------------------------------------------------------------------------
|
| first | all
|
| first => Display the first validation error message
| all => Display all errors messages
|
*/
'show_validation_failed_message' => 'all',

];
Loading

0 comments on commit 1a56ba1

Please sign in to comment.