Skip to content

Consolidate the three API access patterns into a single client module #3

Description

@priscaenoch

Problem

The frontend reaches the backend three different ways, which duplicates configuration and error handling:

  1. src/api.ts uses a hardcoded base with raw fetch.
  2. Several pages call relative /api/... paths directly: src/pages/RpcMetricsDashboard.tsx, src/pages/SetupPage.tsx, src/pages/BatchMultiCall.tsx, src/pages/RateLimitDashboard.tsx, and src/hooks/useTxStatus.ts.
  3. src/services/sandbox-api.ts uses its own VITE_API_URL base.

Unifying these is cross-cutting: it touches the new shared client, every ad-hoc caller, and their tests.

What needs to be done

  • Create a single API client (src/services/apiClient.ts) that owns the base URL (from the configurable-base work), JSON handling, typed error normalization, and admin auth headers.
  • Route all callers through it: src/api.ts, src/services/sandbox-api.ts, src/hooks/useTxStatus.ts, and the four pages listed above.
  • Standardize error handling so non-2xx responses throw a typed error with status and message.
  • Add or update tests for the client and the migrated callers.

Files

  • new src/services/apiClient.ts
  • src/api.ts, src/services/sandbox-api.ts, src/hooks/useTxStatus.ts
  • src/pages/RpcMetricsDashboard.tsx, src/pages/SetupPage.tsx, src/pages/BatchMultiCall.tsx, src/pages/RateLimitDashboard.tsx
  • tests under test/

Acceptance deliverables

  • Exactly one module defines the API base and fetch/error behavior.
  • No page constructs API URLs or handles fetch errors ad hoc.
  • All CI checks pass; the change cannot be merged until CI is green.

Tests to pass

  • Unit tests for apiClient: success parsing, non-2xx throwing a typed error, base URL resolution.
  • Existing page tests continue to pass after the migration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ADVANCESubstantial, cross-cutting work spanning more than five files. Not a minor fix.GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third CampaignenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions