`hanami new --api` for JSON
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 28/100
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- 言語のデータがありません
- スター
- 3
- フォーク
- 0
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hanami/roadmap のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 15/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 15/100
似ている issue
-
simplify
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
zed-industries/delta-claude#50 ·
メンテナーはふだん 1 日以内に返信
-
source defect
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
mampfes/hacs_waste_collection_schedule#8269 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
traceroot-ai/traceroot#3319 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100