コンテンツにスキップ

作成の設計

03 章 作成の設計

メモを作る操作(Create)を書き足します。

メソッド パス 中身
POST /notes 本文(JSON)からメモを作り、201 で返す
api/openapi.yaml
4
4
version: 1.0.0
5
5
servers:
6
6
- url: http://localhost:3000
7
paths: {}
7
paths:
8
/notes:
9
post:
10
summary: メモを作る
11
requestBody:
12
required: true
13
content:
14
application/json:
15
schema:
16
type: object
17
additionalProperties: false
18
required: [title]
19
properties:
20
title:
21
type: string
22
minLength: 1
23
maxLength: 100
24
body:
25
type: string
26
maxLength: 1000
27
default: ''
28
responses:
29
'201':
30
description: 作ったメモ
31
content:
32
application/json:
33
schema:
34
type: object
35
required: [id, title, body, createdAt, updatedAt]
36
properties:
37
id:
38
type: string
39
format: uuid
40
title:
41
type: string
42
body:
43
type: string
44
createdAt:
45
type: string
46
format: date-time
47
updatedAt:
48
type: string
49
format: date-time
50
'400':
51
description: 入力が決まりに合わない
  • paths → /notes → post … POST /notes の操作です
  • requestBody … 送る本文の形です。required に書いた title は必ず送り、body は省略すると '' になります。additionalProperties: false は、ほかの項目を受け付けないという意味です
  • id は送る本文に書きません。ID はサーバーが作ります。additionalProperties: false があるので、id を送っても受け付けません
  • responses … ステータスコードごとの応答です。キーは '201' のように文字列で書きます。400 は入力が決まりに合わないときです

VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 POST /notes が出て、送る本文と返るメモの形が見えれば大丈夫です。

この節は、次の公式ドキュメントをもとに書いています。

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えた状態をダウンロード