Mandraled-Studios / filament-api-service

A simple api service for supporting filamentphp

Home Page:https://filamentphp.com/plugins/rupadana-api-service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A simple api service for supporting filamentphp

Total Downloads Fix Code Run Test

Installation

You can install the package via composer:

composer require rupadana/filament-api-service

Register it to your filament Provider

use Rupadana\ApiService\ApiServicePlugin;

$panel->plugins([
    ApiServicePlugin::make()
])

Usage

php artisan make:filament-api-service BlogResource

From version 3.0, routes automatically registered. it will grouped as '/api/admin'. admin is panelId.

So, You don't need to register the routes manually.

The routes will be :

  • [GET] '/api/admin/blogs' - Return LengthAwarePaginator
  • [GET] '/api/admin/blogs/1' - Return single resource
  • [PUT] '/api/admin/blogs/1' - Update resource
  • [POST] '/api/admin/blogs' - Create resource
  • [DELETE] '/api/admin/blogs/1' - Delete resource

On CreateHandler, you need to be create your custom request validation.

Token Resource

By default, Token resource only show on super_admin role. you can modify give permission to other permission too.

Token Resource is protected by TokenPolicy. You can disable it by publishing the config and change this line.

'models' => [
        'token' => [
            'enable_policy' => false // default: true
        ]
    ],

Filtering & Allowed Field

We used "spatie/laravel-query-builder": "^5.3" to handle query selecting, sorting and filtering. Check out the spatie/laravel-query-builder documentation for more information. You can specified allowedFilters and allowedFields in your model. For example:

class User extends Model {
    // Which fields can be selected from the database through the query string
    public static array $allowedFields = [
        'name'
    ];

    // Which fields can be used to sort the results through the query string
    public static array $allowedSorts = [
        'name',
        'created_at'
    ];

    // Which fields can be used to filter the results through the query string
    public static array $allowedFilters = [
        'name'
    ];
}

Create a Handler

To create a handler you can use this command. By default, i'm using CreateHandler

php artisan make:filament-api-handler BlogResource

or

php artisan make:filament-api-handler Blog

Transform API Response

php artisan make:filament-api-transformer Blog

it will be create BlogTransformer in App\Filament\Resources\BlogResource\Api\Transformers

<?php
namespace App\Filament\Resources\BlogResource\Api\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class BlogTransformer extends JsonResource
{

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return $this->resource->toArray();

        // or

        return [
            "modified_name" => $this->name . ' so Cool!'  
        ];
    }
}

next step you need to edit & add it to your Resource

    use App\Filament\Resources\BlogResource\Api\Transformers\BlogTransformer;

    class BlogResource extends Resource
    {
        ...
        public static function getApiTransformer() 
        { 
            return BlogTransformer::class; 
        }
        ...
    }

Group Name & Prefix

You can edit prefix & group route name as you want, default this plugin use model singular label;

    class BlogApiService extends ApiService
    {
        ...
        protected static string | null $groupRouteName = 'myblog';
        ...
    }

How to secure it?

From version 3.0, it will automatically detect routes and secure it using sanctum.

To Generate Token, you just need create it from admin panel. It will be Token Resource there.

Image

Public API

Set API to public by overriding this property on your API Handler. Assume we have a PaginationHandler

class PaginationHandler extends Handlers {
    public static bool $public = true;
}

TODO

  • Test Plugin for Tenancy purpose
  • Each user can manage their own token only

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.

Supported By

About

A simple api service for supporting filamentphp

https://filamentphp.com/plugins/rupadana-api-service

License:MIT License


Languages

Language:PHP 96.8%Language:JavaScript 3.1%Language:CSS 0.1%