tobyzerner / json-api-server

A JSON:API server implementation in PHP.
https://tobyzerner.github.io/json-api-server/
MIT License
63 stars 21 forks source link

Consider a class-based API #37

Closed tobyzerner closed 1 year ago

tobyzerner commented 3 years ago

It could look like:

class Posts extends ResourceType
{
    public static $type = 'posts';

    public function adapter(): AdapterInterface
    {
        return new EloquentAdapter(QuestionRevision::class);
    }

    public function scope(Builder $query, Context $context): void
    {
        $query->where('user_id', $context->getRequest()->getAttribute('userId'));
    }

    public function endpoints(): array
    {
        return [
            Endpoint::show(),
            Endpoint::create()->authorized(fn($context) => true),
            Endpoint::update(),
        ];
    }

    public function fields(): array
    {
        return [
            String::make('title')->writable(),
            String::make('body')->writable(),
            Boolean::make('isPublished')->writable(),
        ];
    }
}

Please comment below if you have any thoughts about this vs. the current API.

tobyzerner commented 3 years ago

@franzliedke @smtlab any thoughts on this?

smtlab commented 3 years ago

This is good, but the current closure-based API is flexible and can be hooked into any existing MVC controller, Service or framework. Class-based API would be a good dedicated implementation specific to this package.

franzliedke commented 3 years ago

I like it, but also think it should be optional.

tobyzerner commented 2 years ago

TODO: look at https://www.django-rest-framework.org and https://django-ninja.rest-framework.com/ for inspiration?

tobyzerner commented 1 year ago

New class-based API released in v1.0.0-alpha.1! Sorry, I dumped the old API because the new one is vastly superior :)