How paths are decided

The path is decided automatically from the directories under comatDir and the file name.

Directories and file names#

File (under comatDir)MethodPath
post_user.comatPOST/user
users/post_uid.comatPOST/users/uid
users/[uid]/post.comatPOST/users/:uid (dynamic path. Recommended)
users/post_[uid].comatPOST/users/:uid (dynamic path. Not recommended)
users/posts/[id]/get.comatGET/users/posts/:id
users/get.comatGET/users (just the method name = the directory itself)
get.comatGET/
(noauth)/get_user.comatGET/user (groups are not part of the path)
  • A file name is <method>_<segment>. Everything up to the first _ is the method, and the rest is one segment (get_user_list is /user_list).
  • Express deeper levels with directories.
  • A file named only after a method (get.comat, post.comat) is the path of the directory itself.

Dynamic segments ([name])#

Write a dynamic segment as [name]. This is the same notation as file routing in Next.js, Nuxt, SvelteKit and Astro, and it can be used in Windows file names. Declare the type of the name in the file's params.

NotationStatusExample
Directory name [id]/get.comatRecommendedpath/[id]/user/[userid]/get.comat → GET /path/:id/user/:userid
File name get_[id].comatAllowed (not recommended)path/[id]/user/get_[userid].comat → GET /path/:id/user/:userid
File name get__id.comat (__)Removed (error)
File name get_:id.comat (:)Removed (error). It can still be written in an explicit path
Directory name :id/ / __id/Removed (error)
  • A name is made of letters, digits, - and _, and cannot start with _.
  • [...rest] (matches everything left) and [[id]] (optional) cannot be used (error).
  • Two files that give the same path (a/[id]/get.comat and a/get_[id].comat) are a duplicate error.
  • In the module names and file names of generated code, [id] becomes __id (path/[id]/get.comat → path/__id/get, user/put_[id].comat → user/put__id). So no [ or ] ends up in generated output.

comatype format rewrites the not recommended and removed notations into the recommended form (CLI).

In a shell

[ and ] are interpreted as shell patterns. Quote ls path/[id] and mkdir path/[id] (mkdir 'path/[id]').

If you write path inside the file, it takes priority. comatype check gives a warning.

NotationMeaning
path /fix/pathFixed. That path, wherever the directory is
path ./relative/pathRelative. Relative to the path of the directory the file is in

Quotes are optional (path '/user'). In an explicit path, dynamic segments are written as :name (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

Groups (name)#

A directory name wrapped in parentheses, like (name), becomes a group.

  • The defaults.comat of a group applies only below it.
  • The name of a group is not included in the path or in the module names of generated code.

For example, writing wrap auth off in (noauth)/defaults.comat removes the authentication wrapper only from the endpoints under (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

All patterns#

PatternFilePath
Directly under comatDirpost_user.comat / options_user.comat / query_user.comat/user
Explicit path (not recommended)get_user.comat/user/:id
The directory itselfuser/get.comat/user
Static segmentuser/(noauth)/post_login.comat/user/login
Dynamic segmentuser/[id]/put.comat (recommended) / user/put_[id].comat/user/:id
Nested directoriesuser/profile/get.comat/user/profile
Group (directly under)(noauth)/post_user.comat/user
Fixed pathuser/(noauth)/get_ping.comat (path /ping)/ping
Relative pathuser/get_me.comat (path ./me)/user/me

What is an error#

  • A method in the file name that is not valid, such as fetch_a.comat
  • A :id in the path that is not in params (or in params but not in the path)
  • Several endpoints with the same method and path
  • Duplicate generated module names

A path with a dynamic segment in the middle (/pets/:petId/photos/:photoId) cannot be expressed with directories, so spell out the path.