コンテンツにスキップ

共通部分の設計

03 章 共通部分の設計

ここまでで、メモの形を 4 回、id のパラメーターを 3 回、同じように書きました。同じものは components にまとめ、$ref で参照します。片方だけ直し忘れることがなくなります。

api/openapi.yaml
16
16
schema:
17
17
type: array
18
18
items:
19
type: object
20
required: [id, title, body, createdAt, updatedAt]
21
properties:
22
id:
23
type: string
24
format: uuid
25
title:
26
type: string
27
body:
28
type: string
29
createdAt:
30
type: string
31
format: date-time
32
updatedAt:
33
type: string
34
format: date-time
19
$ref: '#/components/schemas/Note'
35
20
post:
36
21
summary: メモを作る
37
22
requestBody:
api/openapi.yaml
39
24
content:
40
25
application/json:
41
26
schema:
42
type: object
43
additionalProperties: false
44
required: [title]
45
properties:
46
title:
47
type: string
48
minLength: 1
49
maxLength: 100
50
body:
51
type: string
52
maxLength: 1000
53
default: ''
27
$ref: '#/components/schemas/CreateNoteInput'
54
28
responses:
55
29
'201':
56
30
description: 作ったメモ
57
31
content:
58
32
application/json:
59
33
schema:
60
type: object
61
required: [id, title, body, createdAt, updatedAt]
62
properties:
63
id:
64
type: string
65
format: uuid
66
title:
67
type: string
68
body:
69
type: string
70
createdAt:
71
type: string
72
format: date-time
73
updatedAt:
74
type: string
75
format: date-time
34
$ref: '#/components/schemas/Note'
76
35
'400':
77
description: 入力が決まりに合わない
36
$ref: '#/components/responses/BadRequest'
78
37
/notes/{id}:
79
38
get:
80
39
summary: メモを 1 件返す
81
40
parameters:
82
- name: id
83
in: path
84
required: true
85
schema:
86
type: string
87
format: uuid
41
- $ref: '#/components/parameters/NoteId'
88
42
responses:
89
43
'200':
90
44
description: メモ
91
45
content:
92
46
application/json:
93
47
schema:
94
type: object
95
required: [id, title, body, createdAt, updatedAt]
96
properties:
97
id:
98
type: string
99
format: uuid
100
title:
101
type: string
102
body:
103
type: string
104
createdAt:
105
type: string
106
format: date-time
107
updatedAt:
108
type: string
109
format: date-time
48
$ref: '#/components/schemas/Note'
110
49
'400':
111
description: ID が UUID の形でない
50
$ref: '#/components/responses/BadRequest'
112
51
'404':
113
description: メモが無い
52
$ref: '#/components/responses/NotFound'
114
53
put:
115
54
summary: メモを置き換える(全部の項目を送る)
116
55
parameters:
117
- name: id
118
in: path
119
required: true
120
schema:
121
type: string
122
format: uuid
56
- $ref: '#/components/parameters/NoteId'
123
57
requestBody:
124
58
required: true
125
59
content:
126
60
application/json:
127
61
schema:
128
type: object
129
additionalProperties: false
130
required: [title, body]
131
properties:
132
title:
133
type: string
134
minLength: 1
135
maxLength: 100
136
body:
137
type: string
138
maxLength: 1000
62
$ref: '#/components/schemas/UpdateNoteInput'
139
63
responses:
140
64
'200':
141
65
description: 書き換えたメモ
142
66
content:
143
67
application/json:
144
68
schema:
145
type: object
146
required: [id, title, body, createdAt, updatedAt]
147
properties:
148
id:
149
type: string
150
format: uuid
151
title:
152
type: string
153
body:
154
type: string
155
createdAt:
156
type: string
157
format: date-time
158
updatedAt:
159
type: string
160
format: date-time
69
$ref: '#/components/schemas/Note'
161
70
'400':
162
description: 入力が決まりに合わない
71
$ref: '#/components/responses/BadRequest'
163
72
'404':
164
description: メモが無い
73
$ref: '#/components/responses/NotFound'
165
74
delete:
166
75
summary: メモを消す
167
76
parameters:
168
- name: id
169
in: path
170
required: true
171
schema:
172
type: string
173
format: uuid
77
- $ref: '#/components/parameters/NoteId'
174
78
responses:
175
79
'204':
176
80
description: 消した
177
81
'400':
178
description: ID が UUID の形でない
82
$ref: '#/components/responses/BadRequest'
179
83
'404':
180
description: メモが無い
84
$ref: '#/components/responses/NotFound'
85
components:
86
parameters:
87
NoteId:
88
name: id
89
in: path
90
required: true
91
schema:
92
type: string
93
format: uuid
94
schemas:
95
Note:
96
type: object
97
required: [id, title, body, createdAt, updatedAt]
98
properties:
99
id:
100
type: string
101
format: uuid
102
title:
103
type: string
104
body:
105
type: string
106
createdAt:
107
type: string
108
format: date-time
109
updatedAt:
110
type: string
111
format: date-time
112
CreateNoteInput:
113
type: object
114
additionalProperties: false
115
required: [title]
116
properties:
117
title:
118
type: string
119
minLength: 1
120
maxLength: 100
121
body:
122
type: string
123
maxLength: 1000
124
default: ''
125
UpdateNoteInput:
126
type: object
127
additionalProperties: false
128
required: [title, body]
129
properties:
130
title:
131
type: string
132
minLength: 1
133
maxLength: 100
134
body:
135
type: string
136
maxLength: 1000
137
responses:
138
BadRequest:
139
description: 入力が決まりに合わない
140
NotFound:
141
description: メモが無い
components の中 まとめたもの
schemas メモの形(Note)、作るときの本文(CreateNoteInput)、書き換えるときの本文(UpdateNoteInput)
parameters パスの id(NoteId)
responses 400(BadRequest)・404(NotFound)
  • $ref: '#/components/schemas/Note' は、「このファイルの components → schemas → Note」を指します。Cmd(Windows・Linux は Ctrl)を押しながらクリックすると、参照先へ移動します
  • schemas に付けた名前は、04 章で作る TypeScript の型の名前にもなります

VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 前の節と同じ 5 つの操作が、同じ形で見えれば大丈夫です。まとめても、約束の中身は変わりません。

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

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

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