Group Definition
Each group has a prefix that goes in front of every path it calls.
ts
import { courier, defineApi, defineGroup } from "@noverax/courier/define"
const { endpoint } = courier()
// Each defineGroup has a prefix that maps to a backend path
const users = defineGroup({
prefix: "users", // → /users/*
endpoints: {
getAll: endpoint.get("").response<UsersListResponse>(), // GET /users
getById: endpoint.get(":id").variables<{ id: number }>().response<User>(), // GET /users/:id
create: endpoint.post("").variables<CreateBody>().response<User>(), // POST /users
},
})
const posts = defineGroup({
prefix: "posts", // → /posts/*
endpoints: {
getAll: endpoint.get("").response<PostsListResponse>(), // GET /posts
create: endpoint.post("").variables<CreateBody>().response<Post>(), // POST /posts
},
})
// Groups are registered in defineApi
const api = defineApi({
baseURL: "http://localhost:4000",
groups: { users, posts },
})users
calls /users/*
getAll, getById, create, update, patch, remove
posts
calls /posts/*
getAll, getById, create
misc
calls the root
stream, slow, error
Calling Across Groups
Each group is a property of the client — q.users, q.posts — and every call of a group goes under its prefix.
ts
import { q } from "@/lib/query"
// Dot-notation — group name maps to the key in defineApi groups
const users = q.users.useQuery("getAll")
const posts = q.posts.useQuery("getAll")
const user = q.users.useQuery("getById", { variables: { id: 1 } })
// The prefix is prepended automatically:
// users:getAll → GET /users
// posts:getAll → GET /posts
// users:getById → GET /users/1q.users · q.postsSANDBOX
undefinedundefinedundefined