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#
| Statement | Meaning |
|---|---|
import Type from path | Use a type from another file (the path is relative to this file) |
path /fix/path / path ./relative | Spell 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 Type | The response body per status (Status codes) |
wrap name / wrap name off | Turn a wrapper on or off (init / defaults / wrappers) |
cookie on / cookie off | Whether cookies are sent and received |
cache off / cache 5000 / cache 5000 cookie | Caching 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 ApiErrorparams { id: uuidv4 }declares the type of[id]in the path. The:idin the path andparamsmust match exactly, with nothing missing or extra, or it is an error.body UserDatatakes a body of typeUserData.s200 UserDataands404 ApiErrorare 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 offThe ? 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...UserDataexpands the fields ofUserDatain place.uid: emptyremoves that field (the key is omitted and cannot be sent). It lets you build a type such as "when registering, the server decidesuid" out ofUserData.
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
bodystatements are an error. paramscannot have optional fields.defaults.comatcannot containbodyparamssearchorpath.