エンドポイント
1 つの .comat(init.comat と defaults.comat 以外)が、1 つのエンドポイントです。メソッドと path はファイルの置き場所と名前から決まります(path の決め方)。
書けるもの#
| 文 | 内容 |
|---|---|
import 型 from パス | 別のファイルの型を使う(パスはこのファイルからの相対) |
path /fix/path / path ./relative | path の明示(非推奨)。path の決め方を参照 |
params { ... } | path パラメータ([id] のディレクトリ名・ファイル名に対応) |
search { ... } | クエリ文字列 |
body 型 / body { ... } | リクエストのボディ。エンドポイントに 1 つ。無ければボディなし |
header { } headerReq { } headerRes { } | ヘッダ(ヘッダと cookie) |
sNNN 型 | ステータスごとのレスポンスのボディ(ステータスコード) |
wrap 名前 / wrap 名前 off | ラッパーの有効・無効(init / defaults / ラッパー) |
cookie on / cookie off | cookie を送受信するか |
cache off / cache 5000 / cache 5000 cookie | GET / QUERY のレスポンスのキャッシュ(キャッシュ)。defaults.comat に書いても効きません |
defaults.comat に書ける wrap cookie ヘッダ sNNN は、ここに書いても構いません(エンドポイント側が勝ちます)。
例: PUT /user/:id#
comat
import UserData from ../../types/UserData.comat
import ApiError from ../../types/common.comat
// user/put__id.comat: the method is put, and the path is /user/:id from the location and file name
// A segment starting with `__` is a dynamic path. The type of the name (id) is declared in params
params {
id: uuidv4,
}
body UserData
s200 UserData
s404 ApiErrorparams { id: uuidv4 }は、path の[id]の型を宣言します。path にある:idとparamsは、過不足なく一致しなければエラーです。body UserDataは、UserData型のボディを受け取ります。s200 UserDataとs404 ApiErrorは、ステータスごとのレスポンスです。
例: クエリ文字列と省略可能なフィールド#
comat
import UserData from ../types/UserData.comat
import ApiError from ../types/common.comat
// The path is explicit (deprecated). To follow the file location, use user/get__id.comat -> /user/:id
path /user/:id
// Path parameter
params {
id: uuidv4,
}
// Query string (?verbose=true). `?` makes it optional
search {
verbose?: bool,
}
// There is no `body`, so the request has no body
s200 UserData
s404 ApiError
// User information must show updates immediately, so it is not cached (`cache off`).
// If you write nothing, it is not cached anyway. `cache off` is a marker that states the intent
cache offverbose?: bool の ? は、省略できるという意味です。
例: ボディの組み立て(... と empty)#
comat
// (noauth)/post_user.comat: the method is the file name (post), and the path is /user from the location and file name (the group (noauth) is not part of the path)
import UserData from ../../types/UserData.comat
// Sign-up is called while logged out. The auth wrapper is disabled in (noauth)/defaults.comat
// body <type>: the request body (client -> server). One per endpoint. Without `body`, there is no body
body {
...UserData,
uid: empty,
}
// s[status code] <type>: the response body for that status (server -> client, nullable)
// `...UserData` spreads the fields, and `uid: empty` omits the key (the server does not send it)
s200 UserData...UserDataは、UserDataのフィールドをその場に展開します。uid: emptyは、そのフィールドを消します(キーごと省略。送れません)。「登録のとき、uidはサーバが決める」といった型を、UserDataから作れます。
メソッド#
get post put patch delete options query head。query は HTTP の QUERY メソッド(検索条件をボディで送る、安全で冪等な呼び出し)です。
comat
import UserData from ../types/UserData.comat
// QUERY: safe and idempotent like GET, but sends the search criteria in the body
body {
name?: string,
email?: email,
limit?: int,
}
s200 UserData[]comat
// OPTIONS: ask which methods the path supports
// The second block of s200 is the response headers (headers only for this status. Optional)
// An array of headers becomes a single string joined with ", " on the wire
s200 none, {
Allow: string[],
}規則#
- 同じメソッドと path が 2 つあると、エラーです(グループが違っても)。
bodyが 2 つあるとエラーです。paramsに省略可能なフィールドは書けません。defaults.comatにbodyparamssearchpathは書けません。