init / defaults / ラッパー
init.comat#
comatDir の直下に 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
}| 文 | 内容 |
|---|---|
baseUrl URL | 全エンドポイントの base url。実行時に環境変数 COMATYPE_BASE_URL や init の引数で上書きできる |
cookie 名前 { ... } | 発行する cookie の設定(ヘッダと cookie) |
wrapper 名前 { ... } | ラッパーの宣言 |
wrapper#
認証のような、複数のエンドポイントに共通する処理をラッパーとして宣言します。宣言には、ラッパーがハンドラに渡す値(ctx)の型と、ラッパー自身が返せるステータスを書きます。
comat
wrapper auth {
ctx {
user: UserData,
}
s401 ApiError
}実装は、各言語のコードで書きます(ラッパーと cookie)。init に渡す実装が、宣言と合っていないと、型検査でエラーになります(Go を除く)。
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書けるもの: wrap / cookie on|off / header headerReq headerRes / sNNN(共通のステータス)。path や body は書けません。cache は書いても効かず、警告が出ます(エンドポイントごとに書いてください)。
合成の順#
親の defaults.comat → そのディレクトリの defaults.comat(グループを含む)→ エンドポイントの順に合成し、後ろが勝ちます。
| 項目 | 合成のしかた |
|---|---|
wrap | 名前ごと。wrap auth は有効に、wrap auth off は外す |
cookie | エンドポイント側が勝つ |
| ヘッダ | ヘッダ名ごと |
sNNN | コードごと。wrapper のステータスは、そのラッパーが有効なエンドポイントに加わる |
wrap の順序(複数のラッパー)#
複数のラッパーが有効なときは、外側から内側へ、次の順に重なります。
- ディレクトリが上のファイルほど外側(親の
defaults.comat> 子のdefaults.comat> エンドポイント) - 同じ階層では、
defaults.comatが外側 - 同じファイルの中では、上に書いた
wrapほど外側
外側のラッパーが先に動き、レスポンスは内側から外側へ戻ります。wrap 名前 off は、その名前を取り除くだけで、残りの順は変わりません。下位で同じ名前をもう一度 wrap すると、下位の位置(内側)に置き直されます。
グループで切り替える#
(noauth) のようなグループの defaults.comat に wrap auth off と書くと、その配下だけ認証が外れます。
comat
import ApiError from ../../../types/common.comat
// user/(noauth)/post_login.comat: POST /user/login (what follows `_` is a static segment. The group (noauth) is not part of the path)
// Login is called while logged out. The auth wrapper is disabled in user/(noauth)/defaults.comat
body {
email: email,
password: string,
}
s200 {
uid: uuidv4,
}
// The auth wrapper is disabled, so the wrapper's s401 is not added. Declare 401 yourself here
s401 ApiErrorここでは wrap auth が外れているので、ラッパーの s401 は加わりません。ログイン失敗のために、s401 を自分で宣言しています。