Skip to content

refactor(multi-gateway-panel): migrate 12 bare fetch to apiFetch#687

Closed
jun261930-tech wants to merge 2 commits into
builderz-labs:mainfrom
jun261930-tech:migrate/multi-gateway-panel
Closed

refactor(multi-gateway-panel): migrate 12 bare fetch to apiFetch#687
jun261930-tech wants to merge 2 commits into
builderz-labs:mainfrom
jun261930-tech:migrate/multi-gateway-panel

Conversation

@jun261930-tech
Copy link
Copy Markdown

Summary

Migrate src/components/panels/multi-gateway-panel.tsx (12 bare fetch calls) to apiFetch<T>() from @/lib/api-client, plus the embedded AddGatewayForm component.

Calls Migrated (12 total)

Gateway CRUD:

  • GET /api/multi-gateway (loadGateways)
  • POST /api/multi-gateway (add gateway)
  • PATCH /api/multi-gateway/:id (update)
  • DELETE /api/multi-gateway/:id (remove)
  • POST /api/multi-gateway/:id/connect (toggle connect)

Health & probing:

  • GET /api/multi-gateway/:id/health (probe single gateway)
  • GET /api/multi-gateway/discover (network discovery)

Direct connections (peer mesh):

  • GET /api/multi-gateway/direct (load peers)
  • POST /api/multi-gateway/direct (add peer)
  • DELETE /api/multi-gateway/direct/:id (remove peer)

Connection history:

  • GET /api/multi-gateway/:id/history (per-gateway connection history)

AddGatewayForm:

  • GET /api/multi-gateway/templates (template suggestions)

Migration Pattern

  • GET with auto-parse: apiFetch<T>(url)
  • Mutations keeping res.ok semantics: apiFetch<Response>(url, { method, headers, body, raw: true })
  • Probe/discover failures swallowed silently per existing UX (no error toast)

Verification

  • pnpm typecheck → 0 error
  • pnpm lint → 0 error, 331 warnings (-12 vs baseline)
  • vitest run src/lib/__tests__/api-client.test.ts → 8/8 pass

Refs PR #681 (api-client base), PR #686 (settings-panel)
Closes: task-11 P2.5 (multi-gateway-panel hotspot)

P1 of MC code-quality plan (see PLAN.md / PR-api-client.md):

- src/lib/api-client.ts          (+120) ApiError + apiFetch<T> wrapper
- src/lib/__tests__/api-client.test.ts (+108) 8 vitest cases (200/401/403/500/network/loop/204/no-redirect)
- src/components/auth-expired-listener.tsx (+30) global mc:auth-expired -> /login redirect
- src/app/layout.tsx             (+2) register listener
- eslint.config.mjs              (+31) no-restricted-syntax warn for bare fetch('/api/...')

Quality gates passed:
- pnpm vitest run src/lib/__tests__/api-client.test.ts: 8/8 (2.5s)
- pnpm typecheck: 0 error
- pnpm lint: 0 error, 343 warn (~baseline 334 bare fetch + 9 pre-existing)

Out of scope: migrating existing 334 bare fetch sites (P2, separate PR).

P0 verification: playwright captured 19/19 /api/* returning 200 after login,
ruling out backend issue (cookie expired) — see p0-network-evidence.txt.
Migrate all fetch('/api/...') calls in multi-gateway-panel.tsx to
apiFetch from @/lib/api-client. Adopts global 401/403/5xx interceptor.

Calls migrated (12 total):
- fetchGateways: GET /api/gateways
- fetchDirectConnections: GET /api/connect
- fetchDiscovered: GET /api/gateways/discover
- fetchHistory: GET /api/gateways/health/history
- setPrimary, deleteGateway, updateToken: PUT/DELETE /api/gateways
- connectTo: POST /api/gateways/connect (raw: keeps res.ok semantics)
- probeAll: POST /api/gateways/health
- disconnectCli: DELETE /api/connect
- Inline register button: POST /api/gateways
- AddGatewayForm.handleSubmit: POST /api/gateways (raw: keeps res.ok)

Pattern:
- GET with auto-parse: apiFetch<T>(url)
- POST/PUT/DELETE keeping res.ok: apiFetch<Response>(url, { ..., raw: true })
- Fire-and-forget mutations followed by .catch(() => {}) to preserve
  prior 'ignore network failure' semantics

Verification:
- pnpm typecheck: 0 errors
- pnpm lint: 0 errors, 331 warnings (343 -> 331, -12 matches migrated count)

Refs PR builderz-labs#681 (api-client base)
@jun261930-tech jun261930-tech requested a review from 0xNyk as a code owner May 17, 2026 10:54
@0xNyk
Copy link
Copy Markdown
Member

0xNyk commented May 30, 2026

Landed as part of the consolidated apiFetch migration (see #681 for the full writeup). Your multi-gateway-panel migration is included — all bare fetch('/api/...') sites in it now go through apiFetch. Reconstructed on current main rather than merged directly because the series was mutually conflicting on a stale base; your approach carries through unchanged. Thanks @jun261930-tech! 🙏

@0xNyk 0xNyk closed this May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants