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

[RFC] Expose REST API endpoints from Avo resources #1146

Open
adrianthedev opened this issue Aug 17, 2022 · 5 comments
Open

[RFC] Expose REST API endpoints from Avo resources #1146

adrianthedev opened this issue Aug 17, 2022 · 5 comments
Labels

Comments

@adrianthedev
Copy link
Collaborator

adrianthedev commented Aug 17, 2022

Proposal

Create a JSON API endpoints using the resource configuration.
Read-only in the initial iteration.

Example

A resource like the one below to expose a /resources/teams REST API endpoints for:

GET    /resources/teams(.:format)
POST   /resources/teams(.:format)
GET    /resources/teams/new(.:format)
GET    /resources/teams/:id/edit(.:format)
GET    /resources/teams/:id(.:format)
PATCH  /resources/teams/:id(.:format)
PUT    /resources/teams/:id(.:format)
DELETE /resources/teams/:id(.:format)
class TeamResource < Avo::BaseResource
  self.title = :name
  self.includes = [:admin, :team_members]

  field :id, as: :id
  field :name, as: :text, sortable: true
  field :url, as: :text
  field :logo, as: :external_image, as_avatar: :rounded do |model|
    if model.url
      "//logo.clearbit.com/#{URI.parse(model.url).host}?size=180"
    end
  end
end

Benefits

This could make scaffolding API endpoints very easy.

Actions

Actins could turn into their own paths, mapping parameters to fields and using their logic to execute that action

Filters

Filters could take in params and use their logic to filter the records in the response.

Views

Maybe add a new view with the visibility helpers hide_on: :json?

Approach (initial iteration)

Have a route and controller that automatically generates the endpoints. Each endpoint would expose the fields that have been configured in the resource.

We'll have a generator that the user runs to generate those controllers. Each controller will handle one resource.

Controller methods

Let's try to keep as much logic as we can in those controllers and allow the user to take control by overriding those methods similar to how we do it with destroy_success_action, and destroy_fail_action.

Property visibility

Each endpoint will take into account the visibility settings.
Ex: on /api/resources/teams each object will have only the properties that are visible on the Index view. Same for other views.

We might want to add a few other helpful methods like process_record where the user can take more control over the data manipulation.

Rest structure

Let's take inspiration from the greats. We don't want to reinvent the wheel here.

Endpoint versioning

Each controller/controller set will land in a version directory. The user can generate new versions of the API in new directories.

app/controllers/api/v1/products.rb

Endpoint structure

{
  records: [ ... ], # array of records
  pagination: { # open to suggestions
    page: 3,
    total_pages: 12
    ...
  }
}
@adrianthedev adrianthedev pinned this issue Aug 17, 2022
@tracyloisel
Copy link

I upvote for this

@iyerushalmi
Copy link

Hi Adrian, any chance this is on the roadmap for v3?

@adrianthedev
Copy link
Collaborator Author

I'd like to start working on that after we launch V3. We don't yet have it on the roadmap.

@PedroAugustoRamalhoDuarte
Copy link
Contributor

Hello @adrianthedev with the hype from Tropical.rb, i have made a small poc to this feature: https://github.com/PedroAugustoRamalhoDuarte/avo-api

It s a very minimal example for us to see the necessary work to make this feature. Only working for show endpoint right now

@adrianthedev
Copy link
Collaborator Author

You rule @PedroAugustoRamalhoDuarte!

Let me give it a spin next week.
Maybe we can release it as a separate gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

4 participants