`hanami new --api` for JSON
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
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/htmlunless the request sendsAccept: 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 404and 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
--apiapp are JSON-only automatically, since they inherit the app's action config and error rendering, sogenerate sliceneeds no--apiflag. 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
--apithe right name? It's what Rails uses, so may be familiar. I was thinking--api=jsonwith--jsonas 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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của hanami/roadmap
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
Hanami language serverĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
-
Decommission hanami-utilsĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
Tất cả issue của hanami/roadmap
Issue tương tự
-
simplify
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
zed-industries/delta-claude#50 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
source defect
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
mampfes/hacs_waste_collection_schedule#8269 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
traceroot-ai/traceroot#3319 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100