Skip to content

ExamplesRequest Groups

Request Groups

Organize endpoints into groups with prefixes using defineGroup.

both modes

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

defineGroup docs

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/1
q.users · q.postsSANDBOX

users.getAll

sending…
undefined

posts.getAll

sending…
undefined

users.getById · id 1

sending…
undefined