init / defaults / wrappers
init.comat#
Project-wide settings. Put exactly one directly under comatDir.
// 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
}| Statement | Meaning |
|---|---|
baseUrl URL | The base url of every endpoint. It can be overridden at run time with the COMATYPE_BASE_URL environment variable or an argument to init |
cookie name { ... } | The settings of the cookie to issue (Headers and cookies) |
wrapper name { ... } | Declares a wrapper |
wrapper#
Declare processing shared by several endpoints, such as authentication, as a wrapper. The declaration holds the type of the value the wrapper hands to the handler (ctx) and the statuses the wrapper itself can return.
wrapper auth {
ctx {
user: UserData,
}
s401 ApiError
}You write the implementation in each language's code (Wrappers and cookies). If the implementation you pass to init does not match the declaration, the type check reports an error (except in Go).
defaults.comat#
A file of default values that can be put in any directory. It applies to that directory and the endpoints below it.
// 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 ApiErrorWhat you can write: wrap / cookie on|off / header headerReq headerRes / sNNN (shared statuses). You cannot write path or body. cache has no effect and gives a warning (write it per endpoint).
Order of merging#
Merging goes parent defaults.comat → that directory's defaults.comat (including groups) → endpoint, and the later one wins.
| Item | How it merges |
|---|---|
wrap | Per name. wrap auth enables it, wrap auth off removes it |
cookie | The endpoint wins |
| Headers | Per header name |
sNNN | Per code. A wrapper's statuses are added to endpoints where that wrapper is enabled |
Order of wrap (several wrappers)#
When several wrappers are enabled, they nest from outside to inside in this order.
- The higher the directory of the file, the further out (parent
defaults.comat> childdefaults.comat> endpoint) - At the same level,
defaults.comatis further out - Within the same file, the
wrapwritten higher is further out
The outer wrapper runs first, and the response goes back from inside to outside. wrap name off only removes that name; the order of the rest does not change. If a lower level writes wrap for the same name again, it is placed again at the lower position (inside).
Switching by group#
Writing wrap auth off in the defaults.comat of a group such as (noauth) removes authentication only below it.
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 ApiErrorHere wrap auth is removed, so the wrapper's s401 is not added. s401 is declared by the endpoint itself, for a failed login.