一覧取得の設計
メモの一覧を返す操作(List)を書き足します。
| メソッド | パス | 中身 |
|---|---|---|
GET |
/notes |
メモの一覧を返す |
api/openapi.yaml を変更する
Section titled “api/openapi.yaml を変更する”66 - url: http://localhost:300077paths:88 /notes:9 get:10 summary: メモの一覧を返す11 responses:12 '200':13 description: メモの一覧14 content:15 application/json:16 schema:17 type: array18 items:19 type: object20 required: [id, title, body, createdAt, updatedAt]21 properties:22 id:23 type: string24 format: uuid25 title:26 type: string27 body:28 type: string29 createdAt:30 type: string31 format: date-time32 updatedAt:33 type: string34 format: date-time935 post:1036 summary: メモを作る1137 requestBody:/notesにgetを足します。同じパスに、メソッドごとの操作を並べて書きます- 一覧なので、応答は
type: arrayで、itemsに 1 件の形を書きます
クエリー文字列のパラメーターの書き方
Section titled “クエリー文字列のパラメーターの書き方”/notes?limit=10 の ? より後ろを クエリー文字列 と呼びます。一覧の件数や絞り込みの条件を渡すのに使います。このハンズオンの一覧では使いませんが、書くときはパスパラメーターと同じく parameters に書きます。
/notes: get: parameters: - name: limit in: query required: false schema: type: integer minimum: 1in: query… クエリー文字列の値だという意味ですrequired: false… 省略できる値にします。パスパラメーターと違い、省略できるようにすることが多いです- パス(
/notes)には書きません。/notes?limit=10も/notesも、同じgetの操作になります
プレビューで確かめる
Section titled “プレビューで確かめる”VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 GET /notes が増え、応答がメモの配列になっていれば大丈夫です。
参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- OpenAPI Specification v3.0.3 … Schema Object の
type: arrayとitems、Parameter Object のin: query