Endpoints

One .comat file (other than init.comat and defaults.comat) is one endpoint. The method and the path are decided by where the file is and what it is called (How paths are decided).

What you can write#

StatementMeaning
import Type from pathUse a type from another file (the path is relative to this file)
path /fix/path / path ./relativeSpell out the path (not recommended). See How paths are decided
params { ... }Path parameters (they match the [id] directory or file name)
search { ... }The query string
body Type / body { ... }The request body. One per endpoint. No body means no body
header { } headerReq { } headerRes { }Headers (Headers and cookies)
sNNN TypeThe response body per status (Status codes)
wrap name / wrap name offTurn a wrapper on or off (init / defaults / wrappers)
cookie on / cookie offWhether cookies are sent and received
cache off / cache 5000 / cache 5000 cookieCaching of GET / QUERY responses (Caching). It has no effect in defaults.comat

wrap, cookie, headers and sNNN that you can write in defaults.comat may also be written here (the endpoint wins).

Example: PUT /user/:id#

comat
import UserData from ../../types/UserData.comat
import ApiError from ../../types/common.comat

// user/put__id.comat: the method is put, and the path is /user/:id from the location and file name
// A segment starting with `__` is a dynamic path. The type of the name (id) is declared in params
params {
    id: uuidv4,
}

body UserData

s200 UserData
s404 ApiError
  • params { id: uuidv4 } declares the type of [id] in the path. The :id in the path and params must match exactly, with nothing missing or extra, or it is an error.
  • body UserData takes a body of type UserData.
  • s200 UserData and s404 ApiError are the responses per status.

Example: query string and optional fields#

comat
import UserData from ../types/UserData.comat
import ApiError from ../types/common.comat

// The path is explicit (deprecated). To follow the file location, use user/get__id.comat -> /user/:id
path /user/:id

// Path parameter
params {
    id: uuidv4,
}

// Query string (?verbose=true). `?` makes it optional
search {
    verbose?: bool,
}

// There is no `body`, so the request has no body
s200 UserData
s404 ApiError

// User information must show updates immediately, so it is not cached (`cache off`).
// If you write nothing, it is not cached anyway. `cache off` is a marker that states the intent
cache off

The ? in verbose?: bool means the field may be omitted.

Example: building a body (... and empty)#

comat
// (noauth)/post_user.comat: the method is the file name (post), and the path is /user from the location and file name (the group (noauth) is not part of the path)
import UserData from ../../types/UserData.comat

// Sign-up is called while logged out. The auth wrapper is disabled in (noauth)/defaults.comat

// body <type>: the request body (client -> server). One per endpoint. Without `body`, there is no body
body {
    ...UserData,
    uid: empty,
}

// s[status code] <type>: the response body for that status (server -> client, nullable)
// `...UserData` spreads the fields, and `uid: empty` omits the key (the server does not send it)
s200 UserData
  • ...UserData expands the fields of UserData in place.
  • uid: empty removes that field (the key is omitted and cannot be sent). It lets you build a type such as "when registering, the server decides uid" out of UserData.

Methods#

get post put patch delete options query head. query is the HTTP QUERY method (a safe, idempotent call that sends its search conditions in the body).

comat
import UserData from ../types/UserData.comat

// QUERY: safe and idempotent like GET, but sends the search criteria in the body
body {
    name?: string,
    email?: email,
    limit?: int,
}

s200 UserData[]
comat
// OPTIONS: ask which methods the path supports
// The second block of s200 is the response headers (headers only for this status. Optional)
// An array of headers becomes a single string joined with ", " on the wire
s200 none, {
    Allow: string[],
}

Rules#

  • Two endpoints with the same method and path are an error (even in different groups).
  • Two body statements are an error.
  • params cannot have optional fields.
  • defaults.comat cannot contain body params search or path.