`hanami new --api` for JSON

Đang mở
#33 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
28/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Sôi nổi
Công nghệ
json, ruby
Lĩnh vực
api, backend, cli

Hướng nghiên cứu

Begin by separating the generator work in hanami-cli from framework behavior in hanami, then trace PublicErrorsApp, webconsole, action halt, and automatic 406/415 responses. Done means an agreed scope and response-format/error-body contract for --api across development, production, unknown routes, exceptions, and action errors.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

There's no way to generate an app that speaks only JSON. Even with --skip-view and --skip-assets and config.actions.formats.accept :json, HTML or plain text still leaks out:

  • Production error responses are text/html unless the request sends Accept: application/json (PublicErrorsApp)
  • Development errors are never JSON (webconsole renders HTML or plain text; without it, exceptions bubble to the server)
  • The welcome page is HTML and shows on every fresh app
  • halt 404 and the automatic 406/415 return plain-text bodies

Why? I hate having to wrangle with the framework immediately to get it to give me the (basically ubiquitous API) format I want, then tracking down parsing errors later when error pages return as parsing issues instead of their actual errors. It's pretty tedious and annoying, and we can fix it for our users without a ton of work.

Parts

1. Generator only (hanami-cli)
--api implies --skip-view --skip-assets, sets formats.accept :json, omits hanami-webconsole, and generates a base action that overrides halt to emit JSON. Unknown routes and 500s would still be text/html for clients that don't send a JSON Accept header.

2. Plus JSON errors from the framework (hanami)
This extends hanami/hanami#1309, which already renders JSON errors, but only in production and only for requests that opt in with a JSON Accept header. Instead, every error response would follow config.actions.formats.default, so the generated app needs no overrides. Also the errors app and welcome page render JSON, in all environments, with exception detail when render_detailed_errors is on.

3. Make hanami-action responses respect their specified format
halt and the automatic 406/415 responess produce text bodies currently, but they should be JSON instead.

Questions

  • Error body shape: keep the {"status": 404, "error": "Not Found"} from hanami/hanami#1309 as the default, overridable via a lambda in config that receives the status and exception and returns a Hash. To give one consistent shape, it would need to cover errors from actions (halt, 406/415) as well as from the middleware (unknown routes, unhandled exceptions).
  • Slices: slices generated in an --api app are JSON-only automatically, since they inherit the app's action config and error rendering, so generate slice needs no --api flag. Mixing formats is future work (a JSON slice in an HTML app, or an HTML slice in an API app): errors are rendered app-wide, so that would need a per-slice error format, and an HTML slice in an API app would also need the view layer added.
  • Is --api the right name? It's what Rails uses, so may be familiar. I was thinking --api=json with --json as a shorthand would be more accurate, but also feels like a big YAGNI/KISS that overcomplicates things for little value.
Disclosure I worked with AI to write this. This is a feature I've wanted for a long time and AI helped me iron out the edge cases and reason about how far we'd want to take this, as well as figure out where fixes should go. It helped me find & fix hanami/action#531 and another similar issue outlined in that PR description.
Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
3
Fork
0
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của hanami/roadmap

Tất cả issue của hanami/roadmap

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.