Headers and cookies
Headers#
| Block | Applies to |
|---|---|
header { ... } | Both the request and the response |
headerReq { ... } | The request only |
headerRes { ... } | The response only |
sNNN Type, { ... } | The response of that status only |
You can write them in defaults.comat and in an endpoint. Quote names that contain a hyphen. ? means optional.
// 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 ApiErrorPriority#
They are merged per header name.
Per-status headers > headerReq / headerRes > header
Merging goes parent defaults.comat → child defaults.comat → endpoint, and the later one wins.
On the wire#
Header values arrive as strings. The runtime converts them to match the type.
| Type | Conversion |
|---|---|
int | Made an integer |
bool | true / false |
| Array | Joined with ", " (split on , when received) |
datetime and the like | Validated as strings |
Header names are case-insensitive.
Cookies#
cookie on / cookie off#
Decides whether cookies are sent and received for that endpoint. It is usual to write it in defaults.comat so it applies to everything below.
cookie onDeclaring a cookie (init.comat)#
Declare the name and attributes of the cookie to issue once in init.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
}| Key | Value |
|---|---|
httpOnly / secure | true / false |
sameSite | lax / strict / none (none needs secure: true) |
path | A string starting with / (default /) |
domain | A string |
maxAge | Seconds (an integer, 0 or more) |
comatype check reports unknown keys and invalid values as errors.
Issuing a cookie#
In an endpoint with cookie on, pass cookie when you build the response. Attributes such as httpOnly come from the init.comat settings.
return s200({ data: { uid }, cookie: { session: { value: token, maxAge: 3600 } } })
// A plain string also works: cookie: { session: token }
// Delete: cookie: { session: null }return user_post_login.S200(&user_post_login.S200Res{UID: uid}).WithCookie(token, comatype.MaxAge(3600))
// Delete: .ClearCookie()return S200(data=S200.Res(uid=uid), cookie=Cookie(token, max_age=3600))
# Delete: cookie=Cookie.clear()Passing a cookie in an endpoint with cookie off is treated as a server mistake and gives 500 (no Set-Cookie is sent).
Cookies on the client#
In a browser, use credentials: 'include' and leave it to the browser. In environments that do not keep cookies, such as Node, pass a cookie jar (Wrappers and cookies).