`hanami new --api` for JSON

オープン
#33 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
28/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
活発
技術スタック
json, ruby
領域
api, backend, cli

調査の方向性

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/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.
主要言語
言語のデータがありません
スター
3
フォーク
0
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

hanami/roadmap のほかの issue

hanami/roadmap の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。