https://github.com/trailblazer/trailblazer-endpoint
Generic HTTP endpoints to deal with different results from your operations.
https://github.com/trailblazer/trailblazer-endpoint
Last synced: 6 months ago
JSON representation
Generic HTTP endpoints to deal with different results from your operations.
- Host: GitHub
- URL: https://github.com/trailblazer/trailblazer-endpoint
- Owner: trailblazer
- Created: 2016-10-29T05:01:51.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-03-14T14:50:01.000Z (7 months ago)
- Last Synced: 2025-04-15T14:55:59.544Z (6 months ago)
- Language: Ruby
- Homepage:
- Size: 361 KB
- Stars: 23
- Watchers: 6
- Forks: 19
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
Awesome Lists containing this project
README
# Trailblazer::Endpoint
*Endpoints handle authentication, authorization, calling the business logic and response rendering.*
## Overview
An endpoint links your routing with your business code. The idea is that your controllers are pure HTTP routers, calling the respective endpoint for each action. From there, the endpoint takes over, handles authentication, policies, executing the domain code, interpreting the result, and providing hooks to render a response.
Instead of dealing with a mix of `before_filter`s, Rack-middlewares, controller code and callbacks, an endpoint is just another activity and allows to be customized with the well-established Trailblazer mechanics.
In a Rails controller, a controller action could look as follows.
```ruby
class DiagramsController < ApplicationController
endpoint Diagram::Operation::Create, [:is_logged_in?, :can_add_diagram?]def create
endpoint Diagram::Operation::Create do |ctx, **|
redirect_to diagram_path(ctx[:diagram].id)
end.Or do |ctx, **|
render :form
end
end
end
```While routing and redirecting/rendering still happens in Rails, all remaining steps are handled in the endpoint.
An API controller action, where the rendering is done generically, could look much simpler.
```ruby
class API::V1::DiagramsController < ApplicationController
endpoint Diagram::Operation::Create, [:is_logged_in?, :can_add_diagram?]def create
endpoint Diagram::Operation::Create
end
end
```Endpoints are easily customized but their main intent is to reduce fuzzy controller code and providing best practices for both HTML-rendering controllers and APIs.
## Documentation
Read the [full documentation for endpoint](https://trailblazer.to/2.1/docs/endpoint.html) on our website.