oktorino/midresapi

Return consistency json structure, handling exception response, handling validation exception

v1.2.1 2021-09-30 08:34 UTC

This package is auto-updated.

Last update: 2024-10-13 04:31:28 UTC


README

Quality Score Total Downloads Latest Stable Version License

Midres api , return response with consistency structure in json,

  • simple use.
  • simple return models,collecton and also fractal transofrmer
  • Handling exception.
  • Handling validation exception.

Installation

Requires :

  • PHP 7.2 * or latest
  • Laravel 7,8, or latest

Installation Midresapi in Composer:

composer require oktorino/midresapi --dev

Register to MiddlewareRoute

Configure in kernel.php, and place at middllewareRoute :

'midresapi' => \Oktorino\Midresapi\ConsistencyStructure::class;

then call this "midresapi" on your api routes. example in api.php :

Route::get('/', function(){
    return response("response work !!!");
})->middleware("midresapi");

Usage & Example

return success message

 return "successs";
 #or
 return response("success");

response :

{
 "status_code": 200,
 "success": true,
 "message": "success",
 "data": null
}

return with data

return model;

 $user=\App\User::first();
 return response($user);

response :

{
   "status_code": 200,
   "success": true,
   "message": "ok",
   "data": {
       "id": 1,
       "email": "admin@basahinajadeh.com",
       "username": "admin",
   }
}

return collection;

 $users=\App\User::limit(2)->get();
 return response($users);

response :

{
   "status_code": 200,
   "success": true,
   "message": "ok",
   "data": [
       {
           "id": 5532,
           "email": "dancuk@dummy.com"
       },
       {
           "id": 5531,
           "email": "jancuk@dummy.com"
       }
   ]
}

that also support return fractal in tranformer

Failed Response

  • reponse mistakes
{
   "status_code": 500,
   "success": false,
   "message": "Something went wrong !!!",
   "data": null,
   "errors": [
       {
           "message": "Undefined variable: undefine_var",
           "file": "/home/tuah/apps/consistency_response/routes/api.php",
           "line": 24
       }
   ]
}
  • Mistake on purpose ex :
  return response("can't be proccess");
{
   "status_code": 422,
   "success": false,
   "message": "can't proccess",
   "data": null
}
  • Return failed validation
{
    "status_code": 422,
    "success": false,
    "message": "The given data was invalid.",
    "data": null,
    "errors": {
        "validation": [
            "The attendance no field is required."
        ]
    }
}

License

midrespi createb by Tuah Oktorino.