ヘッダと cookie
ヘッダ#
| ブロック | 対象 |
|---|---|
header { ... } | リクエストとレスポンスの両方 |
headerReq { ... } | リクエストだけ |
headerRes { ... } | レスポンスだけ |
sNNN 型, { ... } | そのステータスのレスポンスだけ |
defaults.comat にも、エンドポイントにも書けます。ハイフンを含む名前は引用符で囲みます。? は省略可能です。
comat
// defaults.comat: default values for this directory and the endpoints below it. One can be placed per directory
// They are merged in this order: parent defaults.comat -> this directory's defaults.comat -> each endpoint's .comat (the later one wins)
// Project-specific declarations (base url, wrapper, cookie settings) go in init.comat directly under comatDir
import ApiError from ../types/common.comat
// wrap <name>: enable the wrapper <name> declared in init.comat
// wrap <name> off: disable it (use it to turn it off individually in a lower defaults.comat or an endpoint's .comat)
// When there are several wrappers, the one written higher is the outer one (runs first).
// Across levels, the upper directory is outer; at the same level, defaults.comat is outer (SPEC §3)
wrap auth
// cookie on|off: how httpOnly cookies (session cookies) are handled
// client: send and receive cookies (credentials: include in browsers, a cookie jar elsewhere). Client code does not read the value
// server: use the cookie settings of init.comat. Readable from wrappers
cookie on
// Headers: header is for both request and response, headerReq for the request only, headerRes for the response only
// Names containing a hyphen are quoted. `?` makes it optional
// Headers specific to a status go in the second block of `s200 <body>, { ... }` in each .comat
header {
"X-Request-Id"?: uuid,
}
headerReq {
"X-Client-Version"?: string,
}
headerRes {
"X-Server-Time"?: datetime,
}
// Common statuses are added to the Response of every endpoint below (if the same code exists, the lower definition wins)
// So the client does not need to pass s500 one by one (omitted in the examples)
// s401 is declared in init.comat as a status the auth wrapper can return
s500 ApiError優先順位#
ヘッダ名ごとに合成します。
ステータスごとのヘッダ > headerReq / headerRes > header
親の defaults.comat → 子の defaults.comat → エンドポイント、の順に合成し、後ろが勝ちます。
通信上の形#
ヘッダの値は文字列で届きます。ランタイムが、型に合わせて変換します。
| 型 | 変換 |
|---|---|
int | 整数にする |
bool | true / false |
| 配列 | ", " でつなぐ(受け取るときは , で分ける) |
datetime など | 文字列のまま検証する |
ヘッダ名は大文字小文字を区別しません。
cookie#
cookie on / cookie off#
そのエンドポイントで、cookie を送受信するかを決めます。defaults.comat に書いて、配下に効かせるのが普通です。
comat
cookie oncookie の宣言(init.comat)#
発行する cookie の名前と属性は、init.comat に 1 つ宣言します。
comat
// init.comat: project-specific settings. Place exactly one directly under comatDir
// Separate from the defaults.comat of each directory; it declares "what this project has"
import UserData from ../types/UserData.comat
import ApiError from ../types/common.comat
// The base url of all endpoints (paths are appended to it)
// At runtime it can be overridden by the COMATYPE_BASE_URL environment variable or the init argument of each language
baseUrl https://api.example.com
// Cookie settings used by the server (for `cookie on` endpoints). It is httpOnly, so js cannot read it
cookie session {
httpOnly: true,
secure: true,
sameSite: lax,
}
// Wrapper declarations. Implement them in each language's code as ComatypeWrap values, export them, and import and pass them in init
// Each .comat refers to them by name with `wrap auth` / `wrap auth off`
wrapper auth {
// The type of the values a wrapper adds to ctx. On endpoints with `wrap auth` on, a handler can read ctx.user, typed
ctx {
user: UserData,
}
// The statuses this wrapper can return. They are added to the Response of endpoints with `wrap auth` on
s401 ApiError
}| キー | 値 |
|---|---|
httpOnly / secure | true / false |
sameSite | lax / strict / none(none には secure: true が必要) |
path | / で始まる文字列(既定は /) |
domain | 文字列 |
maxAge | 秒(0 以上の整数) |
未知のキーや不正な値は、comatype check がエラーにします。
cookie を発行する#
cookie on のエンドポイントで、レスポンスを作るときに cookie を渡します。httpOnly などの属性は、init.comat の設定が付きます。
TypeScript
return s200({ data: { uid }, cookie: { session: { value: token, maxAge: 3600 } } })
// 文字列だけでもよい: cookie: { session: token }
// 削除: cookie: { session: null }Go
return user_post_login.S200(&user_post_login.S200Res{UID: uid}).WithCookie(token, comatype.MaxAge(3600))
// 削除: .ClearCookie()Python
return S200(data=S200.Res(uid=uid), cookie=Cookie(token, max_age=3600))
# 削除: cookie=Cookie.clear()cookie off のエンドポイントで cookie を渡すと、サーバの誤りとして 500 になります(Set-Cookie は出しません)。
クライアントの cookie#
ブラウザは credentials: 'include' で、ブラウザに任せます。Node など cookie を保持しない環境では、cookie jar を渡します(ラッパーと cookie)。