ステータスコード

エンドポイントのレスポンスは、sNNN でステータスごとに書きます(s200、s404 ...)。

comat
s200 UserData
s404 ApiError
s204 none

使えるコードは決まっています#

sNNN に書けるのは、登録されたコードだけです。

  • 標準のコード(IANA に登録されたもの)62 個
  • 設定ファイルの additionalStatusCodes で足したコード

それ以外(s499 や、未使用の s306)は、comatype check とエディタでエラーになります。wrapper の中の sNNN も同じです。

一覧は ステータスコード一覧 にあります。コマンドでも出せます。

shell
comatype statuses            # 一覧
comatype statuses --json     # JSON

コードを足す#

標準に無いコードは、comatype.config.json の最上位に書きます。

JSON
{
  "services": { "api": { "comatDir": "./comatfiles" } },
  "additionalStatusCodes": [
    { "status": 299, "message": "Custom Thing" },
    { "status": 499, "message": "Client Closed Request" }
  ]
}
  • status は 100〜599 の整数です。
  • 標準で定義済みのコード、重複、空の message は、設定のエラーです。
  • 足したコードは、本文を持てる普通のコードとして扱います。

本文の型を省略する#

ボディの型を省略した sNNN は、既定の本文になります。

comat
s400
s404
s500

は、それぞれ次と同じです。

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

ステータスのメッセージ(Not Found など)は本文の型には入りません。OpenAPI の説明と、エディタのホバー・補完に使います。

型を省略して、ヘッダだけ書くこともできます。

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

本文を持てないコード#

1xx、204、205、304 は、HTTP の決まりで本文を持てません。

  • 型を省略すると none になります(s204 と s204 none は同じ)。
  • s204 UserData のように本文を書くと、エラーです。
comat
s204 none
s304

レスポンスヘッダ#

sNNN 型, { ... } の 2 つ目のブロックが、そのステータスだけのレスポンスヘッダです。

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

ヘッダと cookie も見てください。

共通のステータス#

defaults.comat に書いたステータスは、配下の全エンドポイントに加わります(同じコードをエンドポイントに書けば、そちらが勝ちます)。

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

wrapper が宣言したステータス(s401 など)は、そのラッパーが有効なエンドポイントにだけ加わります。

呼び出し側から見ると#

2xx のボディは data、それ以外は error に入ります。ステータスごとに型が付くので、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