avo-hq / avo

Build Ruby on Rails apps 10x faster
https://avohq.io
Other
1.49k stars 232 forks source link

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

Open adrianthedev opened 2 years ago

adrianthedev commented 2 years ago

Proposal

Create API endpoints using the resource configuration.

Example

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

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?

tracyloisel commented 1 year ago

I upvote for this

iyerushalmi commented 1 year ago

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

adrianthedev commented 1 year ago

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

PedroAugustoRamalhoDuarte commented 5 months ago

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 commented 5 months ago

You rule @PedroAugustoRamalhoDuarte!

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