tempest-framework icon indicating copy to clipboard operation
tempest-framework copied to clipboard

[WIP] feat(api): add API "builder"

Open erikaraujo opened this issue 9 months ago • 1 comments

[!WARNING] This is VERY MUCH a Working in Progress.

Discord discussion here.

This is just to show-case an idea/PoC for a an API Development solution for Tempest.

Here's how I envision a developer would use such a feature:

Sample File structure

Just like the rest of Tempest, the file structure DOES NOT matter - however, just for the sake of having an example, here it is.

.
└── app/
    ├── ApiResources/
    │   └── UserResource.php
    └── Models/
        ├── Post.php
        └── User.php

Using in a dedicated resource class

Here's how a UserResource class could look like.

#[ResourceRecord(User::class)]
final class UserResource
{
    use IsApiResource;
}

Using directly in a model (🤮)

final class Post
{
    use IsApiResource;

    public Id $id;

    public string $title;

    //...
}

Using Only certain CRUD endpoints

#[ResourceRecord(User::class)]
final class UserResource
{
    use HasApiIndexEndpoint;
    use HasApiShowEndpoint;
}

Overwriting defaults

In any class that uses IsApiResource, defaults can be overwritten:

#[ResourceRecord(User::class)]
final class UserResource
{
    use IsApiResource;
    
    public static function getResourceApiVersion(): ?string
    {
        return 'v2';
    }

    public static function getResourcePagination(): ?Pagination
    {
        return new OffsetPagination();
    }

    public static function getResourceSearchableColumns(): ?array
    {
        return ['name', 'email'];
    }

    public static function getResourceMiddlewares(): array
    {
       return [
            LogRequestMiddleware::class,
        ];
    }
}

erikaraujo avatar May 14 '25 22:05 erikaraujo

Here is the related issue #928

gturpin-dev avatar May 15 '25 06:05 gturpin-dev

This pull request is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jun 15 '25 03:06 github-actions[bot]

This pull request was closed because it has been inactive for 1 day since being marked as stale.

github-actions[bot] avatar Jun 17 '25 02:06 github-actions[bot]