Headers and cookies

Headers#

BlockApplies 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.

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

Priority#

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.

TypeConversion
intMade an integer
booltrue / false
ArrayJoined with ", " (split on , when received)
datetime and the likeValidated as strings

Header names are case-insensitive.

Cookies#

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.

comat
cookie on

Declare the name and attributes of the cookie to issue once in init.comat.

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
}
KeyValue
httpOnly / securetrue / false
sameSitelax / strict / none (none needs secure: true)
pathA string starting with / (default /)
domainA string
maxAgeSeconds (an integer, 0 or more)

comatype check reports unknown keys and invalid values as errors.

In an endpoint with cookie on, pass cookie when you build the response. Attributes such as httpOnly come from the init.comat settings.

TypeScript
return s200({ data: { uid }, cookie: { session: { value: token, maxAge: 3600 } } })
// A plain string also works: cookie: { session: token }
// Delete: cookie: { session: null }

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).