Giter Site home page Giter Site logo

dabrokezz / filament-api-service Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rupadana/filament-api-service

0.0 0.0 0.0 210 KB

A simple api service for supporting filamentphp

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

License: MIT License

Shell 0.13% JavaScript 2.39% PHP 97.40% CSS 0.08%

filament-api-service's Introduction

Filament Api Service

Total Downloads Fix Code Run Test

A simple API service for supporting FilamentPHP

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()
])

Config

php artisan vendor:publish --tag=api-service-config
return [
    'navigation' => [
        'token' => [
            'group' => 'User',
            'sort' => -1,
            'icon' => 'heroicon-o-key'
        ]
    ],
    'models' => [
        'token' => [
            'enable_policy' => true,
        ],
    ],
    'route' => [
        'panel_prefix' => true,
    ],
    'tenancy' => [
        'enabled' => false,
        'awareness' => false,
    ]
];

Usage

php artisan make:filament-api-service BlogResource

Since version 3.0, routes automatically registered. it will grouped as '/api/admin'. admin is panelId. to disable panelId prefix, please set route.panel_prefix to false

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 md5(json_encode($this->resource->toArray()));
    }
}

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';
        ...
    }

Tenancy

When you want to enable Tenancy on this package you can enable this by setting the config tenancy.enabled to true. This makes sure that your api responses only retreive the data which that user has access to. So if you have configured 5 tenants and an user has access to 2 tenants. Then, enabling this feature will return only the data of those 2 tenants.

If you have enabled tenancy on this package but on a specific Resource you have defined protected static bool $isScopedToTenant = false;, then the API will honour this for that specific resource and will return all records.

If you want to make api routes tenant aware. you can set tenancy.awareness to true in your published api-service.php. This way this package will register extra API routes which will return only the specific tenant data in the API response.

Now your API endpoints will have URI prefix of {tenant} in the API routes when tenancy.awareness is true.

It will look like this:

  POST      api/admin/{tenant}/blog
  GET|HEAD  api/admin/{tenant}/blog
  PUT       api/admin/{tenant}/blog/{id}
  DELETE    api/admin/{tenant}/blog/{id}
  GET|HEAD  api/admin/{tenant}/blog/{id}

Overriding tenancy ownership relationship name by adding this property to the Handlers protected static ?string $tenantOwnershipRelationshipName = null;

How to secure it?

Since 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;
}

License

The MIT License (MIT).

Supported By

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.