Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filament Panel #45

Merged
merged 24 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4af1f3d
docs: Update for pre-release
adam-code-labx May 13, 2024
c372e7f
Merge branch 'develop'
adam-code-labx May 13, 2024
3df57f2
chore: release v1.0.0-rc.0
adam-code-labx May 13, 2024
bab9042
docs: Add info about tests + warning to update phpunit.xml
adam-code-labx May 13, 2024
c40a819
fix: No need to remove the API directory via XtendRouter
adam-code-labx May 13, 2024
eff7851
chore: release v1.0.0-rc.1
adam-code-labx May 13, 2024
79de5b1
fix: Include API key with test generation
adam-code-labx May 13, 2024
af9418e
chore: release v1.0.0-rc.2
adam-code-labx May 13, 2024
dd12bb6
docs: Update to use correct version + test failure advice
adam-code-labx May 13, 2024
ef891b9
fix: Hide duplicated logo for filament plugin
adam-code-labx May 14, 2024
908561b
Merge branch 'main' into develop
adam-code-labx May 18, 2024
5582afa
feat: Manage users & generate tokens
adam-code-labx Jun 2, 2024
f0ec0bf
fix: Remove view action for endpoints not needed right now
adam-code-labx Jun 3, 2024
ed71d28
feat: New filament command + auto commits
adam-code-labx Jun 6, 2024
0338a79
feat: Generate collection auto commit
adam-code-labx Jun 6, 2024
8c857d8
fix: Generate separate auth stub for sanctum overrides
adam-code-labx Jun 6, 2024
731988d
feat: Apply auth middleware when isAuthenticated is true
adam-code-labx Jun 6, 2024
d28e74c
feat: Improvements to endpoint resource & token generation
adam-code-labx Jun 6, 2024
0b57273
fix: Move type generation to rest-presenter directory
adam-code-labx Jun 6, 2024
4c3ac82
Fix styling
adam-code-labx Jun 6, 2024
f684ecf
docs: Update README to include Filament Plugin
adam-code-labx Jun 7, 2024
91502a4
style: Rector fixes
adam-code-labx Jun 7, 2024
55db517
style: Resolve phpstan issues
adam-code-labx Jun 7, 2024
de955ff
Fix styling
adam-code-labx Jun 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
# Changelog for RESTPresenter

## [1.0.0-rc.2](https://github.com/xtend-packages/rest-presenter/compare/1.0.0-rc.1...1.0.0-rc.2) (2024-05-13)


### Bug Fixes

* Include API key with test generation ([79de5b1](https://github.com/xtend-packages/rest-presenter/commit/79de5b1791e4209a32cea61c992704ac390514cf))

## [1.0.0-rc.1](https://github.com/xtend-packages/rest-presenter/compare/1.0.0-rc.0...1.0.0-rc.1) (2024-05-13)


### Bug Fixes

* No need to remove the API directory via XtendRouter ([c40a819](https://github.com/xtend-packages/rest-presenter/commit/c40a819c5f6defa2afdd39c49ad53dd6881569bf))


### Documentation

* Add info about tests + warning to update phpunit.xml ([bab9042](https://github.com/xtend-packages/rest-presenter/commit/bab904222f88e64a3d025519014334ab96c75f73))

## [1.0.0-rc.0](https://github.com/xtend-packages/rest-presenter/compare/0.11.0...1.0.0-rc.0) (2024-05-13)


### Features

* Extra Security API Key (Enabled by Default) ([b7baa38](https://github.com/xtend-packages/rest-presenter/commit/b7baa380aa8f6caa600a3f04b18a3f431a2d83a5))
* Improve tests + remove service provider ([151bb8b](https://github.com/xtend-packages/rest-presenter/commit/151bb8bc06c3b2ff1867dbdbca1f6b2928558f0c))


### Bug Fixes

* Skip RESTPresenterSetupCommand test issue with expectsChoice ([1a7ee6b](https://github.com/xtend-packages/rest-presenter/commit/1a7ee6b96262bc9a01975266f0eafcc568713659))


### Documentation

* Update for pre-release ([4af1f3d](https://github.com/xtend-packages/rest-presenter/commit/4af1f3d41ae724c9f8eca68cbe08e8ca4479ed8f))

## [0.11.0](https://github.com/xtend-packages/rest-presenter/compare/0.10.1...0.11.0) (2024-05-11)


Expand Down
214 changes: 130 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,156 +1,202 @@
<picture>
<picture class="filament-hidden">
<source media="(prefers-color-scheme: dark)" srcset="https://www.codelabx.ltd/assets/images/xtend-packages/rest-presenter/rest-presenter-banner-dark.png">
<img alt="XtendLaravel" src="https://www.codelabx.ltd/assets/images/xtend-packages/rest-presenter/rest-presenter-banner-light.png">
</picture>

[![Latest Version on Packagist](https://img.shields.io/packagist/v/xtend-packages/rest-presenter.svg?style=flat-square)](https://packagist.org/packages/xtend-packages/rest-presenter)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/xtend-packages/rest-presenter)](https://packagist.org/packages/xtend-packages/rest-presenter)
[![GitHub License](https://img.shields.io/github/license/xtend-packages/rest-presenter)](https://github.com/xtend-packages/rest-presenter/blob/main/LICENSE.md)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/xtend-packages/rest-presenter/tests.yml?label=tests)](https://github.com/xtend-packages/rest-presenter/actions/workflows/tests.yml)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/xtend-packages/rest-presenter/code-styling.yml?label=code%20style)](https://github.com/xtend-packages/rest-presenter/actions/workflows/code-styling.yml)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/xtend-packages/rest-presenter/phpstan.yml?label=static%20analysis)](https://github.com/xtend-packages/rest-presenter/actions/workflows/phpstan.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/xtend-packages/rest-presenter.svg?style=flat-square)](https://packagist.org/packages/xtend-packages/rest-presenter)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/adam-code-labx)](https://github.com/sponsors/adam-code-labx)
[![GitHub License](https://img.shields.io/github/license/xtend-packages/rest-presenter)](https://github.com/xtend-packages/rest-presenter/blob/main/LICENSE.md)

# RESTPresenter

RESTPresenter simplifies Laravel API development by providing a lightweight package that seamlessly integrates with Laravel API Resources and Spatie's powerful Data objects.

## Filament Quick Start
You can now use RESTPresenter to generate your API collection directly from any Filament project.\
Please follow the steps below to get started:

Install package ("minimum-stability": "dev", may be required in your composer.json file):
```bash
composer require xtend-packages/rest-presenter
````
Run the direct setup command for our Filament starter kit to avoid prompts:
```bash
php artisan rest-presenter:setup --starter-kit=Filament
```
To generate your API collection, run the following command below:
```bash
php artisan rest-presenter:generate-api-collection
```

Postman collection generated by default. We also support Insomnia simply set `REST_PRESENTER_EXPORT_PROVIDER=insomnia` in your .env file.

### The following directories with generated files will be created:
- `app/Api/StarterKits/Filament` (contains all generated resources)
- `resources/rest-presenter/postman` (generated Postman collection)
- `resources/rest-presenter/insomnia` (generated Insomnia collection)
- `resources/types/` (generated TypeScript DTOs)
<a href="https://discord.gg/j7EAhVVcyX" class="filament-hidden">
<img src="https://img.shields.io/discord/1033138523584331837?label=Discord&logo=discord" alt="Join our Discord">
</a>

## Starter Kits
Our initial release includes the following Starter Kits to jumpstart your development:
- [Sanctum Auth Starter Kit](https://github.com/xtend-packages/rest-presenter/discussions/categories/general)
- [Filament API Starter Kit](https://github.com/xtend-packages/rest-presenter/discussions/categories/filament-starter-kit)
## Introduction

RESTPresenter simplifies Laravel API development by providing a lightweight package that seamlessly integrates with Laravel API Resources and Spatie's powerful Data objects. Now, you can effortlessly generate your API collection for any Filament project.

## Key Features
### Key Features

- Effortless REST API creation with Laravel API Resources.
- Simplified data transformation using our Presenter layer.
- Built-in example filters and presenters for rapid API development.
- Testing support to ensure reliability and stability.
- Automating API creation from resources in Filament.
- TypeScript auto-generation for API resource DTOs.
- Postman and Insomnia collection generation for easy API testing.
- Secure your API collection with a security API key.
- Simple authentication middleware for your API resources.

## Planned Features
### Planned Features
- Release of Sponsorware API Kits for comprehensive API integration.
- Customisable kits providing a solid foundation for API development.
- Future open-sourcing of full kits upon reaching sponsorship milestones.
- Widget API generation for easy integration with your frontend.
- Social Kit for login and registration with social media platforms.

more to follow soon...
Check out our [Roadmap](https://github.com/orgs/xtend-packages/projects/1/views/1) for upcoming features and improvements. Feel free to open an issue for suggestions or feature requests. Join us on Discord to start a discussion and stay updated on the latest news.

## What Makes This Package Unique?
### What Makes This Package Unique?
RESTPresenter is more than just a CRUD generator. It offers:
- A Presenter layer for easy data transformation without modifying API resources.
- Compliance with standard features like OpenAPI, RESTful CRUD, filtering/pagination.
- Better business logic and direct access to required data for requests.
- Everything is extendable and customisable to fit your project's needs.
- API Kits to jumpstart your development with pre-built features and resources.

## Roadmap
Check out our [Roadmap](https://github.com/orgs/xtend-packages/projects/1/views/1) for upcoming features and improvements. Feel free to open an issue for suggestions or feature requests. We'll soon be launching our Discord for collaborative discussions.
Meanwhile start a discussion on GitHub to share your thoughts and ideas.
### So What Are Presenters?
Presenters are simply a way to transform data before it's sent to the client. They allow you to modify the data in any way you want, without modifying the API resources. This is especially useful when you need to transform data in a specific way for a particular endpoint before sending it to the client.

## Requirements
To use a presenter, simply add the header property `X-REST-PRESENTER: PresenterName` to your request. RESTPresenter will automatically apply the presenter to the data before sending it to the client.
Presenters can be used with collections and single resources. They can also be used with nested resources, allowing you to transform data at any level of the response.

## Installation & Requirements

- PHP ^8.2
- Laravel 10+

## Installation

You can install the package via composer:
Install the package via composer:

```bash
composer require xtend-packages/rest-presenter
composer require xtend-packages/rest-presenter:^1.0.0
```
Please note: if you receive an error during installing this package, you may need to update your minimum stability requirement to dev in your composer.json file:

```bash
> **Note:** If you encounter an error during installation, you may need to update your `minimum-stability` requirement to `dev` in your `composer.json` file:

```json
"minimum-stability": "dev",
```

## Setup
Customize RESTPresenter for your project with our setup command:
## Filament Plugin Integration

Manage your RESTPresenter resources directly from Filament with our dedicated plugin. This integration allows you to generate user tokens, manage your API resources, and more, all from within the Filament interface.

### Installation

To install the RESTPresenter Filament plugin, run the following command:

```bash
php artisan rest-presenter:setup
php artisan rest-presenter:filament --install
```
Select the Starter Kits to use during setup to configure the package according to your needs. The command also publishes the configuration file rest-presenter.php to your config directory and automatically registers the RESTPresenterServiceProvider.

## So What Are Presenters?
Presenters are simply a way to transform data before it's sent to the client. They allow you to modify the data in any way you want, without modifying the API resources. This is especially useful when you need to transform data in a specific way for a particular endpoint before sending it to the client.
This command will prompt you to auto-commit the changes to your Git repository. If you choose not to commit, you can manually commit the changes yourself.

To use a presenter, simply add the header property `X-REST-PRESENTER: PresenterName` to your request. RESTPresenter will automatically apply the presenter to the data before sending it to the client.
Presenters can be used with collections and single resources. They can also be used with nested resources, allowing you to transform data at any level of the response.
### Export API Collection

## Usage
To generate your API collection, run the following command:

### Generate Resources (Prompts)
To generate a new resource, use the following command:
```bash
php artisan rest-presenter:generate-api-collection
```

By default, this command generates a Postman collection. If you prefer Insomnia, you can switch by setting the following in your `.env` file:

```bash
php artisan rest-presenter:make-resource
REST_PRESENTER_EXPORT_PROVIDER=insomnia
```
This command will guide you through creating a new resource. Prompts will allow you to automatically generate presenters, filters, data, and set up your resource ready to use. All model relationships and fields are automatically detected throughout the prompt process. Additionally, we provide a custom option for most prompts to generate without auto-detection.

### Generate Resources (Blueprint)
Coming soon, leveraging the outstanding capabilities of [Laravel Blueprint](https://blueprint.laravelshift.com/)
### Uninstall

### Registering Kits
To uninstall the RESTPresenter Filament plugin, run the following command:

To register a kit, add it to the starterKits array in the register method of the RESTPresenterServiceProvider. For example:
```bash
php artisan rest-presenter:filament --uninstall
```

This command will prompt you to auto-commit and revert changes to your Git repository. If you choose not to commit, you can manually commit the changes yourself.

```php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use XtendPackages\RESTPresenter\Facades\RESTPresenter;
use XtendPackages\RESTPresenter\StarterKits\Auth\Sanctum\SanctumApiKitServiceProvider;
### RESTPresenter Panel
The new RESTPresenter panel serves as a centralized dashboard, offering a comprehensive overview and management interface for your API collection.

class RESTPresenterServiceProvider extends ServiceProvider
We have included the following features:
- **API Endpoints:** View all your API endpoints in one place especially useful when you update a resource to have authentication.


- **Token Generation:** Generate user tokens for secure API access you can define the token name, abilities, and expiration datetime. Simply copy the generated token to your API client to use with any authenticated endpoints.


- **Users Resource:** Detail view about users tokens from here you can revoke tokens.

> **Coming Soon:** Test Coverage and Reports just one of many features in active development.

### Endpoint Authentication & Security
By default, all resources do not include middleware for authentication. However your resources are protected by a security API key.\
You can make any resource public by simply updated the isAuthenticatable property in the resource file. Here's an example setting customer resource to public:

```php
<?php

class CustomerResourceController extends ResourceController
{
public function register(): void
{
RESTPresenter::register($this->app)
->starterKits([
SanctumApiKitServiceProvider::class,
]);
}
protected static string $model = Customer::class;

public static bool $isAuthenticated = true;

// ... rest of the resource controller
}
```

### Generated Files

The following directories with generated files will be created:

- `app/Api/StarterKits/Auth/Sanctum` (allows you to override the default sanctum actions)
- `app/Api/StarterKits/Filament` (contains all auto-generated resources for Filament)
- `resources/rest-presenter/postman` (generated Postman collection)
- `resources/rest-presenter/insomnia` (generated Insomnia collection)
- `resources/rest-presenter/types/` (generated TypeScript DTOs)
- `tests/StarterKits` (generated tests)


### Filament Test Suite

Tests are generated for each resource in the Filament test suite.

> **_Warning:_** To prevent overriding your database, update `phpunit.xml` with the following:
> ```xml
> <env name="DB_CONNECTION" value="sqlite"/>
> <env name="DB_DATABASE" value=":memory:"/>
> ```

- **Initial Test Expectations:** We anticipate that your initial tests will fail. This is a standard part of the testing process and is one of the reasons we provide a comprehensive test suite.


- **Identifying Issues:** The test suite aids in identifying any missing relationships or properties in your factories. It serves as a diagnostic tool to highlight areas that may need adjustment or improvement.


- **Field Matching and Types:** One specific aspect of the tests is to verify that fields match and return the expected data type. For instance, if a field is supposed to contain an integer but is instead a string, the test will fail. This aspect helps ensure data consistency and integrity.


- **Adjustments:** You're encouraged to make adjustments as needed based on the test results. This could involve modifying either the data object or the factory to align with the expected types and specifications.


## Standalone Laravel Setup

### Initial Setup
Customize RESTPresenter for your project with our setup command:

```bash
php artisan rest-presenter:setup
```
Select the Starter Kits to use during setup to configure the package according to your needs. The command also publishes the configuration file rest-presenter.php to your config directory and automatically registers the RESTPresenterServiceProvider.

### Generate Resources (Prompts)
To generate a new resource, use the following command:

```bash
php artisan rest-presenter:make-resource
```
This command will guide you through creating a new resource. Prompts will allow you to automatically generate presenters, filters, data, and set up your resource ready to use. All model relationships and fields are automatically detected throughout the prompt process. Additionally, we provide a custom option for most prompts to generate without auto-detection.

### Configuration

Configure RESTPresenter in the `config/rest-presenter.php` file to customize the package according to your project requirements.

## API Endpoints
Users can import this collection into their Insomnia application to quickly access and test the endpoints provided by RESTPresenter. This file is located [here](https://raw.githubusercontent.com/xtend-packages/rest-presenter/main/resources/insomnia/rest-presenter-Insomnia_2024-04-07.json)

## Testing
### Testing

RESTPresenter includes a comprehensive testing suite to ensure your API's reliability and stability. Test generators will soon be added to facilitate testing when creating new resources.

Expand Down
32 changes: 22 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xtend-packages/rest-presenter",
"version": "0.11.0",
"version": "1.0.0-rc.2",
"description": "REST API Presenter & Generator for Laravel",
"keywords": [
"code-labx",
Expand All @@ -19,6 +19,8 @@
],
"require": {
"php": "^8.2",
"calebporzio/sushi": "^2.5",
"knuckleswtf/scribe": "^4.35",
"laravel/framework": "^10|^11",
"laravel/prompts": "^0.1.16",
"laravel/sanctum": "^3.0|^4.0",
Expand All @@ -39,6 +41,7 @@
"pestphp/pest-plugin-faker": "^2.0",
"pestphp/pest-plugin-laravel": "^2.3",
"pestphp/pest-plugin-livewire": "^2.1",
"pestphp/pest-plugin-type-coverage": "^2.8",
"phpunit/phpunit": "^10",
"rector/rector": "^1.0.4",
"spatie/invade": "^1.1",
Expand All @@ -61,17 +64,25 @@
},
"scripts": {
"post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
"pest": "vendor/bin/pest --testsuite=RESTPresenter",
"pest-coverage": "vendor/bin/pest --testsuite=RESTPresenter --coverage",
"test:format": "pint --test",
"test:refactor": "rector --dry-run",
"test:types": "phpstan analyse",
"test:arch": "pest --filter=arch",
"test:type-coverage": "pest --type-coverage --min=100",
"test:unit": "pest --parallel --coverage --min=100",
"test:profile": "pest --profile --parallel",
"check:format": "pint --test",
"check:refactor": "rector --dry-run",
"check:types": "phpstan analyse",
"format": "vendor/bin/pint",
"refactor": "rector",
"check": [
"@check:format",
"@check:refactor",
"@check:types"
],
"test": [
"@test:format",
"@test:refactor",
"@test:types"
"@test:arch",
"@test:type-coverage",
"@test:unit",
"@test:profile"
]
},
"config": {
Expand All @@ -84,7 +95,8 @@
"extra": {
"laravel": {
"providers": [
"XtendPackages\\RESTPresenter\\RESTPresenterServiceProvider"
"XtendPackages\\RESTPresenter\\RESTPresenterServiceProvider",
"XtendPackages\\RESTPresenter\\StarterKits\\Filament\\FilamentPanelProvider"
]
}
},
Expand Down
Loading