Willywes / ApiResponse by Willywes

Class to generate a standard structure for api json responses
1,318
0
2
Package Data
Maintainer Username: Willywes
Maintainer Contact: alejandro.isla.c@gmail.com (Alejandro Isla)
Package Create Date: 2020-10-22
Package Last Update: 2023-09-12
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-17 03:08:29
Package Statistics
Total Downloads: 1,318
Monthly Downloads: 10
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

ApiResponse

Latest Version on Packagist Total Downloads Build Status StyleCI

Class to generate a standard structure for api json responses.

Installation

Via Composer

$ composer require willywes/apiresponse

Usage

Imports

use Willywes\ApiResponse\ApiResponse;

Functions of Control (HTTP/200 OK)

Default functions that always return a http 200 code, but have a control state.

Params

|Param |Description | |---------------|---------------------------------------| |data |array of data for response (allow null)| |message |custom message to response (optional) | |title |custom title to response (optional) |

Functions

|Function |Description | |--------------------------------|-------------------------------------------------| |JsonSuccess | Response status "success" with HTTP 200 | |JsonError | Response status "error" with HTTP 200 | |JsonWarning | Response status "warning" with HTTP 200 | |JsonInfo | Response status "info" with HTTP 200 | |JsonFieldValidation | Response status "field_validation" with HTTP 200|

Examples

Success example
//Execution in php
return ApiResponse::JsonSuccess([
        'user' => User::first(),
        'roles' => Role::all(),
    ]);
//Response
{
    "status":"success",
    "title":"Operación Exitosa.",
    "message": null,
    "data":{
        "user":{
            "id":1,
            "full_name":"John Smith",
            "email":"jsmith@test.cl",
            "role_id":1
        },
        "roles":[
            {
                "id":1,
                "name":"God Admin"
            },
            {
                "id":2,
                "name":"Administrator"
            }
        ]
    }
}
//HTTP Response 
Status Code: 200 OK
Error example
//Execution in php
return ApiResponse::JsonError(null, 'something has gone wrong!', 'oops');
//Response
{
    "status":"error",
    "title":"oops",
    "message":"something has gone wrong!",
    "data": null
}
//HTTP Response 
Status Code: 200 OK

Functions with specific http code

Default functions that returns a specific http code, but in the same way the body responds

Params

|Param |Description | |---------------|---------------------------------------| |data |array of data for response (allow null)| |message |custom message to response (optional) |

Functions

|Function |Description | |--------------------------------|-------------------------------------------------| |Ok | Response status "error" with HTTP 200 | |BadRequest | Response status "error" with HTTP 400 | |Unauthorized | Response status "error" with HTTP 401 | |Forbidden | Response status "error" with HTTP 403 | |NotFound | Response status "error" with HTTP 404 | |InternalServerError | Response status "error" with HTTP 500 | |NotImplemented | Response status "error" with HTTP 501 | |BadGateway | Response status "error" with HTTP 502 |

Examples

404 Error example
//Execution in php
return ApiResponse::NotFound(null, 'object not found!');
//Response
{
   "status": "error",
   "message": "Not Found",
   "data": null
}
//HTTP Response 
Status Code: 404 Not Found
401 Error example
//Execution in php
return ApiResponse::Unauthorized(null);
//Response
{
   "status": "error",
   "message": "Unauthorized",
   "data": null
}
//HTTP Response 
Status Code: 401 Unauthorized

Credits

License

license. Please see the license file for more information.