path の決め方

path は、comatDir からのディレクトリと、ファイル名から自動で決まります。

ディレクトリとファイル名#

ファイル(comatDir 以下)メソッドpath
post_user.comatPOST/user
users/post_uid.comatPOST/users/uid
users/[uid]/post.comatPOST/users/:uid(動的パス。推奨)
users/post_[uid].comatPOST/users/:uid(動的パス。非推奨)
users/posts/[id]/get.comatGET/users/posts/:id
users/get.comatGET/users(メソッド名だけ = ディレクトリ自体)
get.comatGET/
(noauth)/get_user.comatGET/user(グループは path に含まれない)
  • ファイル名は <method>_<segment>。最初の _ までがメソッド、残りが1 つ分のセグメントです(get_user_list は /user_list)。
  • 深い階層は、ディレクトリで表します。
  • ファイル名がメソッド名だけ(get.comat、post.comat)なら、ディレクトリ自体の path です。

動的なセグメント([名前])#

動的なセグメントは、[名前] で書きます。Next.js・Nuxt・SvelteKit・Astro などのファイルルーティングと同じ書き方で、Windows のファイル名にも使えます。名前の型は、そのファイルの params で宣言します。

書き方状態例
ディレクトリ名 [id]/get.comat推奨path/[id]/user/[userid]/get.comat → GET /path/:id/user/:userid
ファイル名 get_[id].comat許可(非推奨)path/[id]/user/get_[userid].comat → GET /path/:id/user/:userid
ファイル名 get__id.comat(__)廃止(エラー)
ファイル名 get_:id.comat(:)廃止(エラー)。path の明示では書ける
ディレクトリ名 :id/ / __id/廃止(エラー)
  • 名前は英数字・-・_ で、_ では始められません。
  • [...rest](残り全部に合う)や [[id]](省略可能)は、使えません(エラー)。
  • 同じ path になる 2 つのファイル(a/[id]/get.comat と a/get_[id].comat)は、重複エラーです。
  • 生成されるコードのモジュール名・ファイル名では、[id] を __id にそろえます(path/[id]/get.comat → path/__id/get、user/put_[id].comat → user/put__id)。そのため、生成物に [ ] は入りません。

comatype format で、非推奨・廃止の書き方を、推奨の形に直せます(CLI)。

シェルで書くとき

[ ] は、シェルのパターンとして解釈されます。ls path/[id] や mkdir path/[id] は、引用符で囲んでください(mkdir 'path/[id]')。

path を明示する(非推奨)#

ファイルの中に path を書くと、そちらが優先されます。comatype check は警告を出します。

書き方意味
path /fix/path固定。ディレクトリの位置に関係なく、その path
path ./relative/path相対。そのファイルのあるディレクトリの path からの相対

引用符は付けても付けなくても構いません(path '/user')。path の明示では、動的なセグメントを :名前 で書けます(path /user/:id)。

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

// user/get_me.comat: `path ./...` is relative to this directory's path (/user). Here it becomes /user/me
path ./me

s200 UserData

// Cache in a cookie for just 3 seconds (client only. The server does not use the cookie cache).
// The response is authenticated, so drop it with `clearCache()` on logout (the client cache key does not include Authorization)
cache 3000 cookie
comat
// user/(noauth)/get_ping.comat: `path /...` is a fixed path. It becomes /ping regardless of the directory position
path /ping

// The health check is called while logged out (the auth wrapper is disabled in user/(noauth)/defaults.comat)

s200 {
    ok: bool,
}

// Cache GET responses in memory for just 60 seconds (client and server). `cache off` forces it off,
// and `cache 60000 cookie` caches in a cookie (client only). Writing it in defaults.comat / init.comat has no effect (warning)
cache 60000

グループ (名前)#

ディレクトリ名を (名前) のように括弧で囲むと、グループになります。

  • そのグループの defaults.comat は、配下だけに効きます。
  • グループの名前は、path にも、生成されるコードのモジュール名にも含まれません。

たとえば (noauth)/defaults.comat に wrap auth off と書けば、(noauth) の下のエンドポイントだけ、認証のラッパーが外れます。

comat
// (noauth)/defaults.comat: a directory named `(name)` is a "group"
//   - This defaults.comat applies only below the group
//   - The group name is not part of the path ((noauth)/post_user.comat becomes /user)
// Here it disables the auth wrapper for the endpoints below
wrap auth off

全パターン#

パターンファイルpath
comatDir 直下post_user.comat / options_user.comat / query_user.comat/user
path を明示(非推奨)get_user.comat/user/:id
ディレクトリ自体user/get.comat/user
静的なセグメントuser/(noauth)/post_login.comat/user/login
動的なセグメントuser/[id]/put.comat(推奨)/ user/put_[id].comat/user/:id
ネストしたディレクトリuser/profile/get.comat/user/profile
グループ(直下)(noauth)/post_user.comat/user
固定のパスuser/(noauth)/get_ping.comat(path /ping)/ping
相対のパスuser/get_me.comat(path ./me)/user/me

エラーになるもの#

  • fetch_a.comat のように、ファイル名のメソッドが不正
  • path の :id が params に無い(または params にあって path に無い)
  • 同じメソッドと path のエンドポイントが複数ある
  • 生成されるモジュール名が重複する

途中に動的なセグメントがある path(/pets/:petId/photos/:photoId)は、ディレクトリでは表せないので、path を明示します。