Skip to content

refactor: extract duplicated helpers into shared modules - #3

Merged
erubboli merged 1 commit into
mainfrom
refactor/dry-extract-shared-helpers
Sep 1, 2026
Merged

refactor: extract duplicated helpers into shared modules#3
erubboli merged 1 commit into
mainfrom
refactor/dry-extract-shared-helpers

Conversation

@erubboli

@erubboli erubboli commented Sep 1, 2026

Copy link
Copy Markdown
Member

Summary

DRY refactor of duplication identified across components and API routes. No behavior changes intended — every extracted helper is byte-identical (or a strict superset, via default params / optional headers) to the copies it replaces.

Extractions

New shared module Replaces
src/lib/client-rpc.tsrpc<T>() browser-side RPC wrapper 12 identical copies across components
src/lib/api-utils.tsjson() + readFormData() 12 local json() helpers; 6 request.formData() try/catch blocks
src/lib/token-utils.tstoHexField() 3 copies; placed next to its inverse hexToText()
src/components/ui/ModalOverlay.tsx 3 identical overlay shells (IssueTokenModal, IssueNFTModal, TokenManagePanel via maxWidth/maxHeight props)
src/lib/passkey.tsconsumeChallengeFromRequest() verbatim challenge-cookie parsing in both passkey verify routes

All 4 passkey routes now use the shared json() helper (extended with an optional headers param, backward compatible).

Deliberately left alone

  • SyncStatus.tsx raw fetch — tolerates partial RPC failure; the throwing helper would change behavior
  • rpc.ts jsonError — different response envelope (error: {message, code})
  • IssueTokenButton/IssueNFTButton, ModeToggle — only 2 occurrences; premature abstraction
  • Marketplace/PoolActions/ReceiveModal overlay markup — styling/structure genuinely differ

Test plan

  • 502/502 vitest tests pass (passkey route tests updated to mock the new consumeChallengeFromRequest export)
  • tsc --noEmit clean
  • Net −236 lines (170 insertions / 406 deletions across 37 files)

- client rpc<T>() fetch wrapper (12 copies) -> src/lib/client-rpc.ts
- server json() Response helper (12 copies) + readFormData() (6 try/catch
  blocks) -> src/lib/api-utils.ts
- toHexField() (3 copies) -> src/lib/token-utils.ts, next to its inverse
  hexToText()
- ModalOverlay component (3 identical overlay shells) ->
  src/components/ui/ModalOverlay.tsx
- consumeChallengeFromRequest() (verbatim in both passkey verify routes)
  -> src/lib/passkey.ts; all 4 passkey routes now use the shared json()

No behavior changes. Tests updated to mock the new passkey export.
502/502 tests pass; tsc clean. Net -294 lines.
@erubboli erubboli assigned erubboli and anyxem and unassigned erubboli Sep 1, 2026
@erubboli
erubboli merged commit a43ec40 into main Sep 1, 2026
3 checks passed
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