Advanced Features

Retry, timeout, headers, dedupe, throttle, and output validation

Retry (Flaky API)

This endpoint fails 2 out of 3 times. Retry is set to 3 attempts with 500ms delay on status 503.

useAction(flakyApi, {
  retry: { count: 3, delay: 500, statusCodes: [503] },
})

Timeout

The endpoint takes 3s. Timeout is set to 1s, so it will abort.

useAction(slowEndpoint, {
  timeout: 1000, // 1s timeout for a 3s endpoint
})

Custom Headers (static & dynamic)

// Static headers
useAction(echoHeaders, {
  headers: { 'X-Custom-Token': 'my-static-token' },
})

// Dynamic headers (function called each request)
useAction(echoHeaders, {
  headers: () => ({
    'X-Request-Id': crypto.randomUUID(),
  }),
})

Dedupe (cancel / defer)

Click rapidly — 'cancel' aborts the previous request, 'defer' ignores subsequent ones until the first completes.

// Cancel: new request aborts the in-flight one
useAction(slowEndpoint, { dedupe: 'cancel' })

// Defer: new request is ignored while one is in-flight
useAction(slowEndpoint, { dedupe: 'defer' })

Throttle

Throttled to 1 call per second. Click rapidly — only fires once per interval.

Clicks: 0 | Server counter: —
useAction(throttleCounter, {
  throttle: 1000, // max 1 call per second
})

Output Schema Validation

The server validates both input AND output shape with Zod.

defineAction({
  input: z.object({ name: z.string(), role: z.enum([...]) }),
  outputSchema: z.object({
    id: z.number(),
    name: z.string(),
    role: z.string(),
    permissions: z.array(z.string()),
  }),
  handler: async ({ input }) => { ... },
})

Debounce Error Handling

When a debounced action throws, the error is properly surfaced to the caller instead of being silently swallowed.

// Errors are properly rejected, not silently lost
useAction(flakyApi, {
  debounce: 500,
  onError(err) {
    console.log('Error handled:', err.message)
  },
})

executeAsync (throw on error)

Unlike execute() which returns a result, executeAsync() throws on error — useful with try/catch.

try {
  const data = await action.executeAsync(input)
  // data is the unwrapped output (not ActionResult)
} catch (e) {
  // e is ActionError — thrown automatically
}