Files
dashboard/docs/contexts/api-client.md
sakuradairong dc9c9d9735 fix(i18n): localize RouteModal, SetupKeyModal, CreateAccessTokenModal
- RouteModal: routeType, networkRange, domains, distributionGroups, metric
- SetupKeyModal: createTitle, nameHelp, usageLimitHelp, expiresIn
- CreateAccessTokenModal: tokenName, tokenNameHelp, tokenExpiresIn
- Add corresponding keys to en.ts and zh.ts
2026-06-23 21:38:18 +08:00

2.1 KiB

API Client

Purpose

Centralized API client with SWR integration and OIDC authentication.

File Path

  • src/utils/api.tsx

Key Exports

// Main hook for API calls
export default function useFetchApi<T>(
  url: string,
  options?: RequestOptions
): SWRResponse<T, ErrorResponse>;

// Request options
type RequestOptions = {
  key?: string;
  signal?: AbortSignal;
  origin?: string;
  globalParams?: Params;
  ignoreGlobalParams?: boolean;
  refreshInterval?: number;
  blob?: boolean;
  shouldRetryOnError?: boolean;
};

// Error response type
export type ErrorResponse = {
  code: number;
  message: string;
};

// Query params type
export type Params = Record<string, string | number | boolean>;

Usage Patterns

GET Request

const { data, isLoading, error } = useFetchApi<Peer[]>("/peers");

GET with Refresh

const { data } = useFetchApi<Peer[]>("/peers", {
  refreshInterval: 5000, // Poll every 5 seconds
});

POST/PUT/DELETE

const { mutate } = useFetchApi("/peers", { method: "POST" });
// Or use direct apiRequest function

Custom Key

const { data } = useFetchApi("/peers", {
  key: "my-custom-key",
});

Implementation Details

Authentication

  • Uses OIDC tokens from @axa-fr/react-oidc
  • Automatically injects Authorization header
  • Handles token refresh

Caching

  • Uses SWR for caching and revalidation
  • Global config in ApplicationProvider
  • Supports refresh intervals

Error Handling

  • Returns structured ErrorResponse
  • Integrates with ErrorBoundary
  • Configurable retry behavior

Configuration

  • Base URL from config.json (apiOrigin)
  • Can override with origin option
  • Global params merged from ApplicationContext

Dependencies

  • swr - Data fetching and caching
  • @axa-fr/react-oidc - OIDC authentication
  • react-jwt - JWT token handling

Gotchas

  • Requires OIDC provider to be initialized
  • Tokens stored in memory (not localStorage)
  • Global params applied to all requests unless ignoreGlobalParams: true
  • Use shouldRetryOnError: false to disable automatic retries