Status codes

An endpoint's responses are written per status with sNNN (s200, s404 ...).

comat
s200 UserData
s404 ApiError
s204 none

Only registered codes can be used#

What you can write in sNNN is only registered codes.

  • The 62 standard codes (registered with IANA)
  • Codes added with additionalStatusCodes in the config file

Anything else (s499, or the unused s306) is an error in comatype check and in the editor. The same goes for an sNNN inside a wrapper.

The list is on the Status code list page. You can also print it with a command.

shell
comatype statuses            # list
comatype statuses --json     # JSON

Adding codes#

Write codes that are not standard at the top level of comatype.config.json.

JSON
{
  "services": { "api": { "comatDir": "./comatfiles" } },
  "additionalStatusCodes": [
    { "status": 299, "message": "Custom Thing" },
    { "status": 499, "message": "Client Closed Request" }
  ]
}
  • status is an integer from 100 to 599.
  • A code that is already defined as standard, a duplicate, or an empty message is a config error.
  • An added code is treated as an ordinary code that can have a body.

Omitting the body type#

An sNNN with no body type gets the default body.

comat
s400
s404
s500

is the same as the following.

comat
s400 { message: string }
s404 { message: string }
s500 { message: string }

The status message (such as Not Found) is not part of the body type. It is used for OpenAPI descriptions and for hover and completion in the editor.

You can also omit the type and write only headers.

comat
s404, {
    "X-Reason"?: string,
}

Codes that cannot have a body#

1xx, 204, 205 and 304 cannot have a body under the HTTP rules.

  • Omitting the type makes it none (s204 and s204 none are the same).
  • Writing a body, as in s204 UserData, is an error.
comat
s204 none
s304

Response headers#

The second block in sNNN Type, { ... } is the response headers for that status only.

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[],
}

See also Headers and cookies.

Shared statuses#

Statuses written in defaults.comat are added to every endpoint below it (if an endpoint writes the same code, the endpoint wins).

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

Statuses declared by a wrapper (such as s401) are added only to endpoints where that wrapper is enabled.

From the caller's side#

The body of a 2xx goes into data, and anything else into error. Each status has its own type, so you can narrow by status.

TypeScript
const res = await c.put(path, { params: { id }, body: user }, s200, s404)
if (res.status === 200) res.data   // UserData
if (res.status === 404) res.error  // ApiError