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

Skip to content

ExamplesRequest Options

Request Options

Configure timeout, retry, cache, and Next.js revalidation per request or globally.

Timeout

The /slow endpoint takes 3 seconds. Set a shorter timeout to trigger a timeout error: the browser gives up, and cancels its request — Chrome records it below, cancelled; Safari records none.

run the call
ts
// How long the browser waits, in ms, via .with()
// The /slow endpoint takes 3 seconds: the browser gives up first, and cancels its request
try {
  await caller.misc.with({ timeout: 1000 }).slow()
} catch (error) {
  // TimeoutError: the call ran out of time in the browser
}
caller.misc.with({ timeout }).slow()SANDBOX
idle
// run the request to see the response

Requests

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

// no request yet: run the call

Options docs

Timeout on the Server

An endpoint's own timeout bounds the site's server's request to the API, and its retry asks again: the request below shows both attempts, each given up after a second, and the 504 the route answered with.

ts
// How long the server waits for the API, and whether it asks again: the endpoint's own
slowRetried: endpoint
  .get("slow")
  .timeout(1000)
  .retry({ limit: 1, delay: 200, jitter: "none", retryOn: ["timeout"] })

await caller.misc.slowRetried()
// → 504 after two attempts of a second each
caller.misc.slowRetried()SANDBOX
idle
// run the request to see the response

Requests

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

// no request yet: run the call

Retry

An endpoint's retry policy asks again on the answers it names. The request below shows each attempt, when it was sent and what the API answered.

ts
// Asked again on the server: twice more on a 503, 300 ms after each attempt
flaky: endpoint
  .get("error/:code")
  .variables<{ code: number }>()
  .retry({ limit: 2, delay: 300, backoff: "fixed", jitter: "none", retryOn: [503] })

await caller.misc.flaky({ code: 503 })
// → 503 after three attempts
caller.misc.flaky({ code: 503 })SANDBOX
idle
// run the request to see the response

Requests

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

// no request yet: run the call

Retry docs

Cache

cacheMiddleware keeps a successful read on the server for a while: the second read within 30 s is answered from it, and the request below says so — no request reaches the API.

ts
// A read kept on the server for 30 s, a copy for each visitor
import { cacheMiddleware } from "@noverax/courier/middleware"

cached: endpoint
  .get("")
  .use(cacheMiddleware({ scope: visitorScope, ttl: 30_000, maxEntries: 200 }))

await caller.posts.cached() // the API answers, and its answer is kept
await caller.posts.cached() // the cache answers: no request reaches the API
caller.posts.cached()SANDBOX

Read them twice: the second read, within 30 s of the first, is the cache's.

idle
// run the request to see the response

Requests

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

// no request yet: run the call

Next.js Revalidation

Use Next.js-specific options for ISR-style caching and tag-based revalidation.

ts
// Next.js revalidation — ISR-style caching
const data = await caller.users.with({
  next: { revalidate: 60 },  // revalidate every 60 seconds
}).getAll()

// Tag-based revalidation
const tagged = await caller.users.with({
  next: { tags: ["users"] },
}).getAll()

// Then in a Server Action:
import { revalidateTag } from "next/cache"
revalidateTag("users", "max")

The next option is passed through to the server-side fetch call. Works with both time-based and tag-based revalidation strategies.

Global Defaults

Set defaults for all requests via createProxyCaller or createProxyQueryCaller.

ts
import { createProxyCaller, isHTTPError } from "@noverax/courier/client"
import { routes } from "@noverax/courier/routes"

// Set defaults for all requests
const caller = createProxyCaller(routes.api, {
  timeout: 10000,           // 10s default timeout
  credentials: "include",  // send cookies
  headers: {
    "X-Custom-Header": "value",
  },

  // Global error handler
  onError: (error) => {
    if (isHTTPError(error) && error.status === 401) {
      window.location.href = "/login"
    }
  },
})

Global defaults apply to every request made through the instance. Per-request options override globals.