refactor: extract shared apiClient and vectorMath utilities - #4
refactor: extract shared apiClient and vectorMath utilities#4devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
✅ Runtime test — PR #4 (shared Tested locally (Vite dev server). This is a behavior-preserving refactor; verified both extracted utilities at runtime.
{ "url": "/api/auth/login", "method": "POST",
"headers": { "Content-Type": "application/json" },
"body": "{\"email\":\"test@example.com\",\"password\":\"password123\"}" }With a token in localStorage ( ✅ vectorMath collectors — verified against a real headless Rapier sim (expand)Ran the actual
|
Summary
Two verbatim-duplicated code patterns were factored into shared utilities. Pure refactor — no behavior change.
1. Frontend HTTP client — the exact same
request<T>()helper (plusAPI_BASEconstant andphyverse-tokenlocalStorage/Bearerheader handling) was copy-pasted inauthApi.ts,aiConfigApi.ts, andcloudApi.ts, with the token/header block duplicated again insendAiChat. Extracted intosrc/shared/utils/apiClient.ts:The three api modules now just call
apiRequest(...);sendAiChatusesbuildAuthHeaders().AuthProvider.tsxnow importsTOKEN_KEY/getStoredTokeninstead of redefining the token key literal.2. Vector math in experiment data collectors —
Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z)and its horizontalsqrt(x²+z²)/sqrt(dx²+dz²)variants were repeated across ~8 mechanics experiments. Extracted intosrc/shared/utils/vectorMath.ts:Applied to
centripetalForce,curvilinearMotion,energyConservation,motionComposition,freeFall,tickerTimer,newtonSecondLaw,galileoIncline. Rapier'stranslation()/linvel()return{x,y,z}objects that satisfy the helper signatures, so call sites collapse e.g. toreturn magnitude(ball.rigidBody.linvel()).Notes
tsc -bpasses; ESLint is clean on all touched files.api/ai/chat.ts,AiAgentPanel.tsx,sandboxStore.ts, and a broken vitest/jsdom setup in this environment (native binding / ESM resolution) — both unrelated to this refactor.Link to Devin session: https://app.devin.ai/sessions/cee3bacf2df6421f8934c4bafc857a60
Requested by: @Zyz555444