This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

ExamplesRequest Groups

Request Groups

Organize endpoints into groups with prefixes using defineGroup.

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: each request below names the path the site's server asked the API for, the group's prefix and the endpoint's own path together.

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

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call