sarav/descriptive-api

A Simple Laravel Package providing descriptive API methods

0.0.2 2023-01-03 07:38 UTC

This package is auto-updated.

Last update: 2024-09-30 02:02:51 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

A Simple Laravel Package providing descriptive API methods

Install Composer package

Open your terminal and navigate to your laravel folder. Now run the following command

composer require sarav/descriptive-api

That's all is required for the package setup.

Usage

Following are the list of available methods

Now you can start using the methods as follows

ok()

public function index()
{
    $articles = Article::paginate();
    return response()->ok($articles);
}

created

public function store(StoreArticleRequest $request)
{
    $article = Article::create($request->validated());
    return response()->created($article);
}

noContent

public function destroy(Article $article)
{
    $article->delete();
    return response()->noContent();
}

badRequest

public function store(Request $request)
{
    if ($request->age < 0) {
      return response()->badRequest();
    }
}

unauthorized

public function store(Request $request)
{
    if (!auth()->check()) {
      return response()->unauthorized();
    }
}

forbidden

public function store(Request $request)
{
    $user = $request->user();
    if (!$user->isAdmin()) {
        return response()->forbidden();
    }
}

notFound

public function update(Request $request, $id)
{
    $article = Article::find($id);
    if (!$article) {
        return response()->notFound();
    }
}

validationFailure()

public function update(Request $request, $id)
{
    $validator = Validator::make([
      'comment' => 'required'
    ], $request->all());
    
    if ($validator->fails()) {
        return response()->validationFailure('Validation failure', $validator->errors());
    }
}

internalServerError()

public function update(Request $request, $id)
{
    try {
        // Your Business Logic
    } catch (Exception $e) {
        return response()->internalServerError();
    }
}

Overriding Default Messages

If you wish to override the default message, then simply provide your default message as param. That's it.

For example,

public function store(Request $request)
{
    // Overriding badRequest()
    if ($request->age < 0) {
      return response()->badRequest('Invalid age provided');
    }
    
    // Overriding unauthorized()
    if (!auth()->check()) {
      return response()->unauthorized('User not authorized');
    }
  
    // Overriding forbidden()
    $user = $request->user();
    if (!$user->isAdmin()) {
        return response()->forbidden('Access denied. Please contact administrator');
    }
    
    // Overriding notFound()
    $article = Article::find($id);
    if (!$article) {
        return response()->notFound('Article not found');
    }
    
    // Overriding validationFailure()
    $validator = Validator::make([
      'comment' => 'required'
    ], $request->all());
    
    if ($validator->fails()) {
        return response()->validationFailure('Please check the errors', $validator->errors());
    }
}

For more information check out this article.